Skip to content

Commit

Permalink
commands => cli; refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cyx committed Sep 5, 2020
1 parent 60d8a9e commit ec6e2b0
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 81 deletions.
4 changes: 2 additions & 2 deletions cmd/auth0/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import "github.com/auth0/auth0-cli/internal/commands"
import "github.com/auth0/auth0-cli/internal/cli"

func main() {
commands.Execute()
cli.Execute()
}
98 changes: 89 additions & 9 deletions internal/commands/actions.go → internal/cli/actions.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package commands
package cli

import (
"fmt"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/config"
"github.com/auth0/auth0-cli/internal/validators"
"github.com/cyx/auth0/management"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -31,16 +34,22 @@ func listActionsCmd(cfg *config.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List existing actions",
Long: `List all actions within a tenant.`,
Long: `List actions within a specific trigger.`,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("list called")
list, err := cfg.API.Action.List(management.WithTriggerID(management.TriggerID(flags.trigger)))
if err != nil {
return err
}

cfg.Renderer.ActionList(list.Actions)
return nil
},
}

cmd.Flags().StringVarP(&flags.trigger,
"trigger", "t", "", "Only list actions within this trigger.",
)
mustRequireFlags(cmd, "trigger")

return cmd
}
Expand All @@ -52,21 +61,39 @@ func createActionCmd(cfg *config.Config) *cobra.Command {

cmd := &cobra.Command{
Use: "create <name>",
Args: validators.ExactArgs("<name>"),
Short: "Create an action.",
Long: `Creates an action, and generates a few files for working with actions:
- code.js - function signature.
- testdata.json - sample payload for testing the action.
`,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("create called")
return nil
// TODO(cyx): cache / list the set of triggers
// somewhere maybe? From there we can use them to
// determine what the valid triggers are.
action := &management.Action{
Name: args[0],
SupportedTriggers: []management.Trigger{
{
ID: management.TriggerID(flags.trigger),
Version: "v1",
},
},
}

return ansi.Spinner("Creating action", func() error {
return cfg.API.Action.Create(action)
})

// TODO: add some more help text here.
},
}

cmd.Flags().StringVarP(&flags.trigger,
"trigger", "t", "", "Supported trigger for the action.",
)
mustRequireFlags(cmd, "trigger")

return cmd
}
Expand All @@ -93,8 +120,13 @@ The deploy lifecycle is as follows:
}

func renameActionCmd(cfg *config.Config) *cobra.Command {
var flags struct {
newname string
}

cmd := &cobra.Command{
Use: "rename <name>",
Args: validators.ExactArgs("<name>"),
Short: "Rename an existing action.",
Long: `Renames an action. If any generated files are found those files are also renamed.:
Expand All @@ -104,11 +136,23 @@ The following generated files will be moved:
- testdata.json
`,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("rename called")
return nil
name := args[0]
action, err := findActionByName(cfg, name)
if err != nil {
return err
}

return ansi.Spinner("Renaming action", func() error {
return cfg.API.Action.Update(action.ID, &management.Action{Name: flags.newname})
})
},
}

cmd.Flags().StringVarP(&flags.newname,
"newname", "n", "", "New name of the action.",
)
mustRequireFlags(cmd, "newname")

return cmd
}

Expand All @@ -119,6 +163,7 @@ func deleteActionCmd(cfg *config.Config) *cobra.Command {

cmd := &cobra.Command{
Use: "delete <name>",
Args: validators.ExactArgs("<name>"),
Short: "Delete an existing action.",
Long: `Deletes an existing action. Only actions not bound to triggers can be deleted.
Expand All @@ -130,14 +175,49 @@ To delete an action already bound, you have to:
Note that all code artifacts will also be deleted.
`,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("delete called")
return nil
name := args[0]

if flags.confirm != args[0] {
return fmt.Errorf("Confirmation required. Try running `auth0 actions delete %s --confirm %s`", name, name)
}

action, err := findActionByName(cfg, name)
if err != nil {
return err
}

return ansi.Spinner("Deleting action", func() error {
return cfg.API.Action.Delete(action.ID)
})
},
}

cmd.Flags().StringVarP(&flags.confirm,
"confirm", "c", "", "Confirm the action name to be deleted.",
)
mustRequireFlags(cmd, "confirm")

return cmd
}

func findActionByName(cfg *config.Config, name string) (*management.Action, error) {
// TODO(cyx): add a WithName and a filter by name in
// the management API. For now we're gonna use
// post-login since that's all we're creating to test
// it out.
list, err := cfg.API.Action.List(management.WithTriggerID(management.TriggerID("post-login")))
if err != nil {
return nil, err
}

// Temporary shim: when we have a list by name, we'll
// just straight check the count and ensure it's 1
// then.
for _, a := range list.Actions {
if a.Name == name {
return a, nil
}
}

return nil, fmt.Errorf("Action with name `%s` not found.", name)
}
11 changes: 11 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cli

import "github.com/spf13/cobra"

func mustRequireFlags(cmd *cobra.Command, flags ...string) {
for _, f := range flags {
if err := cmd.MarkFlagRequired(f); err != nil {
panic(err)
}
}
}
53 changes: 53 additions & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cli

import (
"fmt"
"os"

"github.com/auth0/auth0-cli/internal/config"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)

// Execute is the primary entrypoint of the CLI app.
func Execute() {
// fs is a mock friendly os.File interface.
fs := afero.NewOsFs()

// cfg contains tenant related information, e.g. `travel0-dev`,
// `travel0-prod`. some of its information can be sourced via:
// 1. env var (e.g. AUTH0_API_KEY)
// 2. global flag (e.g. --api-key)
// 3. JSON file (e.g. api_key = "..." in ~/.config/auth0/config.json)
cfg := &config.Config{}

rootCmd := &cobra.Command{
Use: "auth0",
SilenceUsage: true,
SilenceErrors: true,
Short: "Command-line tool to interact with Auth0.",
Long: "Command-line tool to interact with Auth0.\n" + getLogin(&fs, cfg),

PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// Initialize everything once. Later callers can then
// freely assume that config is fully primed and ready
// to go.
return cfg.Init()
},
}

rootCmd.SetUsageTemplate(namespaceUsageTemplate())
rootCmd.PersistentFlags().StringVar(&cfg.Tenant,
"tenant", "", "Specific tenant to use")

rootCmd.PersistentFlags().BoolVar(&cfg.Verbose,
"verbose", false, "Enable verbose mode.")

rootCmd.AddCommand(actionsCmd(cfg))
rootCmd.AddCommand(triggersCmd(cfg))

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
15 changes: 7 additions & 8 deletions internal/commands/templates.go → internal/cli/templates.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package commands
package cli

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/auth0/auth0-cli/internal/ansi"
Expand Down Expand Up @@ -77,13 +75,14 @@ func WrappedNonRequestParamsFlagUsages(cmd *cobra.Command) string {
//

func getLogin(fs *afero.Fs, cfg *config.Config) string {
// We're checking against the path because we don't initialize the config
// at this point of execution.
path := cfg.GetConfigFolder(os.Getenv("XDG_CONFIG_HOME"))
file := filepath.Join(path, "config.toml")
// // We're checking against the path because we don't initialize the config
// // at this point of execution.
// path := cfg.GetConfigFolder(os.Getenv("XDG_CONFIG_HOME"))
// file := filepath.Join(path, "config.toml")

exists, _ := afero.Exists(*fs, file)
// exists, _ := afero.Exists(*fs, file)

exists := false
if !exists {
return `
Before using the CLI, you'll need to login:
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/triggers.go → internal/cli/triggers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package commands
package cli

import (
"fmt"
Expand Down
61 changes: 0 additions & 61 deletions internal/commands/root.go

This file was deleted.

0 comments on commit ec6e2b0

Please sign in to comment.