From 4bbbb553df9bc07f866b22e7803453d14070f502 Mon Sep 17 00:00:00 2001 From: Evsyukov Denis Date: Tue, 3 Sep 2024 14:33:27 +0300 Subject: [PATCH] feat: new flag options for cache --- cmd/gobrew/main.go | 79 +++++++++++++++++++++++++++----------- cmd/gobrew/main_unix.go | 2 +- cmd/gobrew/main_windows.go | 2 +- go.mod | 1 + go.sum | 2 + gobrew.go | 14 +++++++ helpers.go | 17 +++++--- 7 files changed, 88 insertions(+), 29 deletions(-) diff --git a/cmd/gobrew/main.go b/cmd/gobrew/main.go index 5046f0f..3a84298 100644 --- a/cmd/gobrew/main.go +++ b/cmd/gobrew/main.go @@ -1,11 +1,15 @@ package main import ( - "flag" + "fmt" "log" "os" + "regexp" "strconv" "strings" + "time" + + flag "github.com/spf13/pflag" "github.com/kevincobain2000/gobrew" "github.com/kevincobain2000/gobrew/utils" @@ -16,6 +20,11 @@ var actionArg = "" var versionArg = "" var version = "dev" +var help bool +var clearCache bool +var ttl time.Duration +var disableCache bool + var allowedArgs = []string{ "h", "help", @@ -37,11 +46,25 @@ func init() { if !isArgAllowed() { log.Println("[Info] Invalid usage") - log.Print(usage()) + flag.Usage() return } + flag.BoolVarP(&disableCache, "disable-cache", "d", false, "disable local cache") + flag.BoolVarP(&clearCache, "clear-cache", "c", false, "clear local cache") + flag.DurationVarP(&ttl, "ttl", "t", 20*time.Minute, "set cache duration in minutes") + + flag.BoolVarP(&help, "help", "h", false, "show usage message") + + flag.Usage = Usage + flag.Parse() + + if help { + Usage() + return + } + args = flag.Args() if len(args) == 0 { actionArg = "interactive" @@ -92,6 +115,9 @@ func main() { GobrewDownloadURL: gobrew.DownloadURL, GobrewTags: gobrew.TagsAPI, GobrewVersionsURL: gobrew.VersionsURL, + TTL: ttl, + DisableCache: disableCache, + ClearCache: clearCache, } gb := gobrew.NewGoBrew(config) @@ -101,7 +127,7 @@ func main() { case "noninteractive": gb.Interactive(false) case "h", "help": - log.Print(usage()) + flag.Usage() case "ls", "list": gb.ListVersions() case "ls-remote": @@ -124,17 +150,22 @@ func main() { } } +// isArgAllowed checks if the arg is allowed +// but ignored flags +// +// if the any arg is not allowed, it will return false func isArgAllowed() bool { ok := true - if len(os.Args) > 1 { - _, ok = Find(allowedArgs, os.Args[1]) - if !ok { - return false + for i := range os.Args { + if i == 0 { + continue + } + arg := os.Args[i] + if strings.HasPrefix(arg, "-") { + continue } - } - if len(os.Args) > 2 { - _, ok = Find(allowedArgs, os.Args[1]) + ok = Find(allowedArgs, arg) if !ok { return false } @@ -145,22 +176,21 @@ func isArgAllowed() bool { // Find takes a slice and looks for an element in it. If found it will // return it's key, otherwise it will return -1 and a bool of false. -func Find(slice []string, val string) (int, bool) { - for i, item := range slice { +func Find(slice []string, val string) bool { + r := regexp.MustCompile("([0-9]+).([0-9]+)*|beta.*|rc.*|latest") + if matches := r.FindString(val); matches != "" { + return true + } + + for _, item := range slice { if item == val { - return i, true + return true } } - return -1, false + return false } -func usage() string { - usageMsg := - ` -# Add gobrew to your ~/.bashrc or ~/.zshrc -export PATH="$HOME/.gobrew/current/bin:$HOME/.gobrew/bin:$PATH" -export GOROOT="$HOME/.gobrew/current/go" -` +var Usage = func() { msg := ` gobrew ` + version + ` @@ -178,6 +208,11 @@ Usage: gobrew version Show gobrew version gobrew help Show this message +Options: + gobrew [--clear-cache | -c] clear gobrew cache + gobrew [--disable-cache | -d] disable gobrew cache + gobrew [--ttl=20m | -t 20m] set gobrew cache ttl, default 20m + Examples: gobrew use 1.16 # use go version 1.16 gobrew use 1.16.1 # use go version 1.16.1 @@ -195,5 +230,5 @@ Examples: Installation Path: ` + usageMsg - return msg + fmt.Fprintf(os.Stderr, "%s\n", msg) } diff --git a/cmd/gobrew/main_unix.go b/cmd/gobrew/main_unix.go index 311dbd2..fa0816f 100644 --- a/cmd/gobrew/main_unix.go +++ b/cmd/gobrew/main_unix.go @@ -2,7 +2,7 @@ package main -const usageMsg = ` +const usageMsg string = ` # Add gobrew to your ~/.bashrc or ~/.zshrc export PATH="$HOME/.gobrew/current/bin:$HOME/.gobrew/bin:$PATH" export GOROOT="$HOME/.gobrew/current/go" diff --git a/cmd/gobrew/main_windows.go b/cmd/gobrew/main_windows.go index 52bdcda..e783409 100644 --- a/cmd/gobrew/main_windows.go +++ b/cmd/gobrew/main_windows.go @@ -2,7 +2,7 @@ package main -const usageMsg = ` +const usageMsg string = ` # Add gobrew to your environment variables PATH="%USERPROFILE%\.gobrew\current\bin;%USERPROFILE%\.gobrew\bin;%PATH%" GOROOT="%USERPROFILE%\.gobrew\current\go" diff --git a/go.mod b/go.mod index e5eb0d7..0611317 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.4.7 // indirect + github.com/spf13/pflag v1.0.5 // indirect github.com/ulikunitz/xz v0.5.11 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect golang.org/x/sys v0.17.0 // indirect diff --git a/go.sum b/go.sum index 6c14b31..fd0a1c3 100644 --- a/go.sum +++ b/go.sum @@ -33,6 +33,8 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/schollz/progressbar/v3 v3.14.1 h1:VD+MJPCr4s3wdhTc7OEJ/Z3dAeBzJ7yKH/P4lC5yRTI= github.com/schollz/progressbar/v3 v3.14.1/go.mod h1:Zc9xXneTzWXF81TGoqL71u0sBPjULtEHYtj/WVgVy8E= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= diff --git a/gobrew.go b/gobrew.go index 43e9b90..9bddf47 100644 --- a/gobrew.go +++ b/gobrew.go @@ -11,6 +11,7 @@ import ( "sort" "strconv" "strings" + "time" "github.com/Masterminds/semver" "github.com/gookit/color" @@ -56,6 +57,7 @@ type GoBrew struct { currentBinDir string currentGoDir string downloadsDir string + cacheFile string Config } @@ -65,11 +67,18 @@ type Config struct { GobrewDownloadURL string GobrewTags string GobrewVersionsURL string + + // cache settings + TTL time.Duration + DisableCache bool + ClearCache bool } // NewGoBrew instance func NewGoBrew(config Config) GoBrew { installDir := filepath.Join(config.RootDir, goBrewDir) + cacheFile := filepath.Join(installDir, "cache.json") + gb := GoBrew{ Config: config, installDir: installDir, @@ -78,6 +87,11 @@ func NewGoBrew(config Config) GoBrew { currentBinDir: filepath.Join(installDir, "current", "bin"), currentGoDir: filepath.Join(installDir, "current", "go"), downloadsDir: filepath.Join(installDir, "downloads"), + cacheFile: cacheFile, + } + + if gb.ClearCache { + _ = os.RemoveAll(gb.cacheFile) } return gb diff --git a/helpers.go b/helpers.go index 1f40e9f..4cf23d7 100644 --- a/helpers.go +++ b/helpers.go @@ -528,9 +528,12 @@ type Cache struct { } func (gb *GoBrew) getVersionsFromCache() []string { - cacheFile := filepath.Join(gb.installDir, "cache.json") - if _, err := os.Stat(cacheFile); err == nil { - data, e := os.ReadFile(cacheFile) + if gb.DisableCache { + return []string{} + } + + if _, err := os.Stat(gb.cacheFile); err == nil { + data, e := os.ReadFile(gb.cacheFile) if e != nil { return []string{} } @@ -545,8 +548,8 @@ func (gb *GoBrew) getVersionsFromCache() []string { return []string{} } - // cache for 20 minutes - if time.Now().UTC().After(timestamp.Add(20 * time.Minute)) { + // cache for gb.TTL duration + if time.Now().UTC().After(timestamp.Add(gb.TTL)) { return []string{} } @@ -557,6 +560,10 @@ func (gb *GoBrew) getVersionsFromCache() []string { } func (gb *GoBrew) saveVersionsToCache(versions []string) { + if gb.DisableCache { + return + } + cacheFile := filepath.Join(gb.installDir, "cache.json") var cache = Cache{ Timestamp: time.Now().UTC().Format(time.RFC3339),