Skip to content

Commit

Permalink
feat: Add caching dir and resolving option (#82)
Browse files Browse the repository at this point in the history
Signed-off-by: AlexNg <contact@ngjx.org>
  • Loading branch information
caffeine-addictt committed Aug 30, 2024
1 parent d281da1 commit a84da38
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 2 deletions.
30 changes: 29 additions & 1 deletion cmd/options/options.go
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)
}
15 changes: 15 additions & 0 deletions cmd/options/root.go
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>"),
}
10 changes: 9 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"log"
"os"

"github.com/caffeine-addictt/template/cmd/commands"
"github.com/caffeine-addictt/template/cmd/options"
Expand All @@ -13,12 +14,19 @@ var RootCmd = &cobra.Command{
Use: "template",
Short: "Let's make starting new projects feel like a breeze again.",
Long: "This tool helps you to create a new project from templates.",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if err := options.Opts.ResolveOptions(); err != nil {
cmd.PrintErrln(err)
os.Exit(1)
}
},
}

// Setting up configuration
func init() {
RootCmd.PersistentFlags().BoolVarP(&options.Opts.Debug, "debug", "d", false, "Debug mode")
RootCmd.PersistentFlags().BoolVarP(&options.Opts.Debug, "debug", "d", false, "Debug mode [default: false]")
RootCmd.PersistentFlags().VarP(&options.Opts.Repo, "repo", "r", "Community source repository for templates")
RootCmd.PersistentFlags().VarP(&options.Opts.CacheDir, "cache", "C", "Where source repository will be cloned to [default: $XDG_CONFIG_HOME/template]")

commands.InitCommands(RootCmd)
}
Expand Down
63 changes: 63 additions & 0 deletions cmd/utils/file.go
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
}

0 comments on commit a84da38

Please sign in to comment.