diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 5705fcefb..1f3305813 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -229,6 +229,38 @@ func (c *cli) addTenant(ten tenant) error { return nil } +func (c *cli) removeTenant(ten string) error { + // init will fail here with a `no tenant found` error if we're logging + // in for the first time and that's expected. + _ = c.init() + + // If we're dealing with an empty file, we'll need to initialize this + // map. + if c.config.Tenants == nil { + c.config.Tenants = map[string]tenant{} + } + + // If the default tenant is being removed, we'll pick the first tenant + // that's not the one being removed, and make that the new default. + if c.config.DefaultTenant == ten { + Loop: + for t := range c.config.Tenants { + if t != ten { + c.config.DefaultTenant = t + break Loop + } + } + } + + delete(c.config.Tenants, ten) + + if err := c.persistConfig(); err != nil { + return fmt.Errorf("persisting config: %w", err) + } + + return nil +} + func (c *cli) persistConfig() error { dir := filepath.Dir(c.path) if _, err := os.Stat(dir); os.IsNotExist(err) { diff --git a/internal/cli/root.go b/internal/cli/root.go index e18bceb75..9c3292e17 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -34,6 +34,12 @@ func Execute() { return nil } + // 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.Use == "use" && cmd.Parent().Use == "tenants" { return nil @@ -72,6 +78,7 @@ func Execute() { rootCmd.AddCommand(testCmd(cli)) rootCmd.AddCommand(logsCmd(cli)) rootCmd.AddCommand(actionsCmd(cli)) + rootCmd.AddCommand(logoutCmd(cli)) // keep completion at the bottom: rootCmd.AddCommand(completionCmd(cli))