Skip to content

Commit

Permalink
Merge pull request Jguer#273 from Morganamilo/util
Browse files Browse the repository at this point in the history
Add util.go and main.go
  • Loading branch information
Jguer authored Mar 22, 2018
2 parents 415659d + 7fd2a74 commit 59b74e1
Show file tree
Hide file tree
Showing 10 changed files with 358 additions and 389 deletions.
221 changes: 1 addition & 220 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"

alpm "github.com/jguer/go-alpm"
)

var cmdArgs = makeArguments()
Expand Down Expand Up @@ -88,221 +84,6 @@ Yay specific options:
If no operation is provided -Y will be assumed`)
}

func initPaths() {
if configHome = os.Getenv("XDG_CONFIG_HOME"); configHome != "" {
if info, err := os.Stat(configHome); err == nil && info.IsDir() {
configHome = configHome + "/yay"
} else {
configHome = os.Getenv("HOME") + "/.config/yay"
}
} else {
configHome = os.Getenv("HOME") + "/.config/yay"
}

if cacheHome = os.Getenv("XDG_CACHE_HOME"); cacheHome != "" {
if info, err := os.Stat(cacheHome); err == nil && info.IsDir() {
cacheHome = cacheHome + "/yay"
} else {
cacheHome = os.Getenv("HOME") + "/.cache/yay"
}
} else {
cacheHome = os.Getenv("HOME") + "/.cache/yay"
}

configFile = configHome + "/" + configFileName
vcsFile = cacheHome + "/" + vcsFileName
completionFile = cacheHome + "/" + completionFilePrefix
}

func initConfig() (err error) {
defaultSettings(&config)

if _, err = os.Stat(configFile); os.IsNotExist(err) {
err = os.MkdirAll(filepath.Dir(configFile), 0755)
if err != nil {
err = fmt.Errorf("Unable to create config directory:\n%s\n"+
"The error was:\n%s", 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.Printf("Error reading config: %s\n", err)
} else {
defer cfile.Close()
decoder := json.NewDecoder(cfile)
err = decoder.Decode(&config)
if err != nil {
fmt.Println("Loading default Settings.\nError reading config:",
err)
defaultSettings(&config)
}
}
}

return
}

func initVCS() (err error) {
if _, err = os.Stat(vcsFile); os.IsNotExist(err) {
err = os.MkdirAll(filepath.Dir(vcsFile), 0755)
if err != nil {
err = fmt.Errorf("Unable to create vcs directory:\n%s\n"+
"The error was:\n%s", filepath.Dir(configFile), err)
return
}
} else {
vfile, err := os.OpenFile(vcsFile, os.O_RDONLY|os.O_CREATE, 0644)
if err == nil {
defer vfile.Close()
decoder := json.NewDecoder(vfile)
_ = decoder.Decode(&savedInfo)
}
}

return
}

func initAlpm() (err error) {
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 {
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
}

value, _, exists = cmdArgs.getArg("ignore")
if exists {
alpmConf.IgnorePkg = append(alpmConf.IgnorePkg, strings.Split(value, ",")...)
}

value, _, exists = cmdArgs.getArg("ignoregroup")
if exists {
alpmConf.IgnoreGroup = append(alpmConf.IgnoreGroup, strings.Split(value, ",")...)
}

//TODO
//current system does not allow duplicate arguments
//but pacman allows multiple cachdirs to be passed
//for now only handle 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 {
err = fmt.Errorf("Unable to CreateHandle: %s", err)
return
}

value, _, exists = cmdArgs.getArg("color")
if value == "always" || value == "auto" {
useColor = true
} else if value == "never" {
useColor = false
} else {
useColor = alpmConf.Options&alpm.ConfColor > 0
}

alpmHandle.SetQuestionCallback(questionCallback)

return
}

func main() {
var status int
var err error

if 0 == os.Geteuid() {
fmt.Println("Please avoid running yay as root/sudo.")
}

err = cmdArgs.parseCommandLine()
if err != nil {
fmt.Println(err)
status = 1
goto cleanup
}

initPaths()

err = initConfig()
if err != nil {
fmt.Println(err)
status = 1
goto cleanup
}

err = initVCS()
if err != nil {
fmt.Println(err)
status = 1
goto cleanup

}

err = initAlpm()
if err != nil {
fmt.Println(err)
status = 1
goto cleanup
}

err = handleCmd()
if err != nil {
fmt.Println(err)
status = 1
goto cleanup
}

cleanup:
//cleanup
//from here on out dont exit if an error occurs
//if we fail to save the configuration
//at least continue on and try clean up other parts

if alpmHandle != nil {
err = alpmHandle.Release()
if err != nil {
fmt.Println(err)
status = 1
}
}

os.Exit(status)
}

func sudoLoopBackground() {
updateSudo()
go sudoLoop()
Expand Down Expand Up @@ -365,7 +146,7 @@ func handleCmd() (err error) {
case "T", "deptest":
err = passToPacman(cmdArgs)
case "U", "upgrade":
err =passToPacman(cmdArgs)
err = passToPacman(cmdArgs)
case "G", "getpkgbuild":
err = handleGetpkgbuild()
case "P", "print":
Expand Down
Loading

0 comments on commit 59b74e1

Please sign in to comment.