Skip to content

Commit

Permalink
refactor: quickstarts flags (#169)
Browse files Browse the repository at this point in the history
* refactor: quickstarts flags

* Update internal/cli/quickstarts.go

Co-authored-by: Rita Zerrizuela <[email protected]>

* Update internal/cli/quickstarts.go

Co-authored-by: Rita Zerrizuela <[email protected]>

* Update internal/cli/quickstarts.go

Co-authored-by: Rita Zerrizuela <[email protected]>

* refactor: rename flags

* fix: quickstart stack is mandatory

Co-authored-by: Rita Zerrizuela <[email protected]>
  • Loading branch information
jfatta and Widcket authored Mar 19, 2021
1 parent b878bf2 commit 4005c63
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 55 deletions.
48 changes: 24 additions & 24 deletions internal/cli/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ auth0 apps delete <id>
}

func createAppCmd(cli *cli) *cobra.Command {
var flags struct {
var inputs struct {
Name string
Type string
Description string
Expand All @@ -263,35 +263,35 @@ auth0 apps create --name myapp --type [native|spa|regular|m2m]
prepareInteractivity(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := appName.Ask(cmd, &flags.Name); err != nil {
if err := appName.Ask(cmd, &inputs.Name); err != nil {
return err
}

if err := appType.Select(cmd, &flags.Type, appTypeOptions); err != nil {
if err := appType.Select(cmd, &inputs.Type, appTypeOptions); err != nil {
return err
}

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

a := &management.Client{
Name: &flags.Name,
Description: &flags.Description,
AppType: auth0.String(apiTypeFor(flags.Type)),
Callbacks: stringToInterfaceSlice(flags.Callbacks),
AllowedOrigins: stringToInterfaceSlice(flags.AllowedOrigins),
WebOrigins: stringToInterfaceSlice(flags.AllowedWebOrigins),
AllowedLogoutURLs: stringToInterfaceSlice(flags.AllowedLogoutURLs),
TokenEndpointAuthMethod: apiAuthMethodFor(flags.AuthMethod),
Name: &inputs.Name,
Description: &inputs.Description,
AppType: auth0.String(apiTypeFor(inputs.Type)),
Callbacks: stringToInterfaceSlice(inputs.Callbacks),
AllowedOrigins: stringToInterfaceSlice(inputs.AllowedOrigins),
WebOrigins: stringToInterfaceSlice(inputs.AllowedWebOrigins),
AllowedLogoutURLs: stringToInterfaceSlice(inputs.AllowedLogoutURLs),
TokenEndpointAuthMethod: apiAuthMethodFor(inputs.AuthMethod),
OIDCConformant: &oidcConformant,
JWTConfiguration: &management.ClientJWTConfiguration{Algorithm: &algorithm},
}

if len(flags.Grants) == 0 {
a.GrantTypes = apiDefaultGrantsFor(flags.Type)
if len(inputs.Grants) == 0 {
a.GrantTypes = apiDefaultGrantsFor(inputs.Type)
} else {
a.GrantTypes = apiGrantsFor(flags.Grants)
a.GrantTypes = apiGrantsFor(inputs.Grants)
}

err := ansi.Spinner("Creating application", func() error {
Expand All @@ -310,15 +310,15 @@ auth0 apps create --name myapp --type [native|spa|regular|m2m]
},
}

appName.RegisterString(cmd, &flags.Name, "")
appType.RegisterString(cmd, &flags.Type, "")
appDescription.RegisterString(cmd, &flags.Description, "")
appCallbacks.RegisterStringSlice(cmd, &flags.Callbacks, nil)
appOrigins.RegisterStringSlice(cmd, &flags.AllowedOrigins, nil)
appWebOrigins.RegisterStringSlice(cmd, &flags.AllowedWebOrigins, nil)
appLogoutURLs.RegisterStringSlice(cmd, &flags.AllowedLogoutURLs, nil)
appAuthMethod.RegisterString(cmd, &flags.AuthMethod, "")
appGrants.RegisterStringSlice(cmd, &flags.Grants, nil)
appName.RegisterString(cmd, &inputs.Name, "")
appType.RegisterString(cmd, &inputs.Type, "")
appDescription.RegisterString(cmd, &inputs.Description, "")
appCallbacks.RegisterStringSlice(cmd, &inputs.Callbacks, nil)
appOrigins.RegisterStringSlice(cmd, &inputs.AllowedOrigins, nil)
appWebOrigins.RegisterStringSlice(cmd, &inputs.AllowedWebOrigins, nil)
appLogoutURLs.RegisterStringSlice(cmd, &inputs.AllowedLogoutURLs, nil)
appAuthMethod.RegisterString(cmd, &inputs.AuthMethod, "")
appGrants.RegisterStringSlice(cmd, &inputs.Grants, nil)

return cmd
}
Expand Down
70 changes: 39 additions & 31 deletions internal/cli/quickstarts.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ import (
"gopkg.in/auth0.v5/management"
)

// QuickStart app types and defaults
const (
qsNative = "native"
qsSpa = "spa"
qsWebApp = "webapp"
qsBackend = "backend"
_defaultURL = "http://localhost:3000"
)

var (
//go:embed data/quickstarts.json
qsBuf []byte
Expand All @@ -31,15 +40,22 @@ var (
}
return
}()
)

// QuickStart app types and defaults
const (
qsNative = "native"
qsSpa = "spa"
qsWebApp = "webapp"
qsBackend = "backend"
_defaultURL = "http://localhost:3000"
qsClientID = Flag{
Name: "Client ID",
LongForm: "client-id",
ShortForm: "c",
Help: "Client Id of an Auth0 application.",
IsRequired: true,
}

qsStack = Flag{
Name: "Stack",
LongForm: "stack",
ShortForm: "s",
Help: "Tech/Language of the quickstart sample to download.",
IsRequired: true,
}
)

type quickstart struct {
Expand All @@ -64,16 +80,15 @@ func quickstartsCmd(cli *cli) *cobra.Command {
}

func downloadQuickstart(cli *cli) *cobra.Command {
var flags struct {
var inputs struct {
ClientID string
Type string
Stack string
}

cmd := &cobra.Command{
Use: "download",
Short: "Download a quickstart sample app for a specific tech stack",
Long: `auth0 quickstarts download --type <type> --client-id <client-id> --stack <stack>`,
Long: `auth0 quickstarts download --client-id <client-id> --stack <stack>`,
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
Expand All @@ -82,30 +97,24 @@ func downloadQuickstart(cli *cli) *cobra.Command {
return errors.New("This command can only be run on interactive mode")
}

selectedClientID := flags.ClientID

if selectedClientID == "" {
input := prompt.TextInput("client-id", "Client Id:", "Client Id of an Auth0 application.", true)
if err := prompt.AskOne(input, &selectedClientID); err != nil {
return fmt.Errorf("An unexpected error occurred: %v", err)
}
if err := qsClientID.Ask(cmd, &inputs.ClientID); err != nil {
return err
}

client, err := cli.api.Client.Read(selectedClientID)
client, err := cli.api.Client.Read(inputs.ClientID)
if err != nil {
return fmt.Errorf("An unexpected error occurred, please verify your client id: %v", err.Error())
}

selectedStack := flags.Stack

if selectedStack == "" {
if inputs.Stack == "" {
// get the valid types for this App:
stacks, err := quickstartStacksFromType(client.GetAppType())
if err != nil {
return fmt.Errorf("An unexpected error occurred: %v", err)
}
input := prompt.SelectInput("stack", "Stack:", "Tech/Language of the quickstart sample to download", stacks, true)
if err := prompt.AskOne(input, &selectedStack); err != nil {
return fmt.Errorf("An unexpected error occurred: %v", err)
// ask for input using the valid types only:
if err := qsStack.Select(cmd, &inputs.Stack, stacks); err != nil {
return err
}
}

Expand All @@ -120,13 +129,13 @@ func downloadQuickstart(cli *cli) *cobra.Command {
}
}

q, err := getQuickstart(client.GetAppType(), selectedStack)
q, err := getQuickstart(client.GetAppType(), inputs.Stack)
if err != nil {
return fmt.Errorf("An unexpected error occurred with the specified stack %v: %v", selectedStack, err)
return fmt.Errorf("An unexpected error occurred with the specified stack %v: %v", inputs.Stack, err)
}

err = ansi.Spinner("Downloading quickstart sample", func() error {
return downloadQuickStart(context.TODO(), cli, client, target, q)
return downloadQuickStart(cmd.Context(), cli, client, target, q)
})

if err != nil {
Expand All @@ -144,10 +153,9 @@ func downloadQuickstart(cli *cli) *cobra.Command {
}

cmd.SetUsageTemplate(resourceUsageTemplate())
cmd.Flags().StringVarP(&flags.ClientID, "client-id", "c", "", "Client Id of an Auth0 application.")
cmd.Flags().StringVarP(&flags.Stack, "stack", "s", "", "Tech/Language of the quickstart sample to download.")
mustRequireFlags(cmd, "client-id")

qsClientID.RegisterString(cmd, &inputs.ClientID, "")
qsStack.RegisterString(cmd, &inputs.Stack, "")
return cmd
}

Expand Down

0 comments on commit 4005c63

Please sign in to comment.