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
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
Prev Previous commit
Next Next commit
rollback function readFromFileOrStdin
  • Loading branch information
nevill committed Sep 6, 2021
commit 139f53bffdaaa499255743206a3e5993e3bf9ea7
30 changes: 8 additions & 22 deletions cmd/client/command/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"io/ioutil"
"net/http"
"os"
"strings"

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

yamlHandler func(doc, name string)
)

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

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

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

func readFromFileOrStdin(specFile string, cmd *cobra.Command, handler yamlHandler) {
func readFromFileOrStdin(specFile string, cmd *cobra.Command) ([]byte, string) {
var buff []byte
var err error
if specFile != "" {
Expand All @@ -186,22 +182,12 @@ func readFromFileOrStdin(specFile string, cmd *cobra.Command, handler yamlHandle
Kind string `yaml:"kind"`
Name string `yaml:"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)
err = yaml.Unmarshal(buff, &spec)
if err != nil {
ExitWithErrorf("%s failed, invalid spec: %v", cmd.Short, err)
}

return buff, spec.Name
}

nevill marked this conversation as resolved.
Show resolved Hide resolved
func buildVisitorFromFileOrStdin(specFile string, cmd *cobra.Command) Visitor {
Expand Down