Compare commits
No commits in common. "0bffa3e2a4b62d3f20c28b1b68e5b54bba514575" and "34208e43991f5a1849d92b65f4b5675aad716dbf" have entirely different histories.
0bffa3e2a4
...
34208e4399
5
go.mod
5
go.mod
@ -1,5 +0,0 @@
|
|||||||
module pak
|
|
||||||
|
|
||||||
go 1.15
|
|
||||||
|
|
||||||
require github.com/pelletier/go-toml v1.8.2-0.20201124181426-2e01f733df54
|
|
3
go.sum
3
go.sum
@ -1,3 +0,0 @@
|
|||||||
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=
|
|
75
main.go
75
main.go
@ -19,9 +19,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/pelletier/go-toml"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@ -39,38 +37,20 @@ func main() {
|
|||||||
currentUser, err := user.Current()
|
currentUser, err := user.Current()
|
||||||
if err != nil { log.Fatal(err) }
|
if err != nil { log.Fatal(err) }
|
||||||
|
|
||||||
// Create help flags
|
|
||||||
var helpFlagGiven bool
|
|
||||||
flag.BoolVar(&helpFlagGiven, "help", false, "Show help screen")
|
|
||||||
flag.BoolVar(&helpFlagGiven, "h", false, "Show help screen (shorthand)")
|
|
||||||
|
|
||||||
// Check to make sure root is not being used unless -r/--root specified
|
// Check to make sure root is not being used unless -r/--root specified
|
||||||
var rootCheckBypass bool
|
if !Contains(args, "-r") && !Contains(args, "--root") {
|
||||||
// Create --root and -r flags for root check bypass
|
|
||||||
flag.BoolVar(&rootCheckBypass,"root", false, "Bypass root check")
|
|
||||||
flag.BoolVar(&rootCheckBypass,"r", false, "Bypass root check (shorthand)")
|
|
||||||
// Parse arguments for flags
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
// If flag not given
|
|
||||||
if !rootCheckBypass {
|
|
||||||
// If current user is root
|
|
||||||
if strings.Contains(currentUser.Username, "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("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.")
|
fmt.Println("If you would like to bypass this, run this command with -r or --root.")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if Contains(args, "-r") {
|
||||||
|
args = removeAtIndex(args, Find(args, "-r"))
|
||||||
|
} else if Contains(args, "--root") {
|
||||||
|
args = removeAtIndex(args, Find(args, "--root"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create regex to remove all flags using ";;;" as it is uncommon to use in command line
|
|
||||||
flagRegex := regexp.MustCompile(`-+[^;]*;;;`)
|
|
||||||
// Join args into string
|
|
||||||
argsStr := strings.Join(args, ";;;")
|
|
||||||
// Remove all flags from join args
|
|
||||||
argsStr = flagRegex.ReplaceAllString(argsStr, "")
|
|
||||||
// Separate args back into slice
|
|
||||||
args = strings.Split(argsStr, ";;;")
|
|
||||||
|
|
||||||
// Define variables for config file location, and override state boolean
|
// Define variables for config file location, and override state boolean
|
||||||
var configFileLocation string
|
var configFileLocation string
|
||||||
@ -92,38 +72,54 @@ func main() {
|
|||||||
|
|
||||||
// Parse config file removing all comments and empty lines
|
// Parse config file removing all comments and empty lines
|
||||||
config, err := ioutil.ReadFile(configFileLocation)
|
config, err := ioutil.ReadFile(configFileLocation)
|
||||||
parsedConfig, _ := toml.Load(string(config))
|
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
|
||||||
|
|
||||||
// Set first line of config to variable
|
// Set first line of config to variable
|
||||||
packageManagerCommand := parsedConfig.Get("packageManager").(string)
|
packageManagerCommand := cfg[0]
|
||||||
//fmt.Println(packageManagerCommand) //DEBUG
|
//fmt.Println(packageManagerCommand) //DEBUG
|
||||||
|
|
||||||
// Parse list of commands in config line 2 and set to variable as array
|
// Parse list of commands in config line 2 and set to variable as array
|
||||||
commands := InterfaceToString(parsedConfig.Get("commands").([]interface{}))
|
commands := strings.Split(cfg[1], ",")
|
||||||
//fmt.Println(commands) //DEBUG
|
//fmt.Println(commands) //DEBUG
|
||||||
|
|
||||||
// Set the root option in config line 3 to a variable
|
// Set the root option in config line 3 to a variable
|
||||||
useRoot := parsedConfig.Get("useRoot").(bool)
|
useRoot := cfg[2]
|
||||||
//fmt.Println(useRoot) //DEBUG
|
//fmt.Println(useRoot) //DEBUG
|
||||||
|
|
||||||
// Set command to use to invoke root at config line 4 to a variable
|
// Set command to use to invoke root at config line 4 to a variable
|
||||||
rootCommand := parsedConfig.Get("rootCommand").(string)
|
rootCommand := cfg[3]
|
||||||
//fmt.Println(rootCommand) //DEBUG
|
//fmt.Println(rootCommand) //DEBUG
|
||||||
|
|
||||||
// Parse list of shortcuts in config and line 5 set to variable as an array
|
// Parse list of shortcuts in config and line 5 set to variable as an array
|
||||||
shortcuts := InterfaceToString(parsedConfig.Get("shortcuts").([]interface{}))
|
shortcuts := strings.Split(cfg[4], ",")
|
||||||
//fmt.Println(shortcuts) //DEBUG
|
//fmt.Println(shortcuts) //DEBUG
|
||||||
|
|
||||||
// Parse list of shortcuts in config line 6 and set to variable as array
|
// Parse list of shortcuts in config line 6 and set to variable as array
|
||||||
shortcutMappings := InterfaceToString(parsedConfig.Get("shortcutMappings").([]interface{}))
|
shortcutMappings := strings.Split(cfg[5], ",")
|
||||||
//fmt.Println(shortcutMappings) //DEBUG
|
//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
|
// Create similar to slice to put all matched commands into
|
||||||
var similarTo []string
|
var 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 || Contains(args, "-h") || Contains(args, "--help") || Contains(args, "help") {
|
||||||
printHelpMessage(packageManagerCommand, useRoot, rootCommand, commands, shortcuts, shortcutMappings, isOverridden)
|
printHelpMessage(packageManagerCommand, useRootBool, rootCommand, commands, shortcuts, shortcutMappings, isOverridden)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,14 +149,13 @@ 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 { log.Fatalln("This command does not match any known commands or shortcuts") }
|
if len(similarTo) == 0 { log.Fatalln("This command does not match any known commands or shortcuts") }
|
||||||
// Anonymous function to decide whether to print (overridden)
|
|
||||||
printOverridden := func() string { if isOverridden { return "(overridden)" } else { 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(similarTo[0]), "using", strings.Title(packageManagerCommand), printOverridden())
|
fmt.Println("Running:", strings.Title(similarTo[0]), "using", strings.Title(packageManagerCommand))
|
||||||
// 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
|
||||||
if useRoot { cmdArr = append(cmdArr, rootCommand) }
|
if useRootBool { cmdArr = append(cmdArr, rootCommand) }
|
||||||
// Create slice with all commands and arguments for the package manager
|
// Create slice with all commands and arguments for the package manager
|
||||||
cmdArr = append(cmdArr, []string{packageManagerCommand, similarTo[0]}...)
|
cmdArr = append(cmdArr, []string{packageManagerCommand, similarTo[0]}...)
|
||||||
// If greater than 2 arguments, append them to cmdArr
|
// If greater than 2 arguments, append them to cmdArr
|
||||||
|
@ -1,7 +1,13 @@
|
|||||||
# Config for the pak package manager wrapper
|
# Write the name of the package manager in all lowercase below
|
||||||
packageManager = "apt"
|
apt
|
||||||
commands = ["install", "remove", "update", "upgrade", "search", "download"]
|
# Write a comma separated list of commands from the manager below
|
||||||
useRoot = true
|
install,remove,update,upgrade,search,download
|
||||||
rootCommand = "sudo"
|
# Write "yes" or "no" depending on whether you want to use sudo
|
||||||
shortcuts = ["rm", "inst"]
|
yes
|
||||||
shortcutMappings = ["remove", "install"]
|
# 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
|
||||||
|
|
||||||
|
@ -1,7 +1,13 @@
|
|||||||
# Config for the pak package manager wrapper
|
# Write the name of the package manager in all lowercase below
|
||||||
packageManager = "aptitude"
|
aptitude
|
||||||
commands = ["install", "remove", "download", "update", "upgrade", "full-upgrade", "safe-upgrade", "clean", "hold", "unhold", "markauto", "unmarkauto", "why", "whynot", "reinstall", "search", "show", "changelog"]
|
# Write a comma separated list of commands from the manager below
|
||||||
useRoot = true
|
install,remove,download,update,upgrade,full-upgrade,safe-upgrade,clean,hold,unhold,markauto,unmarkauto,why,whynot,reinstall,search,show,changelog
|
||||||
rootCommand = "sudo"
|
# Write "yes" or "no" depending on whether you want to use sudo
|
||||||
shortcuts = ["rm", "inst"]
|
yes
|
||||||
shortcutMappings = ["remove", "install"]
|
# 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
|
||||||
|
|
||||||
|
@ -1,7 +1,13 @@
|
|||||||
# Config for the pak package manager wrapper
|
# Write the name of the package manager in all lowercase below
|
||||||
packageManager = "brew"
|
brew
|
||||||
commands = ["install", "reinstall", "remove", "upgrade", "update", "search", "cask", "list", "doctor", "edit"]
|
# Write a comma separated list of commands from the manager below
|
||||||
useRoot = false
|
install,reinstall,remove,upgrade,update,search,cask,list,doctor,edit
|
||||||
rootCommand = "sudo"
|
# Write "yes" or "no" depending on whether you want to use sudo
|
||||||
shortcuts = ["rm", "src", "dl", "ci", "cr", "cre"]
|
no
|
||||||
shortcutMappings = ["remove", "search", "download", "cask install", "cask remove", "cask reinstall"]
|
# 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
|
||||||
|
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
# Config for the pak package manager wrapper
|
# Write the name of the package manager in all lowercase below
|
||||||
packageManager = "aptman"
|
aptman
|
||||||
commands = ["install", "remove", "upgrade", "update", "refresh", "autoremove", "search", "filesearch", "list"]
|
# Write a comma separated list of commands from the manager below
|
||||||
useRoot = true
|
install,remove,upgrade,update,refresh,autoremove,search,filesearch,list
|
||||||
rootCommand = "sudo"
|
# Write "yes" or "no" depending on whether you want to use sudo
|
||||||
shortcuts = ["rm", "arm", "ls"]
|
yes
|
||||||
shortcutMappings = ["remove", "autoremove", "list"]
|
# 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
|
||||||
|
@ -1,7 +1,13 @@
|
|||||||
# Config for the pak package manager wrapper
|
# Write the name of the package manager in all lowercase below
|
||||||
packageManager = "snap"
|
snap
|
||||||
commands = ["install", "remove", "refresh", "revert", "run", "download"]
|
# Write a comma separated list of commands from the manager below
|
||||||
useRoot = true
|
install,remove,refresh,revert,run,download
|
||||||
rootCommand = "sudo"
|
# Write "yes" or "no" depending on whether you want to use sudo
|
||||||
shortcuts = ["rm", "inst"]
|
yes
|
||||||
shortcutMappings = ["remove", "install"]
|
# 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
|
||||||
|
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
# Config for the pak package manager wrapper
|
# Write the name of the package manager in all lowercase below
|
||||||
packageManager = "aptyay"
|
aptyay
|
||||||
commands = ["install", "remove", "upgrade", "update", "refresh", "autoremove", "search", "filesearch", "list", "interactive"]
|
# Write a comma separated list of commands from the manager below
|
||||||
useRoot = false
|
install,remove,upgrade,update,refresh,autoremove,search,filesearch,list,interactive
|
||||||
rootCommand = "sudo"
|
# Write "yes" or "no" depending on whether you want to use sudo
|
||||||
shortcuts = ["rm", "arm", "ls", "int"]
|
no
|
||||||
shortcutMappings = ["remove", "autoremove", "list", "interactive"]
|
# 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
|
||||||
|
@ -1,7 +1,13 @@
|
|||||||
# Config for the pak package manager wrapper
|
# Write the name of the package manager in all lowercase below
|
||||||
packageManager = "zypper"
|
zypper
|
||||||
commands = ["install", "remove", "refresh", "search", "update", "addrepo"]
|
# Write a comma separated list of commands from the manager below
|
||||||
useRoot = true
|
install,remove,refresh,search,update,addrepo
|
||||||
rootCommand = "sudo"
|
# Write "yes" or "no" depending on whether you want to use sudo
|
||||||
shortcuts = ["rm", "ar", "inst"]
|
yes
|
||||||
shortcutMappings = ["remove", "addrepo", "install"]
|
# 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
|
||||||
|
|
||||||
|
10
slices.go
10
slices.go
@ -1,7 +1,5 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
// Remove an element at an index from a slice
|
// Remove an element at an index from a slice
|
||||||
func removeAtIndex(s []string, index int) []string {
|
func removeAtIndex(s []string, index int) []string {
|
||||||
return append(s[:index], s[index+1:]...)
|
return append(s[:index], s[index+1:]...)
|
||||||
@ -39,11 +37,3 @@ func Find(slice []string, val string) int {
|
|||||||
}
|
}
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
func InterfaceToString(interfaceSlice []interface{}) []string {
|
|
||||||
returnedSlice := make([]string, len(interfaceSlice))
|
|
||||||
for index, element := range interfaceSlice {
|
|
||||||
returnedSlice[index] = fmt.Sprint(element)
|
|
||||||
}
|
|
||||||
return returnedSlice
|
|
||||||
}
|
|
4
usage.go
4
usage.go
@ -6,11 +6,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Print help screen
|
// Print help screen
|
||||||
func printHelpMessage(packageManagerCommand string, useRoot bool, rootCommand string, commands []string, shortcuts []string, shortcutMappings []string, isOverridden bool) {
|
func printHelpMessage(packageManagerCommand string, useRootBool bool, rootCommand string, commands []string, shortcuts []string, shortcutMappings []string, 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 { fmt.Println(" (overridden)") } else { fmt.Print("\n") }
|
if isOverridden { fmt.Println(" (overridden)") } else { fmt.Print("\n") }
|
||||||
if useRoot { fmt.Println("Using root with command:", rootCommand) } else { fmt.Println("Not using root") }
|
if useRootBool { fmt.Println("Using root with command:", rootCommand) } else { fmt.Println("Not using root") }
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
fmt.Println("Usage: pak <command> [package]")
|
fmt.Println("Usage: pak <command> [package]")
|
||||||
fmt.Println("Example: pak in hello")
|
fmt.Println("Example: pak in hello")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user