2020-07-05 02:45:23 +02:00
|
|
|
package settings
|
|
|
|
|
2020-07-05 03:17:35 +02:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2020-07-05 16:58:35 +02:00
|
|
|
"github.com/Jguer/go-alpm"
|
2020-07-05 15:34:27 +02:00
|
|
|
"github.com/Morganamilo/go-pacmanconf"
|
2020-07-05 03:17:35 +02:00
|
|
|
"github.com/leonelquinteros/gotext"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-07-05 02:45:23 +02:00
|
|
|
type TargetMode int
|
|
|
|
|
2020-07-05 03:17:35 +02: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 02:45:23 +02:00
|
|
|
const (
|
|
|
|
ModeAny TargetMode = iota
|
|
|
|
ModeAUR
|
|
|
|
ModeRepo
|
|
|
|
)
|
|
|
|
|
|
|
|
type Runtime struct {
|
2020-07-05 03:17:35 +02:00
|
|
|
Mode TargetMode
|
|
|
|
SaveConfig bool
|
|
|
|
CompletionPath string
|
|
|
|
ConfigPath string
|
|
|
|
VCSPath string
|
2020-07-05 15:34:27 +02:00
|
|
|
PacmanConf *pacmanconf.Config
|
2020-07-05 16:58:35 +02:00
|
|
|
AlpmHandle *alpm.Handle
|
2020-07-05 03:17:35 +02: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-06 00:28:04 +02:00
|
|
|
configHome = filepath.Join(configHome, ".config", "yay")
|
2020-07-05 03:17:35 +02: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-06 00:28:04 +02:00
|
|
|
cacheHome = filepath.Join(cacheHome, ".cache", "yay")
|
2020-07-05 03:17:35 +02: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) {
|
|
|
|
if err = os.MkdirAll(dir, 0755); err != nil {
|
|
|
|
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 02:45:23 +02:00
|
|
|
}
|