Clean up code

This commit is contained in:
Elara 2021-05-06 10:50:27 -07:00
parent 8480882b32
commit a35f05a13e
2 changed files with 13 additions and 17 deletions

28
main.go
View File

@ -35,11 +35,9 @@ var Log = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
func main() { func main() {
// Create help flag // Create help flag
var helpFlagGiven bool helpFlagGiven := flag.BoolP("help", "h", false, "Show help screen")
flag.BoolVarP(&helpFlagGiven, "help", "h", false, "Show help screen")
// Create package manager override flag // Create package manager override flag
var packageManagerOverride string packageManagerOverride := flag.StringP("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()
@ -64,9 +62,9 @@ func main() {
config := NewConfig("/etc/pak.toml") config := NewConfig("/etc/pak.toml")
// If override is set // If override is set
if packageManagerOverride != "" { if *packageManagerOverride != "" {
// Set active package manager to override // Set active package manager to override
config.ActiveManager = packageManagerOverride config.ActiveManager = *packageManagerOverride
// Set override state to true // Set override state to true
isOverridden = true isOverridden = true
} else { } else {
@ -94,8 +92,8 @@ func main() {
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") {
printHelpMessage(config.ActiveManager, useRoot, rootCommand, commands, shortcuts, isOverridden) printHelpMessage(config.ActiveManager, rootCommand, commands, shortcuts, useRoot, isOverridden)
os.Exit(0) os.Exit(0)
} }
@ -127,16 +125,14 @@ func main() {
if len(similarTo) == 0 { if len(similarTo) == 0 {
Log.Fatal().Msg("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) var overriddenStr string
printOverridden := func() string { if isOverridden {
if isOverridden { overriddenStr = "(overridden)"
return "(overridden)" } else {
} else { overriddenStr = ""
return ""
}
} }
// Print text showing command being run and package manager being used // Print text showing command being run and package manager being used
fmt.Println("Running:", strings.Title(GetKey(commands, similarTo[0])), "using", strings.Title(config.ActiveManager), printOverridden()) fmt.Println("Running:", strings.Title(GetKey(commands, similarTo[0])), "using", strings.Title(config.ActiveManager), overriddenStr)
// Run package manager with the proper arguments passed if more than one argument exists // Run package manager with the proper arguments passed if more than one argument exists
var cmdArr []string var cmdArr []string
// If root is to be used, append it to cmdArr // If root is to be used, append it to cmdArr

View File

@ -5,7 +5,7 @@ import (
) )
// Print help screen // Print help screen
func printHelpMessage(packageManagerCommand string, useRoot bool, rootCommand string, commands map[string]string, shortcuts map[string]string, isOverridden bool) { func printHelpMessage(packageManagerCommand, rootCommand string, commands, shortcuts map[string]string, useRoot, isOverridden bool) {
fmt.Println("Arsen Musayelyan's Package Manager Wrapper") fmt.Println("Arsen Musayelyan's Package Manager Wrapper")
fmt.Print("Current package manager is: ", packageManagerCommand) fmt.Print("Current package manager is: ", packageManagerCommand)
if isOverridden { if isOverridden {