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’ll 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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions cmd/client/command/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io/ioutil"
"net/http"
"os"
"strings"

yamljsontool "github.com/ghodss/yaml"
"github.com/spf13/cobra"
Expand All @@ -41,6 +42,8 @@ type (
Code int `yaml:"code"`
Message string `yaml:"message"`
}

yamlHandler func(doc, name string)
)

// CommandlineGlobalFlags is the singleton of GlobalFlags.
Expand All @@ -61,8 +64,9 @@ const (
statusObjectURL = apiURL + "/status/objects/%s"
statusObjectsURL = apiURL + "/status/objects"

wasmCodeURL = apiURL + "/wasm/code"
wasmDataURL = apiURL + "/wasm/data/%s/%s"
wasmCodeURL = apiURL + "/wasm/code"
wasmDataURL = apiURL + "/wasm/data/%s/%s"
yamlSeparator = "---"

// MeshTenantsURL is the mesh tenant prefix.
MeshTenantsURL = apiURL + "/mesh/tenants"
Expand Down Expand Up @@ -163,7 +167,7 @@ func printBody(body []byte) {
fmt.Printf("%s", output)
}

func readFromFileOrStdin(specFile string, cmd *cobra.Command) ([]byte, string) {
func readFromFileOrStdin(specFile string, cmd *cobra.Command, handler yamlHandler) {
var buff []byte
var err error
if specFile != "" {
Expand All @@ -182,10 +186,20 @@ func readFromFileOrStdin(specFile string, cmd *cobra.Command) ([]byte, string) {
Kind string `yaml:"kind"`
Name string `yaml:"name"`
}
err = yaml.Unmarshal(buff, &spec)
if err != nil {
ExitWithErrorf("%s failed, invalid spec: %v", cmd.Short, err)
}

return buff, spec.Name
yamlDocs := strings.Split(string(buff), yamlSeparator)
nevill marked this conversation as resolved.
Show resolved Hide resolved

nevill marked this conversation as resolved.
Show resolved Hide resolved
// make sure each yaml doc valid
for _, yamlDoc := range yamlDocs {
if len(strings.TrimSpace(yamlDoc)) == 0 {
continue
}
err = yaml.Unmarshal([]byte(yamlDoc), &spec)
if err != nil {
ExitWithErrorf("%s failed, invalid spec: %v", cmd.Short, err)
break
}

handler(yamlDoc, spec.Name)
}
nevill marked this conversation as resolved.
Show resolved Hide resolved
}
10 changes: 6 additions & 4 deletions cmd/client/command/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ 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)
readFromFileOrStdin(specFile, cmd, func(doc, name string) {
handleRequest(http.MethodPost, makeURL(objectsURL), []byte(doc), cmd)
})
},
}

Expand All @@ -77,8 +78,9 @@ 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)
readFromFileOrStdin(specFile, cmd, func(doc, name string) {
handleRequest(http.MethodPut, makeURL(objectURL, name), []byte(doc), cmd)
})
},
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/client/command/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ 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)
readFromFileOrStdin(specFile, cmd, func(doc, name string) {
handleRequest(http.MethodPut, makeURL(wasmDataURL, args[0], args[1]), []byte(doc), cmd)
})
},
}
cmd.Flags().StringVarP(&specFile, "file", "f", "", "A yaml file specifying the object.")
Expand Down