Pass command line arguments to alpmConf before init

This allows us to use command line options such as '--root' and '-b' when
performing operations that use out alom handle.

Sadly this does not apply to passToMakepkg which will ignore options
such as '--root' and because we pass '-i' to makepkg it installs for us
using the default options.

Currently I have not planned a solution for this but one which I thought
of but not looked into is. Always call makepkg without arguments (except
from '--noconfirm' and others which might still be needed) and manage
the dependancies and post install outselves.

Another option might be to use makepkg's $PACMAN enviroment variable and
redirect the pacman calls to yay. Although I am unsure about both
stratergys they are just my current thoughts.

Also while editing the flow of cmd.go, I managed to refactor away all
os.Exit calls apart from the very last so it should be more clear as to
where the program exits.
This commit is contained in:
morganamilo 2018-01-04 21:21:39 +00:00
parent 232edc64a6
commit c0fc086a07
No known key found for this signature in database
GPG key ID: 6FE9E7996B0B082E
2 changed files with 109 additions and 48 deletions

124
cmd.go
View file

@ -55,10 +55,9 @@ If no operation is provided -Y will be assumed
`)
}
func init() {
func initYay() (err error){
var configHome string // configHome handles config directory home
var cacheHome string // cacheHome handles cache home
var err error
if 0 == os.Geteuid() {
fmt.Println("Please avoid running yay as root/sudo.")
@ -96,15 +95,15 @@ func init() {
if _, err = os.Stat(configFile); os.IsNotExist(err) {
err = os.MkdirAll(filepath.Dir(configFile), 0755)
if err != nil {
fmt.Println("Unable to create config directory:", filepath.Dir(configFile), err)
os.Exit(2)
err = fmt.Errorf("Unable to create config directory:", filepath.Dir(configFile), err)
return
}
// Save the default config if nothing is found
config.saveConfig()
} else {
cfile, errf := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE, 0644)
if errf != nil {
fmt.Println("Error reading config:", err)
fmt.Println("Error reading config: %s", err)
} else {
defer cfile.Close()
decoder := json.NewDecoder(cfile)
@ -127,65 +126,110 @@ func init() {
decoder := json.NewDecoder(vfile)
_ = decoder.Decode(&savedInfo)
}
return
}
func initAlpm() (err error){
/////////////////
// alpm config //
/////////////////
var value string
var exists bool
//var double bool
value, _, exists = cmdArgs.getArg("config")
if exists {
config.PacmanConf = value
}
alpmConf, err = readAlpmConfig(config.PacmanConf)
if err != nil {
fmt.Println("Unable to read Pacman conf", err)
os.Exit(1)
err = fmt.Errorf("Unable to read Pacman conf: %s", err)
return
}
value, _, exists = cmdArgs.getArg("dbpath", "b")
if exists {
alpmConf.DBPath = value
}
value, _, exists = cmdArgs.getArg("root", "r")
if exists {
alpmConf.RootDir = value
}
value, _, exists = cmdArgs.getArg("arch")
if exists {
alpmConf.Architecture = value
}
//TODO
//current system does not allow duplicate arguments
//but pacman allows multiple cachdirs to be passed
//for now only hanle one cache dir
value, _, exists = cmdArgs.getArg("cachdir")
if exists {
alpmConf.CacheDir = []string{value}
}
value, _, exists = cmdArgs.getArg("gpgdir")
if exists {
alpmConf.GPGDir = value
}
alpmHandle, err = alpmConf.CreateHandle()
if err != nil {
fmt.Println("Unable to CreateHandle", err)
os.Exit(1)
err = fmt.Errorf("Unable to CreateHandle", err)
return
}
return
}
func main() {
status := run()
err := alpmHandle.Release()
if err != nil {
fmt.Println(err)
status = 1
}
os.Exit(status)
}
func run() (status int) {
var status int = 0
var err error
var changedConfig bool
err = cmdArgs.parseCommandLine();
if err != nil {
fmt.Println(err)
status = 1
return
goto cleanup
}
if cmdArgs.existsArg("-") {
err = cmdArgs.parseStdin();
err = initYay()
if err != nil {
fmt.Println(err)
status = 1
goto cleanup
}
if err != nil {
fmt.Println(err)
status = 1
return
}
err = initAlpm()
if err != nil {
fmt.Println(err)
status = 1
goto cleanup
}
changedConfig, err = handleCmd()
if err != nil {
fmt.Println(err)
status = 1
//try continue onward
goto cleanup
}
//ive used a goto here
//i think its the best way to do this sort of thing
cleanup:
//cleanup
//from here on out dont exit if an error occurs
//if we fail to save the configuration
//atleast continue on and try clean up other parts
if updated {
err = saveVCSInfo()
@ -205,11 +249,17 @@ func run() (status int) {
}
return
if alpmHandle != nil {
err = alpmHandle.Release()
if err != nil {
fmt.Println(err)
status = 1
}
}
os.Exit(status)
}
func handleCmd() (changedConfig bool, err error) {
changedConfig = false
@ -534,10 +584,6 @@ func passToPacman(args *arguments) error {
argArr = append(argArr, args.formatArgs()...)
argArr = append(argArr, args.formatTargets()...)
fmt.Println(cmdArgs)
fmt.Println(args)
fmt.Println(argArr)
cmd = exec.Command(argArr[0], argArr[1:]...)
@ -560,4 +606,4 @@ func passToMakepkg(dir string, args ...string) (err error) {
}
}
return
}
}

View file

@ -195,16 +195,23 @@ func (parser *arguments) existsArg(options ...string) bool {
return false
}
func (parser *arguments) getArg(option string) (arg string, double bool, exists bool) {
arg, exists = parser.options[option]
if exists {
_, double = parser.doubles[option]
return
}
func (parser *arguments) getArg(options ...string) (arg string, double bool, exists bool) {
for _, option := range options {
arg, exists = parser.options[option]
arg, exists = parser.globals[option]
_, double = parser.doubles[option]
if exists {
_, double = parser.doubles[option]
return
}
arg, exists = parser.globals[option]
if exists {
_, double = parser.doubles[option]
return
}
}
return
}
@ -523,5 +530,13 @@ func (parser *arguments)parseCommandLine() (err error) {
parser.op = "Y"
}
if cmdArgs.existsArg("-") {
err = cmdArgs.parseStdin();
if err != nil {
return
}
}
return
}