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

refactor: quickstarts flags #169

Merged
merged 7 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
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
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
69 changes: 38 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,21 @@ var (
}
return
}()
)

// QuickStart app types and defaults
const (
qsNative = "native"
qsSpa = "spa"
qsWebApp = "webapp"
qsBackend = "backend"
_defaultURL = "http://localhost:3000"
clientID = Flag{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please prefix it with qs (qsClientID), until we move it to its own package?

Name: "ClientID",
jfatta marked this conversation as resolved.
Show resolved Hide resolved
LongForm: "client ID",
jfatta marked this conversation as resolved.
Show resolved Hide resolved
ShortForm: "c",
Help: "Client Id of an Auth0 application.",
IsRequired: true,
}

stack = Flag{
Copy link
Contributor

@Widcket Widcket Mar 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem above (qsStack).

Name: "Stack",
LongForm: "stack",
ShortForm: "s",
Help: "Tech/Language of the quickstart sample to download",
jfatta marked this conversation as resolved.
Show resolved Hide resolved
}
)

type quickstart struct {
Expand All @@ -64,16 +79,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 +96,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 := clientID.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 := stack.Select(cmd, &inputs.Stack, stacks); err != nil {
return err
}
}

Expand All @@ -120,13 +128,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 +152,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")

clientID.RegisterString(cmd, &inputs.ClientID, "")
stack.RegisterString(cmd, &inputs.Stack, "")
return cmd
}

Expand Down