diff --git a/cmd/client/command/common.go b/cmd/client/command/common.go index 4fbb9c7da5..7cb0d2de40 100644 --- a/cmd/client/command/common.go +++ b/cmd/client/command/common.go @@ -23,6 +23,7 @@ import ( "io/ioutil" "net/http" "os" + "strings" yamljsontool "github.com/ghodss/yaml" "github.com/spf13/cobra" @@ -41,6 +42,8 @@ type ( Code int `yaml:"code"` Message string `yaml:"message"` } + + yamlHandler func(doc, name string) ) // CommandlineGlobalFlags is the singleton of GlobalFlags. @@ -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" @@ -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 != "" { @@ -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) + } } diff --git a/cmd/client/command/object.go b/cmd/client/command/object.go index e96308aac1..d185b714bd 100644 --- a/cmd/client/command/object.go +++ b/cmd/client/command/object.go @@ -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) + }) }, } @@ -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) + }) }, } diff --git a/cmd/client/command/wasm.go b/cmd/client/command/wasm.go index 3c79dbc255..e8af5e797d 100644 --- a/cmd/client/command/wasm.go +++ b/cmd/client/command/wasm.go @@ -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.")