Skip to content

Commit

Permalink
add logout command
Browse files Browse the repository at this point in the history
Context: we'll want to give our customers the ability to clear out their
sessions and prune out any access tokens.
  • Loading branch information
cyx committed Mar 6, 2021
1 parent c9d1f92 commit 57b23b4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
32 changes: 32 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 57b23b4

Please sign in to comment.