Skip to content

Commit

Permalink
feat: cache installed applications
Browse files Browse the repository at this point in the history
  • Loading branch information
5n7-sk committed Dec 13, 2020
1 parent 0a21141 commit 8719c8c
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
50 changes: 50 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package vin

import (
"os"
"path/filepath"
"strings"

"github.com/skmatz/vin/cache"
)

func (v *Vin) cacheDir() (string, error) {
cache, err := os.UserCacheDir()
if err != nil {
return "", err
}
return filepath.Join(cache, "vin"), nil
}

var repoReplacer = strings.NewReplacer("/", "---")

func (v *Vin) AppAlreadyInstalled(app App) (bool, error) {
cacheDir, err := v.cacheDir()
if err != nil {
return false, err
}

c := cache.New(cacheDir)
key := repoReplacer.Replace(app.Repo)
value := c.GetString(key)
return value == *app.release.TagName, nil
}

func (v *Vin) SaveCache(app App) error {
cacheDir, err := v.cacheDir()
if err != nil {
return err
}

if err := os.MkdirAll(cacheDir, os.ModePerm); err != nil {
return err
}

c := cache.New(cacheDir)
key := repoReplacer.Replace(app.Repo)
value := *app.release.TagName
if err := c.SetString(key, value); err != nil {
return err
}
return nil
}
56 changes: 56 additions & 0 deletions cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cache

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
)

// Cache represents a file cache.
type Cache struct {
dir string
m sync.RWMutex
}

func New(dir string) *Cache {
return &Cache{dir: dir}
}

func (c *Cache) path(key string) string {
return filepath.Join(c.dir, key)
}

func (c *Cache) Get(key string) []byte {
c.m.RLock()
defer c.m.RUnlock()

b, err := ioutil.ReadFile(c.path(key))
if err != nil {
return nil
}
return b
}

func (c *Cache) GetString(key string) string {
return string(c.Get(key))
}

func (c *Cache) Set(key string, value []byte) error {
if len(value) == 0 {
return fmt.Errorf("value is empty")
}

c.m.Lock()
defer c.m.Unlock()

if err := ioutil.WriteFile(c.path(key), value, os.ModePerm); err != nil {
return err
}
return nil
}

func (c *Cache) SetString(key, value string) error {
return c.Set(key, []byte(value))
}
9 changes: 9 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ func (c *CLI) Run(opt Options) error {
for _, app := range v.Apps {
app := app
eg.Go(func() error {
exists, err := v.AppAlreadyInstalled(app)
if err != nil || exists {
return err
}

urls := app.SuitableAssetURLs()
if len(urls) == 0 {
return fmt.Errorf("no suitable assets are found: %s", app.Repo)
Expand All @@ -115,6 +120,10 @@ func (c *CLI) Run(opt Options) error {
if err := app.RunCommand(); err != nil {
return err
}

if err := v.SaveCache(app); err != nil {
return err
}
return nil
})
}
Expand Down

0 comments on commit 8719c8c

Please sign in to comment.