Skip to content

Commit

Permalink
improve egctl to support sending multi configs at once (fix #29)
Browse files Browse the repository at this point in the history
  • Loading branch information
nevill committed Sep 3, 2021
1 parent aad887f commit ab208eb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
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)

// 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)
}
}
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

0 comments on commit ab208eb

Please sign in to comment.