diff --git a/cmd/create.go b/cmd/create.go index 44b2e63a..6a0b5071 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -7,9 +7,9 @@ import ( "github.com/elasticpath/epcc-cli/external/httpclient" "github.com/elasticpath/epcc-cli/external/json" "github.com/elasticpath/epcc-cli/external/resources" + log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "io/ioutil" - "log" "strings" ) diff --git a/cmd/root.go b/cmd/root.go index 692d6512..ea5eebc7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -28,6 +28,7 @@ func init() { get, create, delete, + update, resourceListCommand, ) diff --git a/cmd/update.go b/cmd/update.go new file mode 100644 index 00000000..64e1f93a --- /dev/null +++ b/cmd/update.go @@ -0,0 +1,82 @@ +package cmd + +import ( + "context" + "fmt" + "github.com/elasticpath/epcc-cli/external/completion" + "github.com/elasticpath/epcc-cli/external/httpclient" + "github.com/elasticpath/epcc-cli/external/json" + "github.com/elasticpath/epcc-cli/external/resources" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "io/ioutil" + "strings" +) + +var update = &cobra.Command{ + Use: "update [PARENT_ID_1] [PARENT_ID_2] [ID]... ...", + Short: "Updates an entity of a resource.", + Args: cobra.MinimumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + // Find Resource + resource, ok := resources.GetResourceByName(args[0]) + if !ok { + return fmt.Errorf("Could not find resource %s", args[0]) + } + + if resource.UpdateEntityInfo == nil { + return fmt.Errorf("resource %s doesn't support UPDATE", args[0]) + } + + // Count ids in UpdateEntity + resourceURL := resource.UpdateEntityInfo.Url + idCount, err := resources.GetNumberOfVariablesNeeded(resourceURL) + if err != nil { + return err + } + + // Replace ids with args in resourceURL + resourceURL, err = resources.GenerateUrl(resourceURL, args[1:]) + if err != nil { + return err + } + + args = append(args, "type", resource.JsonApiType) + // Create the body from remaining args + body, err := json.ToJson(args[(idCount+1):], false) + if err != nil { + return err + } + + // Submit request + resp, err := httpclient.DoRequest(context.TODO(), "PUT", resourceURL, "", strings.NewReader(body)) + if err != nil { + return fmt.Errorf("Got error %s", err.Error()) + } + defer resp.Body.Close() + + // Print the body + resBody, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatal(err) + } + + // Check if error response + if resp.StatusCode >= 400 && resp.StatusCode <= 600 { + json.PrintJson(string(resBody)) + return fmt.Errorf(resp.Status) + } + + return json.PrintJson(string(resBody)) + }, + + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return completion.Complete(completion.Request{ + Type: completion.CompleteSingularResource, + }) + } + + return []string{}, cobra.ShellCompDirectiveNoFileComp + }, +}