Skip to content

Commit

Permalink
Resolves #355 - Allow commands to specified on the verb instead of ju…
Browse files Browse the repository at this point in the history
…st after the resource (#356)
  • Loading branch information
steve-r-west authored Jun 18, 2023
1 parent 58588ab commit 38bf518
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 71 deletions.
53 changes: 24 additions & 29 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

func NewCreateCommand(parentCmd *cobra.Command) {

var create = &cobra.Command{
var createCmd = &cobra.Command{
Use: "create",
Short: "Creates a resource",
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -34,23 +34,19 @@ func NewCreateCommand(parentCmd *cobra.Command) {
},
}

for _, resource := range resources.GetPluralResources() {

var autoFillOnCreate = false

var noBodyPrint = false

var outputJq = ""
var autoFillOnCreate = false
var noBodyPrint = false
var outputJq = ""
var setAlias = ""
var ifAliasExists = ""
var ifAliasDoesNotExist = ""

var setAlias = ""

var ifAliasExists = ""
var ifAliasDoesNotExist = ""
overrides := &httpclient.HttpParameterOverrides{
QueryParameters: nil,
OverrideUrlPath: "",
}

overrides := &httpclient.HttpParameterOverrides{
QueryParameters: nil,
OverrideUrlPath: "",
}
for _, resource := range resources.GetPluralResources() {

if resource.CreateEntityInfo == nil {
continue
Expand Down Expand Up @@ -188,21 +184,20 @@ func NewCreateCommand(parentCmd *cobra.Command) {
},
}

createResourceCmd.PersistentFlags().StringVar(&overrides.OverrideUrlPath, "override-url-path", "", "Override the URL that will be used for the Request")
createResourceCmd.PersistentFlags().BoolVarP(&autoFillOnCreate, "auto-fill", "", false, "Auto generate value for fields")
createResourceCmd.PersistentFlags().BoolVarP(&noBodyPrint, "silent", "s", false, "Don't print the body on success")
createResourceCmd.PersistentFlags().StringSliceVarP(&overrides.QueryParameters, "query-parameters", "q", []string{}, "Pass in key=value an they will be added as query parameters")
createResourceCmd.PersistentFlags().StringVarP(&outputJq, "output-jq", "", "", "A jq expression, if set we will restrict output to only this")
createResourceCmd.PersistentFlags().StringVarP(&setAlias, "save-as-alias", "", "", "A name to save the created resource as")
createResourceCmd.PersistentFlags().StringVarP(&ifAliasExists, "if-alias-exists", "", "", "If the alias exists we will run this command, otherwise exit with no error")
createResourceCmd.PersistentFlags().StringVarP(&ifAliasDoesNotExist, "if-alias-does-not-exist", "", "", "If the alias does not exist we will run this command, otherwise exit with no error")
createResourceCmd.MarkFlagsMutuallyExclusive("if-alias-exists", "if-alias-does-not-exist")
_ = createResourceCmd.RegisterFlagCompletionFunc("output-jq", jqCompletionFunc)

create.AddCommand(createResourceCmd)
createCmd.AddCommand(createResourceCmd)
}

parentCmd.AddCommand(create)
parentCmd.AddCommand(createCmd)
createCmd.PersistentFlags().StringVar(&overrides.OverrideUrlPath, "override-url-path", "", "Override the URL that will be used for the Request")
createCmd.PersistentFlags().BoolVarP(&autoFillOnCreate, "auto-fill", "", false, "Auto generate value for fields")
createCmd.PersistentFlags().BoolVarP(&noBodyPrint, "silent", "s", false, "Don't print the body on success")
createCmd.PersistentFlags().StringSliceVarP(&overrides.QueryParameters, "query-parameters", "q", []string{}, "Pass in key=value an they will be added as query parameters")
createCmd.PersistentFlags().StringVarP(&outputJq, "output-jq", "", "", "A jq expression, if set we will restrict output to only this")
createCmd.PersistentFlags().StringVarP(&setAlias, "save-as-alias", "", "", "A name to save the created resource as")
createCmd.PersistentFlags().StringVarP(&ifAliasExists, "if-alias-exists", "", "", "If the alias exists we will run this command, otherwise exit with no error")
createCmd.PersistentFlags().StringVarP(&ifAliasDoesNotExist, "if-alias-does-not-exist", "", "", "If the alias does not exist we will run this command, otherwise exit with no error")
createCmd.MarkFlagsMutuallyExclusive("if-alias-exists", "if-alias-does-not-exist")
_ = createCmd.RegisterFlagCompletionFunc("output-jq", jqCompletionFunc)
}

func createInternal(ctx context.Context, overrides *httpclient.HttpParameterOverrides, args []string, autoFillOnCreate bool, aliasName string) (string, error) {
Expand Down
15 changes: 11 additions & 4 deletions cmd/crud_smoke_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package cmd

import (
"fmt"
"github.com/elasticpath/epcc-cli/external/aliases"
"github.com/elasticpath/epcc-cli/external/httpclient"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
"math/rand"
"testing"
)

func TestCrudOnAResource(t *testing.T) {

httpclient.Initialize(1, 60)

// Use a random account name to prevent collisions with other tests
id := rand.Int63()
name := fmt.Sprintf("Test_%d", id)
alias := fmt.Sprintf("name=Test_%d", id)

cmd := getTestCommand()
cmd.SetArgs([]string{"create", "account", "name", "Test", "legal_name", "Test", "--output-jq", ".data.id", "--save-as-alias", "my_test_alias"})
cmd.SetArgs([]string{"create", "account", "name", name, "legal_name", "Test", "--output-jq", ".data.id", "--save-as-alias", "my_test_alias"})
err := cmd.Execute()
require.NoError(t, err)

Expand All @@ -23,7 +30,7 @@ func TestCrudOnAResource(t *testing.T) {
require.True(t, ok, "Expected that my_test_alias exists in the set of aliases :(")

cmd = getTestCommand()
cmd.SetArgs([]string{"get", "account", "name=Test", "--output-jq", ".data.name"})
cmd.SetArgs([]string{"get", "account", alias, "--output-jq", ".data.name"})
err = cmd.Execute()
require.NoError(t, err)

Expand All @@ -33,12 +40,12 @@ func TestCrudOnAResource(t *testing.T) {
require.NoError(t, err)

cmd = getTestCommand()
cmd.SetArgs([]string{"update", "account", "name=Test", "legal_name", "Test Update", "--output-jq", ".data.legal_name"})
cmd.SetArgs([]string{"update", "account", alias, "legal_name", "Test Update", "--output-jq", ".data.legal_name"})
err = cmd.Execute()
require.NoError(t, err)

cmd = getTestCommand()
cmd.SetArgs([]string{"delete", "account", "name=Test"})
cmd.SetArgs([]string{"delete", "account", alias})
err = cmd.Execute()
require.NoError(t, err)

Expand Down
32 changes: 17 additions & 15 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,20 @@ func NewDeleteCommand(parentCmd *cobra.Command) {
},
}

overrides := &httpclient.HttpParameterOverrides{
QueryParameters: nil,
OverrideUrlPath: "",
}

var allow404 = false

var ifAliasExists = ""
var ifAliasDoesNotExist = ""

for _, resource := range resources.GetPluralResources() {
if resource.DeleteEntityInfo == nil {
continue
}
overrides := &httpclient.HttpParameterOverrides{
QueryParameters: nil,
OverrideUrlPath: "",
}

var allow404 = false

var ifAliasExists = ""
var ifAliasDoesNotExist = ""

resource := resource
resourceName := resource.SingularName
Expand Down Expand Up @@ -147,15 +148,16 @@ func NewDeleteCommand(parentCmd *cobra.Command) {
return []string{}, cobra.ShellCompDirectiveNoFileComp
},
}
deleteResourceCommand.Flags().StringVar(&overrides.OverrideUrlPath, "override-url-path", "", "Override the URL that will be used for the Request")
deleteResourceCommand.Flags().StringSliceVarP(&overrides.QueryParameters, "query-parameters", "q", []string{}, "Pass in key=value an they will be added as query parameters")
deleteResourceCommand.Flags().BoolVar(&allow404, "allow-404", allow404, "If set 404's will not be treated as errors")
deleteResourceCommand.PersistentFlags().StringVarP(&ifAliasExists, "if-alias-exists", "", "", "If the alias exists we will run this command, otherwise exit with no error")
deleteResourceCommand.PersistentFlags().StringVarP(&ifAliasDoesNotExist, "if-alias-does-not-exist", "", "", "If the alias does not exist we will run this command, otherwise exit with no error")
deleteResourceCommand.MarkFlagsMutuallyExclusive("if-alias-exists", "if-alias-does-not-exist")

deleteCmd.AddCommand(deleteResourceCommand)
}

deleteCmd.PersistentFlags().StringVar(&overrides.OverrideUrlPath, "override-url-path", "", "Override the URL that will be used for the Request")
deleteCmd.PersistentFlags().StringSliceVarP(&overrides.QueryParameters, "query-parameters", "q", []string{}, "Pass in key=value an they will be added as query parameters")
deleteCmd.PersistentFlags().BoolVar(&allow404, "allow-404", allow404, "If set 404's will not be treated as errors")
deleteCmd.PersistentFlags().StringVarP(&ifAliasExists, "if-alias-exists", "", "", "If the alias exists we will run this command, otherwise exit with no error")
deleteCmd.PersistentFlags().StringVarP(&ifAliasDoesNotExist, "if-alias-does-not-exist", "", "", "If the alias does not exist we will run this command, otherwise exit with no error")
deleteCmd.MarkFlagsMutuallyExclusive("if-alias-exists", "if-alias-does-not-exist")
parentCmd.AddCommand(deleteCmd)
}
func deleteInternal(ctx context.Context, overrides *httpclient.HttpParameterOverrides, allow404 bool, args []string) (string, error) {
Expand Down
20 changes: 9 additions & 11 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,21 +247,19 @@ func NewGetCommand(parentCmd *cobra.Command) {
},
}

newCmd.PersistentFlags().BoolVarP(&noBodyPrint, "silent", "s", false, "Don't print the body on success")
newCmd.PersistentFlags().StringVar(&overrides.OverrideUrlPath, "override-url-path", "", "Override the URL that will be used for the Request")
newCmd.PersistentFlags().StringSliceVarP(&overrides.QueryParameters, "query-parameters", "q", []string{}, "Pass in key=value an they will be added as query parameters")
newCmd.PersistentFlags().StringVarP(&outputJq, "output-jq", "", "", "A jq expression, if set we will restrict output to only this")
newCmd.PersistentFlags().StringVarP(&retryWhileJQ, "retry-while-jq", "", "", "A jq expression, if set and returns true we will retry the get command (see manual for examples)")
newCmd.PersistentFlags().Uint16VarP(&retryWhileJQMaxAttempts, "retry-while-jq-max-attempts", "", 1200, "The maximum number of attempts we will retry with jq")
newCmd.PersistentFlags().StringVarP(&ifAliasExists, "if-alias-exists", "", "", "If the alias exists we will run this command, otherwise exit with no error")
newCmd.PersistentFlags().StringVarP(&ifAliasDoesNotExist, "if-alias-does-not-exist", "", "", "If the alias does not exist we will run this command, otherwise exit with no error")
newCmd.MarkFlagsMutuallyExclusive("if-alias-exists", "if-alias-does-not-exist")
_ = newCmd.RegisterFlagCompletionFunc("output-jq", jqCompletionFunc)

getCmd.AddCommand(newCmd)
}
}

getCmd.PersistentFlags().BoolVarP(&noBodyPrint, "silent", "s", false, "Don't print the body on success")
getCmd.PersistentFlags().StringVar(&overrides.OverrideUrlPath, "override-url-path", "", "Override the URL that will be used for the Request")
getCmd.PersistentFlags().StringSliceVarP(&overrides.QueryParameters, "query-parameters", "q", []string{}, "Pass in key=value an they will be added as query parameters")
getCmd.PersistentFlags().StringVarP(&outputJq, "output-jq", "", "", "A jq expression, if set we will restrict output to only this")
getCmd.PersistentFlags().StringVarP(&retryWhileJQ, "retry-while-jq", "", "", "A jq expression, if set and returns true we will retry the get command (see manual for examples)")
getCmd.PersistentFlags().Uint16VarP(&retryWhileJQMaxAttempts, "retry-while-jq-max-attempts", "", 1200, "The maximum number of attempts we will retry with jq")
getCmd.PersistentFlags().StringVarP(&ifAliasExists, "if-alias-exists", "", "", "If the alias exists we will run this command, otherwise exit with no error")
getCmd.PersistentFlags().StringVarP(&ifAliasDoesNotExist, "if-alias-does-not-exist", "", "", "If the alias does not exist we will run this command, otherwise exit with no error")
getCmd.MarkFlagsMutuallyExclusive("if-alias-exists", "if-alias-does-not-exist")
_ = getCmd.RegisterFlagCompletionFunc("output-jq", jqCompletionFunc)

parentCmd.AddCommand(getCmd)
Expand Down
23 changes: 11 additions & 12 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewUpdateCommand(parentCmd *cobra.Command) {
var ifAliasExists = ""
var ifAliasDoesNotExist = ""

var update = &cobra.Command{
var updateCmd = &cobra.Command{
Use: "update",
Short: "Updates a resource",
SilenceUsage: false,
Expand Down Expand Up @@ -162,19 +162,18 @@ func NewUpdateCommand(parentCmd *cobra.Command) {
},
}

updateResourceCmd.Flags().StringVar(&overrides.OverrideUrlPath, "override-url-path", "", "Override the URL that will be used for the Request")
updateResourceCmd.Flags().StringSliceVarP(&overrides.QueryParameters, "query-parameters", "q", []string{}, "Pass in key=value an they will be added as query parameters")
updateResourceCmd.PersistentFlags().BoolVarP(&noBodyPrint, "silent", "s", false, "Don't print the body on success")
updateResourceCmd.Flags().StringVarP(&outputJq, "output-jq", "", "", "A jq expression, if set we will restrict output to only this")
updateResourceCmd.PersistentFlags().StringVarP(&ifAliasExists, "if-alias-exists", "", "", "If the alias exists we will run this command, otherwise exit with no error")
updateResourceCmd.PersistentFlags().StringVarP(&ifAliasDoesNotExist, "if-alias-does-not-exist", "", "", "If the alias does not exist we will run this command, otherwise exit with no error")
updateResourceCmd.MarkFlagsMutuallyExclusive("if-alias-exists", "if-alias-does-not-exist")
_ = updateResourceCmd.RegisterFlagCompletionFunc("output-jq", jqCompletionFunc)

update.AddCommand(updateResourceCmd)
updateCmd.AddCommand(updateResourceCmd)
}

parentCmd.AddCommand(update)
updateCmd.PersistentFlags().StringVar(&overrides.OverrideUrlPath, "override-url-path", "", "Override the URL that will be used for the Request")
updateCmd.PersistentFlags().StringSliceVarP(&overrides.QueryParameters, "query-parameters", "q", []string{}, "Pass in key=value an they will be added as query parameters")
updateCmd.PersistentFlags().BoolVarP(&noBodyPrint, "silent", "s", false, "Don't print the body on success")
updateCmd.PersistentFlags().StringVarP(&outputJq, "output-jq", "", "", "A jq expression, if set we will restrict output to only this")
updateCmd.PersistentFlags().StringVarP(&ifAliasExists, "if-alias-exists", "", "", "If the alias exists we will run this command, otherwise exit with no error")
updateCmd.PersistentFlags().StringVarP(&ifAliasDoesNotExist, "if-alias-does-not-exist", "", "", "If the alias does not exist we will run this command, otherwise exit with no error")
updateCmd.MarkFlagsMutuallyExclusive("if-alias-exists", "if-alias-does-not-exist")
_ = updateCmd.RegisterFlagCompletionFunc("output-jq", jqCompletionFunc)
parentCmd.AddCommand(updateCmd)

}

Expand Down

0 comments on commit 38bf518

Please sign in to comment.