diff --git a/config.go b/config.go index 10f0220..b2f250d 100644 --- a/config.go +++ b/config.go @@ -3,20 +3,20 @@ package main import ( "github.com/pelletier/go-toml" "io/ioutil" - "log" + "path/filepath" ) // Config contains the root of the TOML config file type Config struct { ActiveManager string - RootCommand string - Managers map[string]Manager + RootCommand string + Managers map[string]Manager } // Manager contains the root of all manager sections in the TOML config file type Manager struct { - UseRoot bool - Commands map[string]string + UseRoot bool + Commands map[string]string Shortcuts map[string]string } @@ -25,14 +25,14 @@ func NewConfig(path string) Config { // Read file at path data, err := ioutil.ReadFile(path) if err != nil { - log.Fatalln(err) + Log.Fatal().Err(err).Str("file", filepath.Base(path)).Msg("Error reading config") } // Create new Config{} cfg := Config{} // Unmarshal TOML in config err = toml.Unmarshal(data, &cfg) if err != nil { - log.Fatalln(err) + Log.Fatal().Err(err).Str("file", filepath.Base(path)).Msg("Error unmarshalling TOML") } // Return config return cfg diff --git a/go.mod b/go.mod index d291212..00d235b 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,6 @@ go 1.15 require ( github.com/pelletier/go-toml v1.8.2-0.20201124181426-2e01f733df54 + github.com/rs/zerolog v1.20.0 github.com/spf13/pflag v1.0.5 ) diff --git a/go.sum b/go.sum index aee2bde..28dd079 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,18 @@ +github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pelletier/go-toml v1.8.2-0.20201124181426-2e01f733df54 h1:U7n5zyrdmyUTvXR7DRuSRrdFBkjcIPeCjSG2GScQSL8= github.com/pelletier/go-toml v1.8.2-0.20201124181426-2e01f733df54/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= +github.com/rs/zerolog v1.20.0 h1:38k9hgtUBdxFwE34yS8rTHmHBa4eN16E4DJlv177LNs= +github.com/rs/zerolog v1.20.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/main.go b/main.go index 0d14417..8f0f1a8 100644 --- a/main.go +++ b/main.go @@ -20,45 +20,37 @@ package main import ( "fmt" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" flag "github.com/spf13/pflag" - "log" "os" "os/exec" "os/user" "strings" ) +var Log = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) + func main() { - // Check which currentUser is running command - currentUser, err := user.Current() - if err != nil { - log.Fatal(err) - } - - // Create help flags + // Create help flag var helpFlagGiven bool flag.BoolVarP(&helpFlagGiven, "help", "h", false, "Show help screen") - - // Check to make sure root is not being used unless -r/--root specified - var rootCheckBypass bool - // Create --root and -r flags for root check bypass - flag.BoolVarP(&rootCheckBypass, "root", "r", false, "Bypass root check") - + // Create package manager override flag var packageManagerOverride string flag.StringVarP(&packageManagerOverride, "package-manager", "p", os.Getenv("PAK_MGR_OVERRIDE"), "Override package manager wrapped by pak") // Parse arguments for flags flag.Parse() - // If flag not given - if !rootCheckBypass { - // If current user is root - if strings.Contains(currentUser.Username, "root") { - // Print warning message and exit - fmt.Println("Do not run as root, this program will invoke root for you if selected in config.") - fmt.Println("If you would like to bypass this, run this command with -r or --root.") - os.Exit(1) - } + // Check which user is running command + currentUser, err := user.Current() + if err != nil { + Log.Fatal().Err(err).Msg("Error getting current user") + } + // If running as root + if strings.Contains(currentUser.Username, "root") { + // Print warning message + Log.Warn().Msg("Running as root may cause extraneous root invocation") } // Get arguments without flags @@ -98,7 +90,7 @@ func main() { //fmt.Println(shortcuts) //DEBUG // Create similar to slice to put all matched commands into - var similarTo []string + similarTo := []string{} // Displays help message if no arguments provided or -h/--help is passed if len(args) == 0 || helpFlagGiven || Contains(args, "help") { @@ -132,7 +124,7 @@ func main() { // If similarTo is still empty, log it fatally as something is wrong with the config or the code if len(similarTo) == 0 { - log.Fatalln("This command does not match any known commands or shortcuts") + Log.Fatal().Msg("This command does not match any known commands or shortcuts") } // Anonymous function to decide whether to print (overridden) printOverridden := func() string { @@ -168,7 +160,6 @@ func main() { err = command.Run() // If command returned an error, log fatally with explanation if err != nil { - fmt.Println("Error received from child process") - log.Fatal(err) + Log.Fatal().Err(err).Msg("Error received from child process") } } diff --git a/pak.toml b/pak.toml index eced486..5a88d78 100644 --- a/pak.toml +++ b/pak.toml @@ -93,6 +93,9 @@ rootCommand = "sudo" remove = "uninstall" install = "install" + [managers.pip] + useRoot = false + [managers.pacman] useRoot = true [managers.pacman.commands] @@ -124,4 +127,50 @@ rootCommand = "sudo" file-search = "-Fx" interactive = "" [managers.yay.shortcuts] - arm = "-Rss $(yay -Qdtq)" + arm = "-Rss $(yay -Qdtq)" + + [managers.apk] + useRoot = true + [managers.apk.commands] + install = "add" + remove = "del" + fix = "fix" + refresh = "update" + upgrade = "upgrade" + search = "search" + list = "info" + + [managers.dnf] + useRoot = true + [managers.dnf.commands] + install = "install" + remove = "remove" + reinstall = "reinstall" + list = "list" + search = "search" + clean = "clean" + downgrade = "downgrade" + + [managers.yum] + useRoot = true + [managers.yum.commands] + install = "install" + remove = "remove" + reinstall = "reinstall" + list = "list" + search = "search" + clean = "clean" + downgrade = "downgrade" + + [managers.zypper] + useRoot = true + [managers.zypper.commands] + install = "install" + remove = "remove" + refresh = "refresh" + clean = "clean" + addrepo = "addrepo" + removerepo = "removerepo" + list-repos = "repos" + list = "packages -i" + search = "search"