Skip to content

Commit

Permalink
Merge pull request #234 from danielgtaylor/clear-auth-cache
Browse files Browse the repository at this point in the history
feat: add command to clear auth token cache
  • Loading branch information
danielgtaylor committed Dec 16, 2023
2 parents 21a9017 + 97c3636 commit d16bdd7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cli/apiconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,27 @@ func initAPIConfig() {
Run: func(cmd *cobra.Command, args []string) { editAPIs(os.Exit) },
})

apiCommand.AddCommand(&cobra.Command{
Use: "clear-auth-cache short-name",
Short: "Clear API auth token cache",
Long: "Clear the API auth token cache for the current profile. This will force a re-authentication the next time you make a request.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
apiName := args[0]
api := configs[apiName]
if api == nil {
panic("API " + apiName + " not found")
}

// Remove the cache entry.
Cache.Set(apiName+":"+viper.GetString("rsh-profile"), "")

if err := Cache.WriteConfig(); err != nil {
panic(fmt.Errorf("Unable to write cache file: %w", err))
}
},
})

apiCommand.AddCommand(&cobra.Command{
Use: "show short-name",
Short: "Show API config",
Expand Down
37 changes: 37 additions & 0 deletions cli/apiconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,43 @@ func TestAPIShow(t *testing.T) {
assert.Equal(t, captured, "\x1b[38;5;247m{\x1b[0m\n \x1b[38;5;74m\"base\"\x1b[0m\x1b[38;5;247m:\x1b[0m \x1b[38;5;150m\"https://api.example.com\"\x1b[0m\n\x1b[38;5;247m}\x1b[0m\n")
}

func TestAPIClearCache(t *testing.T) {
reset(false)

configs["test"] = &APIConfig{
name: "test",
Base: "https://api.example.com",
}
Cache.Set("test:default.token", "abc123")

runNoReset("api clear-auth-cache test")

assert.Equal(t, "", Cache.GetString("test:default.token"))
}

func TestAPIClearCacheProfile(t *testing.T) {
reset(false)

configs["test"] = &APIConfig{
name: "test",
Base: "https://api.example.com",
}
Cache.Set("test:default.token", "abc123")
Cache.Set("test:other.token", "def456")

runNoReset("api clear-auth-cache test -p other")

assert.Equal(t, "abc123", Cache.GetString("test:default.token"))
assert.Equal(t, "", Cache.GetString("test:other.token"))
}

func TestAPIClearCacheMissing(t *testing.T) {
reset(false)

captured := runNoReset("api clear-auth-cache missing-api")
assert.Contains(t, captured, "API missing-api not found")
}

func TestEditAPIsMissingEditor(t *testing.T) {
os.Setenv("EDITOR", "")
os.Setenv("VISUAL", "")
Expand Down

0 comments on commit d16bdd7

Please sign in to comment.