diff --git a/.changes/unreleased/Fixed-20240610-123340.yaml b/.changes/unreleased/Fixed-20240610-123340.yaml new file mode 100644 index 0000000..056ab52 --- /dev/null +++ b/.changes/unreleased/Fixed-20240610-123340.yaml @@ -0,0 +1,5 @@ +kind: Fixed +body: Fixed bug where cache setting was not actually being checked +time: 2024-06-10T12:33:40.797971-05:00 +custom: + Author: Shackelford-Arden diff --git a/cmd/unset.go b/cmd/unset.go index e4ab127..2b74cf1 100644 --- a/cmd/unset.go +++ b/cmd/unset.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "github.com/Shackelford-Arden/hctx/cache" "github.com/urfave/cli/v2" ) @@ -16,10 +17,12 @@ func Unset(ctx *cli.Context) error { } // Get current stacks tokens, if any and cache them - toCache := cache.GetCacheableValues() - updateErr := AppCache.Update(currentStack.Name, toCache) - if updateErr != nil { - return fmt.Errorf("could not update cache for stack %s: %v", currentStack.Name, updateErr) + if AppConfig.CacheAuth { + toCache := cache.GetCacheableValues() + updateErr := AppCache.Update(currentStack.Name, toCache) + if updateErr != nil { + return fmt.Errorf("could not update cache for stack %s: %v", currentStack.Name, updateErr) + } } fmt.Println(currentStack.Unset(AppConfig.Shell)) diff --git a/cmd/use.go b/cmd/use.go index e5ac550..5fa9158 100644 --- a/cmd/use.go +++ b/cmd/use.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "github.com/Shackelford-Arden/hctx/cache" "github.com/urfave/cli/v2" ) @@ -18,7 +19,8 @@ func Use(ctx *cli.Context) error { currentStack := AppConfig.GetCurrentStack() // Get current stacks tokens, if any and cache them - if currentStack != nil { + fmt.Printf("Value of cache_auth: %v", AppConfig.CacheAuth) + if currentStack != nil && AppConfig.CacheAuth { toCache := cache.GetCacheableValues() updateErr := AppCache.Update(currentStack.Name, toCache) if updateErr != nil { @@ -29,7 +31,7 @@ func Use(ctx *cli.Context) error { // rehydrate env w/ new stack cache, if present newStackCache := AppCache.Get(selectedStack.Name) - fmt.Print(selectedStack.Use(AppConfig.Shell, newStackCache)) + fmt.Print(selectedStack.Use(AppConfig.Shell, newStackCache, AppConfig.CacheAuth)) return nil } diff --git a/config/config.go b/config/config.go index 0f48597..4956d47 100644 --- a/config/config.go +++ b/config/config.go @@ -2,11 +2,12 @@ package config import ( "fmt" + "os" + "strings" + "github.com/Shackelford-Arden/hctx/models" "github.com/Shackelford-Arden/hctx/types" "github.com/hashicorp/hcl/v2/hclsimple" - "os" - "strings" ) const ConfigParentDir = ".config" @@ -109,7 +110,7 @@ type Stack struct { } // Use provides commands to set appropriate environment variables -func (s *Stack) Use(shell string, cache *models.StackCache) string { +func (s *Stack) Use(shell string, cache *models.StackCache, useCache bool) string { // Include Stack Name as an environment variable // Allow the Alias name to show in the environment variable stackName := s.Name @@ -120,7 +121,7 @@ func (s *Stack) Use(shell string, cache *models.StackCache) string { var nomadToken string var consulToken string - if cache != nil { + if cache != nil && useCache { nomadToken = cache.NomadToken consulToken = cache.ConsulToken } diff --git a/config/config_test.go b/config/config_test.go index 57a8e7e..dc9940e 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -2,9 +2,10 @@ package config import ( "fmt" + "testing" + "github.com/Shackelford-Arden/hctx/models" "github.com/Shackelford-Arden/hctx/types" - "testing" ) // region Bash/ZSH @@ -17,7 +18,7 @@ func TestNewConfigUseFullNomad(t *testing.T) { } stackName := "just_nomad" - useOut := cfg.GetStack(stackName).Use("bash", nil) + useOut := cfg.GetStack(stackName).Use("bash", nil, false) expectedOutput := fmt.Sprintf(` export %s='%s' @@ -38,7 +39,7 @@ func TestNewConfigUseFullConsul(t *testing.T) { t.Fatalf("failed to read config: %s", err) } stackName := "just_consul" - useOut := cfg.GetStack(stackName).Use("bash", nil) + useOut := cfg.GetStack(stackName).Use("bash", nil, false) expectedOutput := fmt.Sprintf(` export %s='%s' @@ -60,7 +61,7 @@ func TestNewConfigUseFullVault(t *testing.T) { } stackName := "just_vault" - useOut := cfg.GetStack(stackName).Use("bash", nil) + useOut := cfg.GetStack(stackName).Use("bash", nil, false) expectedOutput := fmt.Sprintf(` export %s='%s'