Skip to content

Commit

Permalink
Merge pull request #165 from auth0/refactor-flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Widcket authored Mar 15, 2021
2 parents eeeee50 + 58b4623 commit 9c144d3
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 159 deletions.
110 changes: 56 additions & 54 deletions internal/cli/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cli
import (
"errors"
"fmt"
"strings"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/prompt"
Expand All @@ -12,26 +11,30 @@ import (
)

const (
apiID = "id"
apiScopes = "scopes"
apiID = "id"
)

var (
apiName = Flag{
Name: "Name",
LongForm: "name",
ShortForm: "n",
DefaultValue: "",
Help: "Name of the API.",
IsRequired: true,
Name: "Name",
LongForm: "name",
ShortForm: "n",
Help: "Name of the API.",
IsRequired: true,
}
apiIdentifier = Flag{
Name: "Identifier",
LongForm: "identifier",
ShortForm: "i",
DefaultValue: "",
Help: "Identifier of the API. Cannot be changed once set.",
IsRequired: true,
Name: "Identifier",
LongForm: "identifier",
ShortForm: "i",
Help: "Identifier of the API. Cannot be changed once set.",
IsRequired: true,
}
apiScopes = Flag{
Name: "Scopes",
LongForm: "scopes",
ShortForm: "s",
Help: "Comma-separated list of scopes.",
IsRequired: true,
}
)

Expand Down Expand Up @@ -135,7 +138,7 @@ auth0 apis show <id>
})

if err != nil {
return fmt.Errorf("Unable to get an API with Id %s: %w", inputs.ID, err)
return fmt.Errorf("Unable to get an API with Id '%s': %w", inputs.ID, err)
}

cli.renderer.ApiShow(api)
Expand All @@ -147,10 +150,11 @@ auth0 apis show <id>
}

func createApiCmd(cli *cli) *cobra.Command {
var flags struct {
Name string
Identifier string
Scopes string
var inputs struct {
Name string
Identifier string
Scopes []string
ScopesString string
}

cmd := &cobra.Command{
Expand All @@ -164,56 +168,55 @@ auth0 apis create --name myapi --identifier http://my-api
prepareInteractivity(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := apiName.Ask(cmd, &flags.Name); err != nil {
if err := apiName.Ask(cmd, &inputs.Name); err != nil {
return err
}

if err := apiIdentifier.Ask(cmd, &flags.Identifier); err != nil {
if err := apiIdentifier.Ask(cmd, &inputs.Identifier); err != nil {
return err
}

if shouldPrompt(cmd, apiScopes) {
input := prompt.TextInput(apiScopes, "Scopes:", "Space-separated list of scopes.", false)

if err := prompt.AskOne(input, &flags); err != nil {
return fmt.Errorf("An unexpected error occurred: %w", err)
}
if err := apiScopes.Ask(cmd, &inputs.ScopesString); err != nil {
return err
}

api := &management.ResourceServer{
Name: &flags.Name,
Identifier: &flags.Identifier,
Name: &inputs.Name,
Identifier: &inputs.Identifier,
}

if flags.Scopes != "" {
api.Scopes = apiScopesFor(flags.Scopes)
if len(inputs.ScopesString) > 0 {
api.Scopes = apiScopesFor(commaSeparatedStringToSlice(inputs.ScopesString))
} else if len(inputs.Scopes) > 0 {
api.Scopes = apiScopesFor(inputs.Scopes)
}

err := ansi.Spinner("Creating API", func() error {
return cli.api.ResourceServer.Create(api)
})

if err != nil {
return fmt.Errorf("An unexpected error occurred while attempting to create an API with name %s and identifier %s : %w", flags.Name, flags.Identifier, err)
return fmt.Errorf("An unexpected error occurred while attempting to create an API with name '%s' and identifier '%s': %w", inputs.Name, inputs.Identifier, err)
}

cli.renderer.ApiCreate(api)
return nil
},
}

apiName.RegisterString(cmd, &flags.Name)
apiIdentifier.RegisterString(cmd, &flags.Identifier)
cmd.Flags().StringVarP(&flags.Scopes, apiScopes, "s", "", "Space-separated list of scopes.")
apiName.RegisterString(cmd, &inputs.Name, "")
apiIdentifier.RegisterString(cmd, &inputs.Identifier, "")
apiScopes.RegisterStringSlice(cmd, &inputs.Scopes, nil)

return cmd
}

func updateApiCmd(cli *cli) *cobra.Command {
var inputs struct {
ID string
Name string
Scopes string
ID string
Name string
Scopes []string
ScopesString string
}

cmd := &cobra.Command{
Expand Down Expand Up @@ -246,12 +249,8 @@ auth0 apis update <id> --name myapi
return err
}

if shouldPromptWhenFlagless(cmd, apiScopes) {
input := prompt.TextInput(apiScopes, "Scopes:", "Space-separated list of scopes.", false)

if err := prompt.AskOne(input, &inputs); err != nil {
return fmt.Errorf("An unexpected error occurred: %w", err)
}
if err := apiScopes.AskU(cmd, &inputs.ScopesString); err != nil {
return err
}

api := &management.ResourceServer{}
Expand All @@ -270,7 +269,11 @@ auth0 apis update <id> --name myapi
}

if len(inputs.Scopes) == 0 {
api.Scopes = current.Scopes
if len(inputs.ScopesString) == 0 {
api.Scopes = current.Scopes
} else {
api.Scopes = apiScopesFor(commaSeparatedStringToSlice(inputs.ScopesString))
}
} else {
api.Scopes = apiScopesFor(inputs.Scopes)
}
Expand All @@ -279,16 +282,16 @@ auth0 apis update <id> --name myapi
})

if err != nil {
return fmt.Errorf("An unexpected error occurred while trying to update an API with Id %s: %w", inputs.ID, err)
return fmt.Errorf("An unexpected error occurred while trying to update an API with Id '%s': %w", inputs.ID, err)
}

cli.renderer.ApiUpdate(api)
return nil
},
}

apiName.RegisterStringU(cmd, &inputs.Name)
cmd.Flags().StringVarP(&inputs.Scopes, apiScopes, "s", "", "Space-separated list of scopes.")
apiName.RegisterStringU(cmd, &inputs.Name, "")
apiScopes.RegisterStringSliceU(cmd, &inputs.Scopes, nil)

return cmd
}
Expand Down Expand Up @@ -333,7 +336,7 @@ auth0 apis delete <id>
return ansi.Spinner("Deleting API", func() error {
err := cli.api.ResourceServer.Delete(inputs.ID)
if err != nil {
return fmt.Errorf("An unexpected error occurred while attempting to delete an API with Id %s: %w", inputs.ID, err)
return fmt.Errorf("An unexpected error occurred while attempting to delete an API with Id '%s': %w", inputs.ID, err)
}
return nil
})
Expand Down Expand Up @@ -383,7 +386,7 @@ auth0 apis scopes list <id>
})

if err != nil {
return fmt.Errorf("An unexpected error occurred while getting scopes for an API with Id %s: %w", inputs.ID, err)
return fmt.Errorf("An unexpected error occurred while getting scopes for an API with Id '%s': %w", inputs.ID, err)
}

cli.renderer.ScopesList(api.GetName(), api.Scopes)
Expand All @@ -394,11 +397,10 @@ auth0 apis scopes list <id>
return cmd
}

func apiScopesFor(scopes string) []*management.ResourceServerScope {
list := strings.Fields(scopes)
func apiScopesFor(scopes []string) []*management.ResourceServerScope {
models := []*management.ResourceServerScope{}

for _, scope := range list {
for _, scope := range scopes {
value := scope
models = append(models, &management.ResourceServerScope{Value: &value})
}
Expand Down
Loading

0 comments on commit 9c144d3

Please sign in to comment.