mirror of
https://github.com/Jguer/yay.git
synced 2025-12-27 10:01:53 -05:00
feat(config): refactor config to allow running without config
This commit is contained in:
106
main.go
106
main.go
@@ -1,8 +1,6 @@
|
||||
package main // import "github.com/Jguer/yay"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
@@ -24,57 +22,6 @@ func initGotext() {
|
||||
gotext.Configure(localePath, os.Getenv("LANG"), "yay")
|
||||
}
|
||||
|
||||
func initConfig(configPath string) error {
|
||||
cfile, err := os.Open(configPath)
|
||||
if !os.IsNotExist(err) && err != nil {
|
||||
return errors.New(gotext.Get("failed to open config file '%s': %s", configPath, err))
|
||||
}
|
||||
|
||||
defer cfile.Close()
|
||||
if !os.IsNotExist(err) {
|
||||
decoder := json.NewDecoder(cfile)
|
||||
if err = decoder.Decode(&config); err != nil {
|
||||
return errors.New(gotext.Get("failed to read config file '%s': %s", configPath, err))
|
||||
}
|
||||
}
|
||||
|
||||
aurdest := os.Getenv("AURDEST")
|
||||
if aurdest != "" {
|
||||
config.BuildDir = aurdest
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func initVCS(vcsFilePath string) error {
|
||||
vfile, err := os.Open(vcsFilePath)
|
||||
if !os.IsNotExist(err) && err != nil {
|
||||
return errors.New(gotext.Get("failed to open vcs file '%s': %s", vcsFilePath, err))
|
||||
}
|
||||
|
||||
defer vfile.Close()
|
||||
if !os.IsNotExist(err) {
|
||||
decoder := json.NewDecoder(vfile)
|
||||
if err = decoder.Decode(&savedInfo); err != nil {
|
||||
return errors.New(gotext.Get("failed to read vcs file '%s': %s", vcsFilePath, err))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func initBuildDir() error {
|
||||
if _, err := os.Stat(config.BuildDir); os.IsNotExist(err) {
|
||||
if err = os.MkdirAll(config.BuildDir, 0o755); err != nil {
|
||||
return errors.New(gotext.Get("failed to create BuildDir directory '%s': %s", config.BuildDir, err))
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func initAlpm(cmdArgs *settings.Arguments, pacmanConfigPath string) (*pacmanconf.Config, bool, error) {
|
||||
root := "/"
|
||||
if value, _, exists := cmdArgs.GetArg("root", "r"); exists {
|
||||
@@ -124,6 +71,7 @@ func initAlpm(cmdArgs *settings.Arguments, pacmanConfigPath string) (*pacmanconf
|
||||
}
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
ret := 0
|
||||
defer func() { os.Exit(ret) }()
|
||||
initGotext()
|
||||
@@ -131,28 +79,16 @@ func main() {
|
||||
text.Warnln(gotext.Get("Avoid running yay as root/sudo."))
|
||||
}
|
||||
|
||||
config, err = settings.NewConfig()
|
||||
if err != nil {
|
||||
if str := err.Error(); str != "" {
|
||||
fmt.Fprintln(os.Stderr, str)
|
||||
}
|
||||
ret = 1
|
||||
return
|
||||
}
|
||||
|
||||
cmdArgs := settings.MakeArguments()
|
||||
runtime, err := settings.MakeRuntime()
|
||||
if err != nil {
|
||||
if str := err.Error(); str != "" {
|
||||
fmt.Fprintln(os.Stderr, str)
|
||||
}
|
||||
ret = 1
|
||||
return
|
||||
}
|
||||
|
||||
config = settings.MakeConfig()
|
||||
config.Runtime = runtime
|
||||
|
||||
err = initConfig(runtime.ConfigPath)
|
||||
if err != nil {
|
||||
if str := err.Error(); str != "" {
|
||||
fmt.Fprintln(os.Stderr, str)
|
||||
}
|
||||
ret = 1
|
||||
return
|
||||
}
|
||||
|
||||
err = cmdArgs.ParseCommandLine(config)
|
||||
if err != nil {
|
||||
if str := err.Error(); str != "" {
|
||||
@@ -163,30 +99,12 @@ func main() {
|
||||
}
|
||||
|
||||
if config.Runtime.SaveConfig {
|
||||
errS := config.SaveConfig(runtime.ConfigPath)
|
||||
errS := config.Save(config.Runtime.ConfigPath)
|
||||
if errS != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
}
|
||||
}
|
||||
|
||||
config.ExpandEnv()
|
||||
err = initBuildDir()
|
||||
if err != nil {
|
||||
if str := err.Error(); str != "" {
|
||||
fmt.Fprintln(os.Stderr, str)
|
||||
}
|
||||
ret = 1
|
||||
return
|
||||
}
|
||||
|
||||
err = initVCS(runtime.VCSPath)
|
||||
if err != nil {
|
||||
if str := err.Error(); str != "" {
|
||||
fmt.Fprintln(os.Stderr, str)
|
||||
}
|
||||
ret = 1
|
||||
return
|
||||
}
|
||||
var useColor bool
|
||||
config.Runtime.PacmanConf, useColor, err = initAlpm(cmdArgs, config.PacmanConf)
|
||||
if err != nil {
|
||||
@@ -199,7 +117,7 @@ func main() {
|
||||
|
||||
text.UseColor = useColor
|
||||
|
||||
dbExecutor, err := ialpm.NewExecutor(runtime.PacmanConf)
|
||||
dbExecutor, err := ialpm.NewExecutor(config.Runtime.PacmanConf)
|
||||
if err != nil {
|
||||
if str := err.Error(); str != "" {
|
||||
fmt.Fprintln(os.Stderr, str)
|
||||
|
||||
Reference in New Issue
Block a user