diff --git a/internal/cli/root.go b/internal/cli/root.go index d71c3caea..7c288ce88 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -84,49 +84,19 @@ func buildRootCmd(cli *cli) *cobra.Command { ansi.Initialize(cli.noColor) prepareInteractivity(cmd) - // If the user is trying to login, no need to go - // through setup. - if cmd.Use == "login" && cmd.Parent().Use == "auth0" { + if !commandRequiresAuthentication(cmd.CommandPath()) { return nil } - // We're tracking the login command in its Run method - // so we'll only add this defer if the command is not login + // We're tracking the login command in its Run method, so + // we'll only add this defer if the command is not login. defer func() { - if cli.tracker != nil && cli.isLoggedIn() { + if cli.tracker != nil && cmd.Name() != "login" && cli.isLoggedIn() { cli.tracker.TrackCommandRun(cmd, cli.config.InstallID) } }() - // If the user is trying to logout, session information - // isn't important as well. - if cmd.Use == "logout" && cmd.Parent().Use == "auth0" { - return nil - } - - // Selecting tenants shouldn't really trigger a login. - if cmd.Parent().Use == "tenants" && (cmd.Use == "use" || cmd.Use == "add") { - return nil - } - - // Getting the CLI completion script shouldn't trigger a login. - if cmd.Use == "completion" && cmd.Parent().Use == "auth0" { - return nil - } - - // Getting help shouldn't trigger a login. - if cmd.CalledAs() == "help" && cmd.Parent().Use == "auth0" { - return nil - } - - // config init shouldn't trigger a login. - if cmd.CalledAs() == "init" && cmd.Parent().Use == "config" { - return nil - } - - // Initialize everything once. Later callers can then - // freely assume that config is fully primed and ready - // to go. + // Initialize everything once. return cli.setup(cmd.Context()) }, } @@ -134,6 +104,25 @@ func buildRootCmd(cli *cli) *cobra.Command { return rootCmd } +func commandRequiresAuthentication(invokedCommandName string) bool { + commandsWithNoAuthRequired := []string{ + "auth0 completion", + "auth0 help", + "auth0 login", + "auth0 logout", + "auth0 tenants use", + "auth0 tenants list", + } + + for _, cmd := range commandsWithNoAuthRequired { + if cmd == invokedCommandName { + return false + } + } + + return true +} + func addPersistentFlags(rootCmd *cobra.Command, cli *cli) { rootCmd.PersistentFlags().StringVar(&cli.tenant, "tenant", cli.config.DefaultTenant, "Specific tenant to use.") diff --git a/internal/cli/root_test.go b/internal/cli/root_test.go new file mode 100644 index 000000000..c47202b4a --- /dev/null +++ b/internal/cli/root_test.go @@ -0,0 +1,35 @@ +package cli + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCommandRequiresAuthentication(t *testing.T) { + var testCases = []struct { + givenCommand string + expectedToRequireAuthentication bool + }{ + {"auth0 user list", true}, + {"auth0 user create", true}, + {"auth0 api", true}, + {"auth0 apps list", true}, + {"auth0 apps create", true}, + {"auth0 orgs members list", true}, + {"auth0 completion", false}, + {"auth0 help", false}, + {"auth0 login", false}, + {"auth0 logout", false}, + {"auth0 tenants use", false}, + {"auth0 tenants list", false}, + } + + for index, testCase := range testCases { + t.Run(fmt.Sprintf("TestCase #%d Command: %s", index, testCase.givenCommand), func(t *testing.T) { + actualAuth := commandRequiresAuthentication(testCase.givenCommand) + assert.Equal(t, testCase.expectedToRequireAuthentication, actualAuth) + }) + } +}