Skip to content

Commit

Permalink
Create cache dir if it does not exist; add version in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
gkze committed Nov 6, 2018
1 parent b051771 commit 85ece58
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func main() {
cmdline := cli.NewApp()
cmdline.Name = "stars"
cmdline.Usage = "Command-line interface to YOUR GitHub stars"
cmdline.Version = "0.3.3"
cmdline.Commands = []cli.Command{
{
Name: "save",
Expand Down
23 changes: 22 additions & 1 deletion starmanager/starmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ import (
// GITHUB - the GitHub API host
const GITHUB string = "api.github.com"

// CACHEPATH - the path to the cache db file
const CACHEPATH = ".cache"

// CACHEFILE - the filename of the db cache
const CACHEFILE = "stars.db"

// PAGESIZE - the default response page size (GitHub maximum is 100 so we use that)
const PAGESIZE int = 100

Expand Down Expand Up @@ -64,7 +70,22 @@ func New() (*StarManager, error) {
return nil, err
}

db, err := storm.Open(filepath.Join(currentUser.HomeDir, ".cache/stars.db"), storm.Batch())
cacheFullPath := filepath.Join(currentUser.HomeDir, CACHEPATH, CACHEFILE)
_, ferr := os.Stat(cacheFullPath)
if ferr != nil && os.IsNotExist(ferr) {
mkdirErr := os.Mkdir(filepath.Join(currentUser.HomeDir, CACHEPATH), 0700)
if mkdirErr != nil {
log.Printf(
"An error occurred while attempting to create %s: %s\n",
CACHEPATH,
mkdirErr.Error(),
)

return nil, err
}
}

db, err := storm.Open(cacheFullPath, storm.Batch())
if err != nil {
log.Printf("An error occurred opening the db! %v", err.Error())

Expand Down

0 comments on commit 85ece58

Please sign in to comment.