Skip to content

Commit

Permalink
support create via stdin '-'
Browse files Browse the repository at this point in the history
Signed-off-by: Jeeva Kandasamy <[email protected]>
  • Loading branch information
jkandasa committed Jan 20, 2022
1 parent 3707f54 commit d234fb3
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
24 changes: 23 additions & 1 deletion cmd/command/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package command

import (
"fmt"
"io/ioutil"

jenkins "github.com/jkandasa/jenkinsctl/pkg/jenkins"
cliML "github.com/jkandasa/jenkinsctl/pkg/model/cli"
"github.com/jkandasa/jenkinsctl/pkg/utils"
stdinUtils "github.com/jkandasa/jenkinsctl/pkg/utils/read_stdin"
"github.com/spf13/cobra"
)

Expand All @@ -27,14 +29,34 @@ var createResource = &cobra.Command{
Short: "Create a resource from a file",
Example: ` # create a build using the date in yaml file
jenkinsctl create -f my_build.yaml
# create a build based on the YAML passed into stdin.
cat my_build.yaml | jenkinsctl create -f -
`,
Run: func(cmd *cobra.Command, args []string) {
if CONFIG.JobContext == "" {
fmt.Fprintf(ioStreams.ErrOut, "job context not set")
return
}

resourceInterface, err := utils.GetResource(resourceFile)
var data []byte
if resourceFile == "-" { // process's standard input
stdinData, err := stdinUtils.ReadStdIn()
if err != nil {
fmt.Fprintln(ioStreams.ErrOut, err)
return
}
data = stdinData
} else {
bytes, err := ioutil.ReadFile(resourceFile)
if err != nil {
fmt.Fprintln(ioStreams.ErrOut, err)
return
}
data = bytes
}

resourceInterface, err := utils.GetResource(data)
if err != nil {
fmt.Fprintln(ioStreams.ErrOut, err)
return
Expand Down
50 changes: 50 additions & 0 deletions pkg/utils/read_stdin/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package utils

import (
"bufio"
"io"
"os"
)

type lineIterator struct {
reader *bufio.Reader
}

func newLineIterator(rd io.Reader) *lineIterator {
return &lineIterator{
reader: bufio.NewReader(rd),
}
}

func (ln *lineIterator) next() ([]byte, error) {
var bytes []byte
for {
line, isPrefix, err := ln.reader.ReadLine()
if err != nil {
return nil, err
}
bytes = append(bytes, line...)
if !isPrefix {
break
}
}
return bytes, nil
}

func ReadStdIn() ([]byte, error) {
ln := newLineIterator(os.Stdin)
lines := make([]byte, 0)
for {
line, err := ln.next()
if err != nil {
if err == io.EOF {
break
} else {
return nil, err
}
}
lines = append(lines, line...)
lines = append(lines, '\n')
}
return []byte(lines), nil
}
6 changes: 3 additions & 3 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func FileToStruct(filename string, out interface{}) error {
return yaml.Unmarshal(bytes, out)
}

func GetResource(filename string) (interface{}, error) {
func GetResource(bytes []byte) (interface{}, error) {
kindData := &cliML.Kind{}
err := FileToStruct(filename, kindData)
err := yaml.Unmarshal(bytes, kindData)
if err != nil {
return nil, err
}
Expand All @@ -43,7 +43,7 @@ func GetResource(filename string) (interface{}, error) {
return nil, fmt.Errorf("unknown kind:%s", kindData.Kind)
}

if err = FileToStruct(filename, resource); err != nil {
if err = yaml.Unmarshal(bytes, resource); err != nil {
return nil, err
}
return resource, nil
Expand Down

0 comments on commit d234fb3

Please sign in to comment.