From 0bffa3e2a4b62d3f20c28b1b68e5b54bba514575 Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Mon, 30 Nov 2020 15:27:29 -0800 Subject: [PATCH] Switch to TOML config [skip ci] --- go.mod | 5 +++++ go.sum | 3 +++ main.go | 35 ++++++++++------------------------- plugins/apt/pak.cfg | 20 +++++++------------- plugins/aptitude/pak.cfg | 20 +++++++------------- plugins/brew/pak.cfg | 20 +++++++------------- plugins/pacman/pak.cfg | 19 +++++++------------ plugins/snap/pak.cfg | 20 +++++++------------- plugins/yay/pak.cfg | 19 +++++++------------ plugins/zypper/pak.cfg | 20 +++++++------------- slices.go | 10 ++++++++++ usage.go | 4 ++-- 12 files changed, 79 insertions(+), 116 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c2471b9 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module pak + +go 1.15 + +require github.com/pelletier/go-toml v1.8.2-0.20201124181426-2e01f733df54 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..99ffab8 --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +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= diff --git a/main.go b/main.go index ba26980..37cabd8 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,7 @@ package main import ( "flag" "fmt" + "github.com/pelletier/go-toml" "io/ioutil" "log" "os" @@ -91,54 +92,38 @@ func main() { // Parse config file removing all comments and empty lines config, err := ioutil.ReadFile(configFileLocation) - if err != nil { log.Fatal(err) } - commentRegex := regexp.MustCompile(`#.*`) - emptyLineRegex := regexp.MustCompile(`(?m)^\s*\n`) - parsedConfig := commentRegex.ReplaceAllString(string(config), "") - parsedConfig = emptyLineRegex.ReplaceAllString(parsedConfig, "") - - cfg := strings.Split(parsedConfig, "\n") - //fmt.Println(cfg) //DEBUG + parsedConfig, _ := toml.Load(string(config)) // Set first line of config to variable - packageManagerCommand := cfg[0] + packageManagerCommand := parsedConfig.Get("packageManager").(string) //fmt.Println(packageManagerCommand) //DEBUG // Parse list of commands in config line 2 and set to variable as array - commands := strings.Split(cfg[1], ",") + commands := InterfaceToString(parsedConfig.Get("commands").([]interface{})) //fmt.Println(commands) //DEBUG // Set the root option in config line 3 to a variable - useRoot := cfg[2] + useRoot := parsedConfig.Get("useRoot").(bool) //fmt.Println(useRoot) //DEBUG // Set command to use to invoke root at config line 4 to a variable - rootCommand := cfg[3] + rootCommand := parsedConfig.Get("rootCommand").(string) //fmt.Println(rootCommand) //DEBUG // Parse list of shortcuts in config and line 5 set to variable as an array - shortcuts := strings.Split(cfg[4], ",") + shortcuts := InterfaceToString(parsedConfig.Get("shortcuts").([]interface{})) //fmt.Println(shortcuts) //DEBUG // Parse list of shortcuts in config line 6 and set to variable as array - shortcutMappings := strings.Split(cfg[5], ",") + shortcutMappings := InterfaceToString(parsedConfig.Get("shortcutMappings").([]interface{})) //fmt.Println(shortcutMappings) //DEBUG - // Check if config file allows root and set boolean to a variable - var useRootBool bool - if useRoot == "yes" { - useRootBool = true - } else if useRoot == "no" { - useRootBool = false - } - //fmt.Println(useRootBool) //DEBUG - // Create similar to slice to put all matched commands into var similarTo []string // Displays help message if no arguments provided or -h/--help is passed if len(args) == 0 || helpFlagGiven || Contains(args, "help") { - printHelpMessage(packageManagerCommand, useRootBool, rootCommand, commands, shortcuts, shortcutMappings, isOverridden) + printHelpMessage(packageManagerCommand, useRoot, rootCommand, commands, shortcuts, shortcutMappings, isOverridden) os.Exit(0) } @@ -175,7 +160,7 @@ func main() { // Run package manager with the proper arguments passed if more than one argument exists var cmdArr []string // If root is to be used, append it to cmdArr - if useRootBool { cmdArr = append(cmdArr, rootCommand) } + if useRoot { cmdArr = append(cmdArr, rootCommand) } // Create slice with all commands and arguments for the package manager cmdArr = append(cmdArr, []string{packageManagerCommand, similarTo[0]}...) // If greater than 2 arguments, append them to cmdArr diff --git a/plugins/apt/pak.cfg b/plugins/apt/pak.cfg index 4f29a4d..1ae8433 100644 --- a/plugins/apt/pak.cfg +++ b/plugins/apt/pak.cfg @@ -1,13 +1,7 @@ -# Write the name of the package manager in all lowercase below -apt -# Write a comma separated list of commands from the manager below -install,remove,update,upgrade,search,download -# Write "yes" or "no" depending on whether you want to use sudo -yes -# Write command to use for root -sudo -# Write a comma separated list of shortcuts below -rm,inst -# Write a comma separated list of shortcut mappings from the manager below -remove,install - +# Config for the pak package manager wrapper +packageManager = "apt" +commands = ["install", "remove", "update", "upgrade", "search", "download"] +useRoot = true +rootCommand = "sudo" +shortcuts = ["rm", "inst"] +shortcutMappings = ["remove", "install"] \ No newline at end of file diff --git a/plugins/aptitude/pak.cfg b/plugins/aptitude/pak.cfg index 233ec55..2cabb97 100644 --- a/plugins/aptitude/pak.cfg +++ b/plugins/aptitude/pak.cfg @@ -1,13 +1,7 @@ -# Write the name of the package manager in all lowercase below -aptitude -# Write a comma separated list of commands from the manager below -install,remove,download,update,upgrade,full-upgrade,safe-upgrade,clean,hold,unhold,markauto,unmarkauto,why,whynot,reinstall,search,show,changelog -# Write "yes" or "no" depending on whether you want to use sudo -yes -# Write command to use for root -sudo -# Write a comma separated list of shortcuts below -rm,inst -# Write a comma separated list of shortcut mappings from the manager below -remove,install - +# Config for the pak package manager wrapper +packageManager = "aptitude" +commands = ["install", "remove", "download", "update", "upgrade", "full-upgrade", "safe-upgrade", "clean", "hold", "unhold", "markauto", "unmarkauto", "why", "whynot", "reinstall", "search", "show", "changelog"] +useRoot = true +rootCommand = "sudo" +shortcuts = ["rm", "inst"] +shortcutMappings = ["remove", "install"] \ No newline at end of file diff --git a/plugins/brew/pak.cfg b/plugins/brew/pak.cfg index 56c1286..217d162 100644 --- a/plugins/brew/pak.cfg +++ b/plugins/brew/pak.cfg @@ -1,13 +1,7 @@ -# Write the name of the package manager in all lowercase below -brew -# Write a comma separated list of commands from the manager below -install,reinstall,remove,upgrade,update,search,cask,list,doctor,edit -# Write "yes" or "no" depending on whether you want to use sudo -no -# Write command to use for root -sudo -# Write a comma separated list of shortcuts below -rm,src,dl,ci,cr,cre -# Write a comma separated list of shortcut mappings from the manager below -remove,search,download,cask install,cask remove,cask reinstall - +# Config for the pak package manager wrapper +packageManager = "brew" +commands = ["install", "reinstall", "remove", "upgrade", "update", "search", "cask", "list", "doctor", "edit"] +useRoot = false +rootCommand = "sudo" +shortcuts = ["rm", "src", "dl", "ci", "cr", "cre"] +shortcutMappings = ["remove", "search", "download", "cask install", "cask remove", "cask reinstall"] \ No newline at end of file diff --git a/plugins/pacman/pak.cfg b/plugins/pacman/pak.cfg index 79166e0..1c1b0db 100644 --- a/plugins/pacman/pak.cfg +++ b/plugins/pacman/pak.cfg @@ -1,12 +1,7 @@ -# Write the name of the package manager in all lowercase below -aptman -# Write a comma separated list of commands from the manager below -install,remove,upgrade,update,refresh,autoremove,search,filesearch,list -# Write "yes" or "no" depending on whether you want to use sudo -yes -# Write command to use for root -sudo -# Write a comma separated list of shortcuts below -rm,arm,ls -# Write a comma separated list of shortcut mappings from the manager below -remove,autoremove,list +# Config for the pak package manager wrapper +packageManager = "aptman" +commands = ["install", "remove", "upgrade", "update", "refresh", "autoremove", "search", "filesearch", "list"] +useRoot = true +rootCommand = "sudo" +shortcuts = ["rm", "arm", "ls"] +shortcutMappings = ["remove", "autoremove", "list"] \ No newline at end of file diff --git a/plugins/snap/pak.cfg b/plugins/snap/pak.cfg index d89cbfa..8873aa8 100644 --- a/plugins/snap/pak.cfg +++ b/plugins/snap/pak.cfg @@ -1,13 +1,7 @@ -# Write the name of the package manager in all lowercase below -snap -# Write a comma separated list of commands from the manager below -install,remove,refresh,revert,run,download -# Write "yes" or "no" depending on whether you want to use sudo -yes -# Write command to use for root -sudo -# Write a comma separated list of shortcuts below -rm,inst -# Write a comma separated list of shortcut mappings from the manager below -remove,install - +# Config for the pak package manager wrapper +packageManager = "snap" +commands = ["install", "remove", "refresh", "revert", "run", "download"] +useRoot = true +rootCommand = "sudo" +shortcuts = ["rm", "inst"] +shortcutMappings = ["remove", "install"] \ No newline at end of file diff --git a/plugins/yay/pak.cfg b/plugins/yay/pak.cfg index 15a8a62..8496ef3 100644 --- a/plugins/yay/pak.cfg +++ b/plugins/yay/pak.cfg @@ -1,12 +1,7 @@ -# Write the name of the package manager in all lowercase below -aptyay -# Write a comma separated list of commands from the manager below -install,remove,upgrade,update,refresh,autoremove,search,filesearch,list,interactive -# Write "yes" or "no" depending on whether you want to use sudo -no -# Write command to use for root -sudo -# Write a comma separated list of shortcuts below -rm,arm,ls,int -# Write a comma separated list of shortcut mappings from the manager below -remove,autoremove,list,interactive +# Config for the pak package manager wrapper +packageManager = "aptyay" +commands = ["install", "remove", "upgrade", "update", "refresh", "autoremove", "search", "filesearch", "list", "interactive"] +useRoot = false +rootCommand = "sudo" +shortcuts = ["rm", "arm", "ls", "int"] +shortcutMappings = ["remove", "autoremove", "list", "interactive"] \ No newline at end of file diff --git a/plugins/zypper/pak.cfg b/plugins/zypper/pak.cfg index 569921c..8098822 100644 --- a/plugins/zypper/pak.cfg +++ b/plugins/zypper/pak.cfg @@ -1,13 +1,7 @@ -# Write the name of the package manager in all lowercase below -zypper -# Write a comma separated list of commands from the manager below -install,remove,refresh,search,update,addrepo -# Write "yes" or "no" depending on whether you want to use sudo -yes -# Write command to use for root -sudo -# Write a comma separated list of shortcuts below -rm,ar,inst -# Write a comma separated list of shortcut mappings from the manager below -remove,addrepo,install - +# Config for the pak package manager wrapper +packageManager = "zypper" +commands = ["install", "remove", "refresh", "search", "update", "addrepo"] +useRoot = true +rootCommand = "sudo" +shortcuts = ["rm", "ar", "inst"] +shortcutMappings = ["remove", "addrepo", "install"] \ No newline at end of file diff --git a/slices.go b/slices.go index 0e35773..31e6222 100644 --- a/slices.go +++ b/slices.go @@ -1,5 +1,7 @@ package main +import "fmt" + // Remove an element at an index from a slice func removeAtIndex(s []string, index int) []string { return append(s[:index], s[index+1:]...) @@ -36,4 +38,12 @@ func Find(slice []string, val string) int { } } return -1 +} + +func InterfaceToString(interfaceSlice []interface{}) []string { + returnedSlice := make([]string, len(interfaceSlice)) + for index, element := range interfaceSlice { + returnedSlice[index] = fmt.Sprint(element) + } + return returnedSlice } \ No newline at end of file diff --git a/usage.go b/usage.go index 2d6fde3..8460a5f 100644 --- a/usage.go +++ b/usage.go @@ -6,11 +6,11 @@ import ( ) // Print help screen -func printHelpMessage(packageManagerCommand string, useRootBool bool, rootCommand string, commands []string, shortcuts []string, shortcutMappings []string, isOverridden bool) { +func printHelpMessage(packageManagerCommand string, useRoot bool, rootCommand string, commands []string, shortcuts []string, shortcutMappings []string, isOverridden bool) { fmt.Println("Arsen Musayelyan's Package Manager Wrapper") fmt.Print("Current package manager is: ", packageManagerCommand) if isOverridden { fmt.Println(" (overridden)") } else { fmt.Print("\n") } - if useRootBool { fmt.Println("Using root with command:", rootCommand) } else { fmt.Println("Not using root") } + if useRoot { fmt.Println("Using root with command:", rootCommand) } else { fmt.Println("Not using root") } fmt.Println() fmt.Println("Usage: pak [package]") fmt.Println("Example: pak in hello")