Skip to content

Commit

Permalink
feat: adds app open [id] command (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
as-herzog authored Apr 16, 2021
1 parent 08b0600 commit ba8e0d5
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions internal/cli/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth0"
"github.com/auth0/auth0-cli/internal/open"
"github.com/auth0/auth0-cli/internal/prompt"
"github.com/spf13/cobra"
"gopkg.in/auth0.v5/management"
Expand All @@ -21,6 +22,7 @@ const (
appTypeRegularWeb = "regular_web"
appTypeNonInteractive = "non_interactive"
appDefaultURL = "http://localhost:3000"
manageDomain = "https://manage.auth0.com"
)

var (
Expand Down Expand Up @@ -128,6 +130,7 @@ func appsCmd(cli *cli) *cobra.Command {
cmd.AddCommand(showAppCmd(cli))
cmd.AddCommand(updateAppCmd(cli))
cmd.AddCommand(deleteAppCmd(cli))
cmd.AddCommand(openAppCmd(cli))

return cmd
}
Expand Down Expand Up @@ -656,6 +659,67 @@ auth0 apps update <id> -n myapp --type [native|spa|regular|m2m]`,
return cmd
}

func openAppCmd(cli *cli) *cobra.Command {
var inputs struct {
ID string
}

cmd := &cobra.Command{
Use: "open",
Args: cobra.MaximumNArgs(1),
Short: "Open application settings page in Auth0 Manage",
Long: "Open application settings page in Auth0 Manage.",
Example: `auth0 apps open <id>`,
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
err := appID.Pick(cmd, &inputs.ID, cli.appPickerOptions)
if err != nil {
return err
}
} else {
inputs.ID = args[0]
}

manageUrl := formatManageURL(cli.config, inputs.ID)
if manageUrl == "" {
cli.renderer.Warnf("Unable to format the correct URL, please ensure you have run auth0 login and try again.")
return nil
}
if err := open.URL(manageUrl); err != nil {
cli.renderer.Warnf("Couldn't open the URL, please do it manually: %s.", manageUrl)
}
return nil
},
}

return cmd
}

func formatManageURL(cfg config, id string) string {
if cfg.DefaultTenant == "" || id == "" {
return ""
}
// ex: dev-tti06f6y.us.auth0.com
s := strings.Split(cfg.DefaultTenant, ".")
if len(s) < 4 {
return ""
}
region := s[len(s)-3]
tenant := cfg.Tenants[cfg.DefaultTenant].Name
if tenant == "" {
return ""
}
return fmt.Sprintf("%s/dashboard/%s/%s/applications/%s/settings",
manageDomain,
region,
tenant,
id,
)
}

func apiTypeFor(v string) string {
switch strings.ToLower(v) {
case "native":
Expand Down

0 comments on commit ba8e0d5

Please sign in to comment.