2020-07-05 00:45:23 +00:00
|
|
|
package settings
|
|
|
|
|
2020-07-05 01:17:35 +00:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2020-07-05 13:34:27 +00:00
|
|
|
"github.com/Morganamilo/go-pacmanconf"
|
2020-07-05 01:17:35 +00:00
|
|
|
"github.com/leonelquinteros/gotext"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-07-05 00:45:23 +00:00
|
|
|
type TargetMode int
|
|
|
|
|
2020-07-05 01:17:35 +00:00
|
|
|
// configFileName holds the name of the config file.
|
|
|
|
const configFileName string = "config.json"
|
|
|
|
|
|
|
|
// vcsFileName holds the name of the vcs file.
|
|
|
|
const vcsFileName string = "vcs.json"
|
|
|
|
|
|
|
|
const completionFileName string = "completion.cache"
|
|
|
|
|
2020-07-05 00:45:23 +00:00
|
|
|
const (
|
|
|
|
ModeAny TargetMode = iota
|
|
|
|
ModeAUR
|
|
|
|
ModeRepo
|
|
|
|
)
|
|
|
|
|
|
|
|
type Runtime struct {
|
2020-07-05 01:17:35 +00:00
|
|
|
Mode TargetMode
|
|
|
|
SaveConfig bool
|
|
|
|
CompletionPath string
|
|
|
|
ConfigPath string
|
|
|
|
VCSPath string
|
2020-07-05 13:34:27 +00:00
|
|
|
PacmanConf *pacmanconf.Config
|
2020-07-05 01:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func MakeRuntime() (*Runtime, error) {
|
|
|
|
cacheHome := ""
|
|
|
|
configHome := ""
|
|
|
|
|
|
|
|
runtime := &Runtime{
|
|
|
|
Mode: ModeAny,
|
|
|
|
SaveConfig: false,
|
|
|
|
CompletionPath: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
if configHome = os.Getenv("XDG_CONFIG_HOME"); configHome != "" {
|
|
|
|
configHome = filepath.Join(configHome, "yay")
|
|
|
|
} else if configHome = os.Getenv("HOME"); configHome != "" {
|
2020-07-05 22:28:04 +00:00
|
|
|
configHome = filepath.Join(configHome, ".config", "yay")
|
2020-07-05 01:17:35 +00:00
|
|
|
} else {
|
|
|
|
return nil, errors.New(gotext.Get("%s and %s unset", "XDG_CONFIG_HOME", "HOME"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := initDir(configHome); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cacheHome = os.Getenv("XDG_CACHE_HOME"); cacheHome != "" {
|
|
|
|
cacheHome = filepath.Join(cacheHome, "yay")
|
|
|
|
} else if cacheHome = os.Getenv("HOME"); cacheHome != "" {
|
2020-07-05 22:28:04 +00:00
|
|
|
cacheHome = filepath.Join(cacheHome, ".cache", "yay")
|
2020-07-05 01:17:35 +00:00
|
|
|
} else {
|
|
|
|
return nil, errors.New(gotext.Get("%s and %s unset", "XDG_CACHE_HOME", "HOME"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := initDir(cacheHome); err != nil {
|
|
|
|
return runtime, err
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.ConfigPath = filepath.Join(configHome, configFileName)
|
|
|
|
runtime.VCSPath = filepath.Join(cacheHome, vcsFileName)
|
|
|
|
runtime.CompletionPath = filepath.Join(cacheHome, completionFileName)
|
|
|
|
|
|
|
|
return runtime, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func initDir(dir string) error {
|
|
|
|
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
2020-07-06 00:02:12 +00:00
|
|
|
if err = os.MkdirAll(dir, 0o755); err != nil {
|
2020-07-05 01:17:35 +00:00
|
|
|
return errors.New(gotext.Get("failed to create config directory '%s': %s", dir, err))
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-07-05 00:45:23 +00:00
|
|
|
}
|