Clean up code and switch to zerolog [skip ci]
This commit is contained in:
		
							
								
								
									
										14
									
								
								config.go
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								config.go
									
									
									
									
									
								
							@@ -3,20 +3,20 @@ package main
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"github.com/pelletier/go-toml"
 | 
						"github.com/pelletier/go-toml"
 | 
				
			||||||
	"io/ioutil"
 | 
						"io/ioutil"
 | 
				
			||||||
	"log"
 | 
						"path/filepath"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Config contains the root of the TOML config file
 | 
					// Config contains the root of the TOML config file
 | 
				
			||||||
type Config struct {
 | 
					type Config struct {
 | 
				
			||||||
	ActiveManager string
 | 
						ActiveManager string
 | 
				
			||||||
	RootCommand string
 | 
						RootCommand   string
 | 
				
			||||||
	Managers map[string]Manager
 | 
						Managers      map[string]Manager
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Manager contains the root of all manager sections in the TOML config file
 | 
					// Manager contains the root of all manager sections in the TOML config file
 | 
				
			||||||
type Manager struct {
 | 
					type Manager struct {
 | 
				
			||||||
	UseRoot bool
 | 
						UseRoot   bool
 | 
				
			||||||
	Commands map[string]string
 | 
						Commands  map[string]string
 | 
				
			||||||
	Shortcuts map[string]string
 | 
						Shortcuts map[string]string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -25,14 +25,14 @@ func NewConfig(path string) Config {
 | 
				
			|||||||
	// Read file at path
 | 
						// Read file at path
 | 
				
			||||||
	data, err := ioutil.ReadFile(path)
 | 
						data, err := ioutil.ReadFile(path)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Fatalln(err)
 | 
							Log.Fatal().Err(err).Str("file", filepath.Base(path)).Msg("Error reading config")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// Create new Config{}
 | 
						// Create new Config{}
 | 
				
			||||||
	cfg := Config{}
 | 
						cfg := Config{}
 | 
				
			||||||
	// Unmarshal TOML in config
 | 
						// Unmarshal TOML in config
 | 
				
			||||||
	err = toml.Unmarshal(data, &cfg)
 | 
						err = toml.Unmarshal(data, &cfg)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Fatalln(err)
 | 
							Log.Fatal().Err(err).Str("file", filepath.Base(path)).Msg("Error unmarshalling TOML")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// Return config
 | 
						// Return config
 | 
				
			||||||
	return cfg
 | 
						return cfg
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										1
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.mod
									
									
									
									
									
								
							@@ -4,5 +4,6 @@ go 1.15
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
require (
 | 
					require (
 | 
				
			||||||
	github.com/pelletier/go-toml v1.8.2-0.20201124181426-2e01f733df54
 | 
						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
 | 
						github.com/spf13/pflag v1.0.5
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										12
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 | 
				
			||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 | 
					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 h1:U7n5zyrdmyUTvXR7DRuSRrdFBkjcIPeCjSG2GScQSL8=
 | 
				
			||||||
github.com/pelletier/go-toml v1.8.2-0.20201124181426-2e01f733df54/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=
 | 
					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 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
 | 
				
			||||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
 | 
					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=
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										45
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										45
									
								
								main.go
									
									
									
									
									
								
							@@ -20,45 +20,37 @@ package main
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"github.com/rs/zerolog"
 | 
				
			||||||
 | 
						"github.com/rs/zerolog/log"
 | 
				
			||||||
	flag "github.com/spf13/pflag"
 | 
						flag "github.com/spf13/pflag"
 | 
				
			||||||
	"log"
 | 
					 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"os/exec"
 | 
						"os/exec"
 | 
				
			||||||
	"os/user"
 | 
						"os/user"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var Log = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Check which currentUser is running command
 | 
						// Create help flag
 | 
				
			||||||
	currentUser, err := user.Current()
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		log.Fatal(err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Create help flags
 | 
					 | 
				
			||||||
	var helpFlagGiven bool
 | 
						var helpFlagGiven bool
 | 
				
			||||||
	flag.BoolVarP(&helpFlagGiven, "help", "h", false, "Show help screen")
 | 
						flag.BoolVarP(&helpFlagGiven, "help", "h", false, "Show help screen")
 | 
				
			||||||
 | 
						// Create package manager override flag
 | 
				
			||||||
	// 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")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var packageManagerOverride string
 | 
						var packageManagerOverride string
 | 
				
			||||||
	flag.StringVarP(&packageManagerOverride, "package-manager", "p", os.Getenv("PAK_MGR_OVERRIDE"), "Override package manager wrapped by pak")
 | 
						flag.StringVarP(&packageManagerOverride, "package-manager", "p", os.Getenv("PAK_MGR_OVERRIDE"), "Override package manager wrapped by pak")
 | 
				
			||||||
	// Parse arguments for flags
 | 
						// Parse arguments for flags
 | 
				
			||||||
	flag.Parse()
 | 
						flag.Parse()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// If flag not given
 | 
						// Check which user is running command
 | 
				
			||||||
	if !rootCheckBypass {
 | 
						currentUser, err := user.Current()
 | 
				
			||||||
		// If current user is root
 | 
						if err != nil {
 | 
				
			||||||
		if strings.Contains(currentUser.Username, "root") {
 | 
							Log.Fatal().Err(err).Msg("Error getting current user")
 | 
				
			||||||
			// Print warning message and exit
 | 
						}
 | 
				
			||||||
			fmt.Println("Do not run as root, this program will invoke root for you if selected in config.")
 | 
						// If running as root
 | 
				
			||||||
			fmt.Println("If you would like to bypass this, run this command with -r or --root.")
 | 
						if strings.Contains(currentUser.Username, "root") {
 | 
				
			||||||
			os.Exit(1)
 | 
							// Print warning message
 | 
				
			||||||
		}
 | 
							Log.Warn().Msg("Running as root may cause extraneous root invocation")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Get arguments without flags
 | 
						// Get arguments without flags
 | 
				
			||||||
@@ -98,7 +90,7 @@ func main() {
 | 
				
			|||||||
	//fmt.Println(shortcuts) //DEBUG
 | 
						//fmt.Println(shortcuts) //DEBUG
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Create similar to slice to put all matched commands into
 | 
						// 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
 | 
						// Displays help message if no arguments provided or -h/--help is passed
 | 
				
			||||||
	if len(args) == 0 || helpFlagGiven || Contains(args, "help") {
 | 
						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 similarTo is still empty, log it fatally as something is wrong with the config or the code
 | 
				
			||||||
	if len(similarTo) == 0 {
 | 
						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)
 | 
						// Anonymous function to decide whether to print (overridden)
 | 
				
			||||||
	printOverridden := func() string {
 | 
						printOverridden := func() string {
 | 
				
			||||||
@@ -168,7 +160,6 @@ func main() {
 | 
				
			|||||||
	err = command.Run()
 | 
						err = command.Run()
 | 
				
			||||||
	// If command returned an error, log fatally with explanation
 | 
						// If command returned an error, log fatally with explanation
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		fmt.Println("Error received from child process")
 | 
							Log.Fatal().Err(err).Msg("Error received from child process")
 | 
				
			||||||
		log.Fatal(err)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										51
									
								
								pak.toml
									
									
									
									
									
								
							
							
						
						
									
										51
									
								
								pak.toml
									
									
									
									
									
								
							@@ -93,6 +93,9 @@ rootCommand = "sudo"
 | 
				
			|||||||
    remove = "uninstall"
 | 
					    remove = "uninstall"
 | 
				
			||||||
    install = "install"
 | 
					    install = "install"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  [managers.pip]
 | 
				
			||||||
 | 
					    useRoot = false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  [managers.pacman]
 | 
					  [managers.pacman]
 | 
				
			||||||
    useRoot = true
 | 
					    useRoot = true
 | 
				
			||||||
  [managers.pacman.commands]
 | 
					  [managers.pacman.commands]
 | 
				
			||||||
@@ -124,4 +127,50 @@ rootCommand = "sudo"
 | 
				
			|||||||
    file-search = "-Fx"
 | 
					    file-search = "-Fx"
 | 
				
			||||||
    interactive = ""
 | 
					    interactive = ""
 | 
				
			||||||
  [managers.yay.shortcuts]
 | 
					  [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"
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user