Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve egctl to support sending multi configs at once (fix #29) #230

Merged
merged 5 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions cmd/client/command/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,20 @@ func readFromFileOrStdin(specFile string, cmd *cobra.Command) ([]byte, string) {

return buff, spec.Name
}

nevill marked this conversation as resolved.
Show resolved Hide resolved
func buildVisitorFromFileOrStdin(specFile string, cmd *cobra.Command) Visitor {
var buff []byte
var err error
if specFile != "" {
buff, err = ioutil.ReadFile(specFile)
if err != nil {
ExitWithErrorf("%s failed: %v", cmd.Short, err)
}
} else {
buff, err = ioutil.ReadAll(os.Stdin)
if err != nil {
ExitWithErrorf("%s failed: %v", cmd.Short, err)
}
}
return NewStreamVisitor(string(buff))
}
16 changes: 12 additions & 4 deletions cmd/client/command/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ func createObjectCmd() *cobra.Command {
Use: "create",
Short: "Create an object from a yaml file or stdin",
Run: func(cmd *cobra.Command, args []string) {
buff, _ := readFromFileOrStdin(specFile, cmd)
handleRequest(http.MethodPost, makeURL(objectsURL), buff, cmd)
visitor := buildVisitorFromFileOrStdin(specFile, cmd)
visitor.Visit(func(docs []spec) {
for _, d := range docs {
handleRequest(http.MethodPost, makeURL(objectsURL), []byte(d.doc), cmd)
}
})
},
}

Expand All @@ -77,8 +81,12 @@ func updateObjectCmd() *cobra.Command {
Use: "update",
Short: "Update an object from a yaml file or stdin",
Run: func(cmd *cobra.Command, args []string) {
buff, name := readFromFileOrStdin(specFile, cmd)
handleRequest(http.MethodPut, makeURL(objectURL, name), buff, cmd)
visitor := buildVisitorFromFileOrStdin(specFile, cmd)
visitor.Visit(func(docs []spec) {
for _, d := range docs {
handleRequest(http.MethodPut, makeURL(objectURL, d.Name), []byte(d.doc), cmd)
}
})
},
}

Expand Down
78 changes: 78 additions & 0 deletions cmd/client/command/visitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package command

import (
"bufio"
"io"
"strings"

"k8s.io/apimachinery/pkg/util/yaml"
)

// VisitorFunc executes visition logic
type VisitorFunc func([]spec)
nevill marked this conversation as resolved.
Show resolved Hide resolved

// Visitor walk through the document via VisitorFunc
type Visitor interface {
Visit(VisitorFunc)
}

type spec struct {
Kind string
Name string
doc string
}

type streamVisitor struct {
io.Reader
}

// NewStreamVisitor returns a streamVisitor.
func NewStreamVisitor(src string) *streamVisitor {
return &streamVisitor{
Reader: strings.NewReader(src),
}
}

type yamlDecoder struct {
reader *yaml.YAMLReader
doc string
}

func newYAMLDecoder(r io.Reader) *yamlDecoder {
return &yamlDecoder{
reader: yaml.NewYAMLReader(bufio.NewReader(r)),
}
}

// Decode reads a YAML document into bytes and tries to yaml.Unmarshal it.
func (d *yamlDecoder) Decode(into interface{}) error {
bytes, err := d.reader.Read()
if err != nil && err != io.EOF {
return err
}
d.doc = string(bytes)
if len(bytes) != 0 {
err = yaml.Unmarshal(bytes, into)
}
return err
}

// Visit implements Visitor over a stream.
func (v *streamVisitor) Visit(fn VisitorFunc) {
d := newYAMLDecoder(v.Reader)
var validDocs []spec
for {
var s spec
if err := d.Decode(&s); err != nil {
if err == io.EOF {
break
} else {
ExitWithErrorf("error parsing %s: %v", d.doc, err)
}
}
s.doc = d.doc
//TODO can validate spec's Kind here
validDocs = append(validDocs, s)
}
fn(validDocs)
}
8 changes: 6 additions & 2 deletions cmd/client/command/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ func wasmApplyDataCmd() *cobra.Command {
},

Run: func(cmd *cobra.Command, args []string) {
buf, _ := readFromFileOrStdin(specFile, cmd)
handleRequest(http.MethodPut, makeURL(wasmDataURL, args[0], args[1]), buf, cmd)
visitor := buildVisitorFromFileOrStdin(specFile, cmd)
visitor.Visit(func(docs []spec) {
for _, d := range docs {
handleRequest(http.MethodPut, makeURL(wasmDataURL, args[0], args[1]), []byte(d.doc), cmd)
}
})
},
}
cmd.Flags().StringVarP(&specFile, "file", "f", "", "A yaml file specifying the object.")
Expand Down