Skip to content

Commit

Permalink
feat: new flag options for cache
Browse files Browse the repository at this point in the history
  • Loading branch information
juev committed Sep 3, 2024
1 parent edeeff9 commit 4bbbb55
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 29 deletions.
79 changes: 57 additions & 22 deletions cmd/gobrew/main.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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",
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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":
Expand All @@ -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
}
Expand All @@ -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 + `
Expand All @@ -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
Expand All @@ -195,5 +230,5 @@ Examples:
Installation Path:
` + usageMsg

return msg
fmt.Fprintf(os.Stderr, "%s\n", msg)
}
2 changes: 1 addition & 1 deletion cmd/gobrew/main_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion cmd/gobrew/main_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
14 changes: 14 additions & 0 deletions gobrew.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sort"
"strconv"
"strings"
"time"

"github.com/Masterminds/semver"
"github.com/gookit/color"
Expand Down Expand Up @@ -56,6 +57,7 @@ type GoBrew struct {
currentBinDir string
currentGoDir string
downloadsDir string
cacheFile string
Config
}

Expand All @@ -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,
Expand All @@ -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
Expand Down
17 changes: 12 additions & 5 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
Expand All @@ -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{}
}

Expand All @@ -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),
Expand Down

0 comments on commit 4bbbb55

Please sign in to comment.