-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Caching the way I should have from the beginning (#11)
* Caching the way I should have from the beginning
- Loading branch information
1 parent
b4dbbbf
commit 8c3e1a3
Showing
11 changed files
with
527 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
kind: Added | ||
body: Caching, the way it should have been from the beginning. | ||
time: 2024-07-12T11:59:26.503843-05:00 | ||
custom: | ||
Author: Shackelford-Arden |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/Shackelford-Arden/hctx/cache" | ||
"github.com/Shackelford-Arden/hctx/models" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// CleanCache checks all items in the cache file | ||
// and removes any that have expired or are not valid, | ||
// including stacks that are no longer in the config. | ||
func CleanCache(ctx *cli.Context) error { | ||
|
||
newCache := cache.Cache{} | ||
currentCache, err := AppCache.Get() | ||
if err != nil { | ||
return fmt.Errorf("failed to get current cache: %s", err) | ||
} | ||
|
||
for stack, stackCache := range currentCache { | ||
|
||
// Check if cached stack is in config | ||
configStack := AppConfig.GetStack(stack) | ||
if configStack == nil { | ||
continue | ||
} | ||
|
||
cleanCache := models.StackCache{ | ||
NomadToken: stackCache.NomadToken, | ||
ConsulToken: stackCache.ConsulToken, | ||
} | ||
|
||
// Validate if tokens have expired. | ||
if stackCache.NomadToken != "" && configStack.Nomad != nil { | ||
nomToken := validNomadToken(configStack.Nomad.Address, stackCache.NomadToken) | ||
if !nomToken { | ||
// Remove expired token from cache | ||
cleanCache.NomadToken = "" | ||
} | ||
} | ||
|
||
if stackCache.ConsulToken != "" && configStack.Consul != nil { | ||
conToken := validNomadToken(configStack.Consul.Address, stackCache.ConsulToken) | ||
if !conToken { | ||
// Remove expired token from cache | ||
cleanCache.ConsulToken = "" | ||
} | ||
} | ||
|
||
newCache[stack] = cleanCache | ||
} | ||
|
||
AppCache = &newCache | ||
saveErr := AppCache.Save("") | ||
if saveErr != nil { | ||
return fmt.Errorf("failed to save cache: %s", saveErr) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ClearCache removes all items from the cache file. | ||
func ClearCache(ctx *cli.Context) error { | ||
|
||
AppCache = &cache.Cache{} | ||
saveErr := AppCache.Save("") | ||
if saveErr != nil { | ||
return fmt.Errorf("failed to save cleared cache: %s", saveErr) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ShowCache shows the content of the cache file. | ||
func ShowCache(ctx *cli.Context) error { | ||
|
||
cache, _ := AppCache.Get() | ||
fmtCache, fmtErr := json.MarshalIndent(cache, "", " ") | ||
if fmtErr != nil { | ||
return fmt.Errorf("failed to read/format the cache: %s", fmtErr.Error()) | ||
} | ||
|
||
fmt.Println(string(fmtCache)) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.