-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add caching dir and resolving option (#82)
Signed-off-by: AlexNg <contact@ngjx.org>
- Loading branch information
1 parent
d281da1
commit a84da38
Showing
4 changed files
with
116 additions
and
2 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 |
---|---|---|
@@ -1,12 +1,40 @@ | ||
package options | ||
|
||
import "github.com/caffeine-addictt/template/cmd/utils/types" | ||
import ( | ||
"github.com/caffeine-addictt/template/cmd/utils" | ||
"github.com/caffeine-addictt/template/cmd/utils/types" | ||
) | ||
|
||
type Options struct { | ||
// The repository Url to use | ||
// Should be this repository by default | ||
Repo types.ValueGuard[string] | ||
|
||
// Where the cached repositories will live | ||
CacheDir types.ValueGuard[string] | ||
|
||
// Wheter or not debug mode should be enabled | ||
Debug bool | ||
} | ||
|
||
// To resolve the options after the user has provided them | ||
func (o *Options) ResolveOptions() error { | ||
if err := o.resolveCacheDir(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (o *Options) resolveCacheDir() error { | ||
if o.CacheDir.Value() != "" { | ||
return nil | ||
} | ||
|
||
defaultCacheDir, err := utils.GetDefaultCacheDir() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return o.CacheDir.Set(defaultCacheDir) | ||
} |
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 |
---|---|---|
@@ -1,11 +1,26 @@ | ||
package options | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/caffeine-addictt/template/cmd/utils" | ||
"github.com/caffeine-addictt/template/cmd/utils/types" | ||
) | ||
|
||
// The global options for the CLI | ||
var Opts = Options{ | ||
Debug: false, | ||
Repo: *types.NewValueGuardNoParsing("", "<repo>"), | ||
CacheDir: *types.NewValueGuard("", func(v string) (string, error) { | ||
ok, err := utils.IsDir(v) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if !ok { | ||
return "", fmt.Errorf("'%s' is not a valid directory", v) | ||
} | ||
|
||
return v, nil | ||
}, "<path>"), | ||
} |
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
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,63 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func GetDefaultCacheDir() (string, error) { | ||
dirPath := os.Getenv("XDG_CACHE_HOME") | ||
|
||
if dirPath == "" { | ||
os.Getenv("LOCALAPPDATA") | ||
} | ||
|
||
if dirPath == "" { | ||
dirPath = filepath.Join(os.Getenv("HOME"), ".cache") | ||
} | ||
|
||
if dirPath == "" { | ||
dirPath = "/tmp" | ||
} | ||
|
||
dirPath = filepath.Join(dirPath, "template") | ||
ok, err := IsDir(dirPath) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// Create the directory if it doesn't exist | ||
if !ok { | ||
err := os.Mkdir(dirPath, 0o600) // rw-rw--- | ||
if err != nil { | ||
return "", fmt.Errorf("failed to create directory %s", err) | ||
} | ||
} | ||
|
||
return dirPath, nil | ||
} | ||
|
||
func IsDir(path string) (bool, error) { | ||
fileinfo, err := os.Stat(path) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return false, nil | ||
} | ||
return false, err | ||
} | ||
|
||
if !fileinfo.IsDir() { | ||
return false, fmt.Errorf("%v is not a directory", path) | ||
} | ||
|
||
mode := fileinfo.Mode() | ||
if mode&0o400 == 0 { | ||
return false, fmt.Errorf("directory %v is not readable", path) | ||
} | ||
if mode&0o200 == 0 { | ||
return false, fmt.Errorf("directory %v is not writable", path) | ||
} | ||
|
||
return true, nil | ||
} |