-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 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,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 | ||
} |
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,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)) | ||
} |
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