Revamp config, Gofmt project, and clean up unneeded code
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -2,4 +2,5 @@ pak
 | 
			
		||||
pak-linux-x86_64
 | 
			
		||||
pak-linux-aarch64
 | 
			
		||||
pak-macos
 | 
			
		||||
pak-new.toml
 | 
			
		||||
.idea
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										82
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										82
									
								
								Makefile
									
									
									
									
									
								
							@@ -2,86 +2,32 @@ GOBUILD ?= go build
 | 
			
		||||
 | 
			
		||||
pak: main.go
 | 
			
		||||
	$(GOBUILD)
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
installbinonly: pak
 | 
			
		||||
	install -Dm755 pak $(DESTDIR)/usr/bin/pak
 | 
			
		||||
	mkdir -p $(DESTDIR)/etc/pak.d
 | 
			
		||||
 | 
			
		||||
aptinstall: plugins/apt/pak.cfg pak
 | 
			
		||||
	install -Dm644 plugins/apt/pak.cfg $(DESTDIR)/etc/pak.cfg
 | 
			
		||||
	mkdir -p $(DESTDIR)/etc/pak.d
 | 
			
		||||
	install -Dm644 plugins/snap/pak.cfg $(DESTDIR)/etc/pak.d/snap.cfg
 | 
			
		||||
	install -Dm644 plugins/yay/pak.cfg $(DESTDIR)/etc/pak.d/yay.cfg
 | 
			
		||||
	install -Dm644 plugins/zypper/pak.cfg $(DESTDIR)/etc/pak.d/zypper.cfg
 | 
			
		||||
	install -Dm644 plugins/pacman/pak.cfg $(DESTDIR)/etc/pak.d/pacman.cfg
 | 
			
		||||
	install -Dm644 plugins/brew/pak.cfg $(DESTDIR)/etc/pak.d/brew.cfg
 | 
			
		||||
	install -Dm644 plugins/aptitude/pak.cfg $(DESTDIR)/etc/pak.d/aptitude.cfg
 | 
			
		||||
	install -Dm755 pak $(DESTDIR)/usr/bin/pak
 | 
			
		||||
aptinstall: install-config.sh pak.toml pak
 | 
			
		||||
	install -Dm644 pak.toml $(DESTDIR)/etc/pak.toml
 | 
			
		||||
	bash install-config.sh apt $(DESTDIR)
 | 
			
		||||
 | 
			
		||||
snapinstall: plugins/snap/pak.cfg pak
 | 
			
		||||
	install -Dm644 plugins/snap/pak.cfg $(DESTDIR)/etc/pak.cfg
 | 
			
		||||
	mkdir -p $(DESTDIR)/etc/pak.d
 | 
			
		||||
	install -Dm644 plugins/apt/pak.cfg $(DESTDIR)/etc/pak.d/apt.cfg
 | 
			
		||||
	install -Dm644 plugins/yay/pak.cfg $(DESTDIR)/etc/pak.d/yay.cfg
 | 
			
		||||
	install -Dm644 plugins/zypper/pak.cfg $(DESTDIR)/etc/pak.d/zypper.cfg
 | 
			
		||||
	install -Dm644 plugins/pacman/pak.cfg $(DESTDIR)/etc/pak.d/pacman.cfg
 | 
			
		||||
	install -Dm644 plugins/brew/pak.cfg $(DESTDIR)/etc/pak.d/brew.cfg
 | 
			
		||||
	install -Dm644 plugins/aptitude/pak.cfg $(DESTDIR)/etc/pak.d/aptitude.cfg
 | 
			
		||||
snapinstall: install-config.sh pak.toml pak
 | 
			
		||||
	install -Dm755 pak $(DESTDIR)/usr/bin/pak
 | 
			
		||||
	bash install-config.sh snap $(DESTDIR)
 | 
			
		||||
 | 
			
		||||
yayinstall: plugins/yay/pak.cfg pak
 | 
			
		||||
	install -Dm644 plugins/yay/pak.cfg $(DESTDIR)/etc/pak.cfg
 | 
			
		||||
	mkdir -p $(DESTDIR)/etc/pak.d
 | 
			
		||||
	install -Dm644 plugins/snap/pak.cfg $(DESTDIR)/etc/pak.d/snap.cfg
 | 
			
		||||
	install -Dm644 plugins/apt/pak.cfg $(DESTDIR)/etc/pak.d/apt.cfg
 | 
			
		||||
	install -Dm644 plugins/zypper/pak.cfg $(DESTDIR)/etc/pak.d/zypper.cfg
 | 
			
		||||
	install -Dm644 plugins/pacman/pak.cfg $(DESTDIR)/etc/pak.d/pacman.cfg
 | 
			
		||||
	install -Dm644 plugins/brew/pak.cfg $(DESTDIR)/etc/pak.d/brew.cfg
 | 
			
		||||
	install -Dm644 plugins/aptitude/pak.cfg $(DESTDIR)/etc/pak.d/aptitude.cfg
 | 
			
		||||
yayinstall: install-config.sh pak.toml pak
 | 
			
		||||
	install -Dm755 pak $(DESTDIR)/usr/bin/pak
 | 
			
		||||
	bash install-config.sh yay $(DESTDIR)
 | 
			
		||||
 | 
			
		||||
pacinstall: plugins/pacman/pak.cfg pak
 | 
			
		||||
	install -Dm644 plugins/pacman/pak.cfg $(DESTDIR)/etc/pak.cfg
 | 
			
		||||
	mkdir -p $(DESTDIR)/etc/pak.d
 | 
			
		||||
	install -Dm644 plugins/snap/pak.cfg $(DESTDIR)/etc/pak.d/snap.cfg
 | 
			
		||||
	install -Dm644 plugins/yay/pak.cfg $(DESTDIR)/etc/pak.d/yay.cfg
 | 
			
		||||
	install -Dm644 plugins/zypper/pak.cfg $(DESTDIR)/etc/pak.d/zypper.cfg
 | 
			
		||||
	install -Dm644 plugins/apt/pak.cfg $(DESTDIR)/etc/pak.d/apt.cfg
 | 
			
		||||
	install -Dm644 plugins/brew/pak.cfg $(DESTDIR)/etc/pak.d/brew.cfg
 | 
			
		||||
	install -Dm644 plugins/aptitude/pak.cfg $(DESTDIR)/etc/pak.d/aptitude.cfg
 | 
			
		||||
pacinstall: install-config.sh pak.toml pak
 | 
			
		||||
	install -Dm755 pak $(DESTDIR)/usr/bin/pak
 | 
			
		||||
	bash install-config.sh pacman $(DESTDIR)
 | 
			
		||||
 | 
			
		||||
aptitude: plugins/aptitude/pak.cfg pak
 | 
			
		||||
	install -Dm644 plugins/aptitude/pak.cfg $(DESTDIR)/etc/pak.cfg
 | 
			
		||||
	mkdir -p $(DESTDIR)/etc/pak.d
 | 
			
		||||
	install -Dm644 plugins/snap/pak.cfg $(DESTDIR)/etc/pak.d/snap.cfg
 | 
			
		||||
	install -Dm644 plugins/yay/pak.cfg $(DESTDIR)/etc/pak.d/yay.cfg
 | 
			
		||||
	install -Dm644 plugins/zypper/pak.cfg $(DESTDIR)/etc/pak.d/zypper.cfg
 | 
			
		||||
	install -Dm644 plugins/pacman/pak.cfg $(DESTDIR)/etc/pak.d/pacman.cfg
 | 
			
		||||
	install -Dm644 plugins/brew/pak.cfg $(DESTDIR)/etc/pak.d/brew.cfg
 | 
			
		||||
	install -Dm644 plugins/apt/pak.cfg $(DESTDIR)/etc/pak.d/apt.cfg
 | 
			
		||||
aptitude: install-config.sh pak.toml pak
 | 
			
		||||
	install -Dm755 pak $(DESTDIR)/usr/bin/pak
 | 
			
		||||
	bash install-config.sh aptitude $(DESTDIR)
 | 
			
		||||
 | 
			
		||||
brewinstall: plugins/brew/pak.cfg pak
 | 
			
		||||
	install -Dm644 plugins/brew/pak.cfg $(DESTDIR)/etc/pak.cfg
 | 
			
		||||
	mkdir -p $(DESTDIR)/etc/pak.d
 | 
			
		||||
	install -Dm644 plugins/snap/pak.cfg $(DESTDIR)/etc/pak.d/snap.cfg
 | 
			
		||||
	install -Dm644 plugins/yay/pak.cfg $(DESTDIR)/etc/pak.d/yay.cfg
 | 
			
		||||
	install -Dm644 plugins/zypper/pak.cfg $(DESTDIR)/etc/pak.d/zypper.cfg
 | 
			
		||||
	install -Dm644 plugins/pacman/pak.cfg $(DESTDIR)/etc/pak.d/pacman.cfg
 | 
			
		||||
	install -Dm644 plugins/apt/pak.cfg $(DESTDIR)/etc/pak.d/apt.cfg
 | 
			
		||||
	install -Dm644 plugins/aptitude/pak.cfg $(DESTDIR)/etc/pak.d/aptitude.cfg
 | 
			
		||||
	install -Dm755 pak $(DESTDIR)/usr/bin/pak
 | 
			
		||||
 | 
			
		||||
zyppinstall: plugins/zypper/pak.cfg pak
 | 
			
		||||
	install -Dm644 plugins/zypper/pak.cfg $(DESTDIR)/etc/pak.cfg
 | 
			
		||||
	mkdir -p $(DESTDIR)/etc/pak.d
 | 
			
		||||
	install -Dm644 plugins/snap/pak.cfg $(DESTDIR)/etc/pak.d/snap.cfg
 | 
			
		||||
	install -Dm644 plugins/yay/pak.cfg $(DESTDIR)/etc/pak.d/yay.cfg
 | 
			
		||||
	install -Dm644 plugins/apt/pak.cfg $(DESTDIR)/etc/pak.d/apt.cfg
 | 
			
		||||
	install -Dm644 plugins/pacman/pak.cfg $(DESTDIR)/etc/pak.d/pacman.cfg
 | 
			
		||||
	install -Dm644 plugins/brew/pak.cfg $(DESTDIR)/etc/pak.d/brew.cfg
 | 
			
		||||
	install -Dm644 plugins/aptitude/pak.cfg $(DESTDIR)/etc/pak.d/aptitude.cfg
 | 
			
		||||
brewinstall: install-config.sh pak.toml pak
 | 
			
		||||
	install -Dm755 pak $(DESTDIR)/usr/bin/pak
 | 
			
		||||
	bash install-config.sh brew $(DESTDIR)
 | 
			
		||||
 | 
			
		||||
.PHONY: pak $(MAKECMDGOALS)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										39
									
								
								config.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								config.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/pelletier/go-toml"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"log"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Config contains the root of the TOML config file
 | 
			
		||||
type Config struct {
 | 
			
		||||
	ActiveManager string
 | 
			
		||||
	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
 | 
			
		||||
	Shortcuts map[string]string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Create new Config{} with values from file path given
 | 
			
		||||
func NewConfig(path string) Config {
 | 
			
		||||
	// Read file at path
 | 
			
		||||
	data, err := ioutil.ReadFile(path)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatalln(err)
 | 
			
		||||
	}
 | 
			
		||||
	// Create new Config{}
 | 
			
		||||
	cfg := Config{}
 | 
			
		||||
	// Unmarshal TOML in config
 | 
			
		||||
	err = toml.Unmarshal(data, &cfg)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatalln(err)
 | 
			
		||||
	}
 | 
			
		||||
	// Return config
 | 
			
		||||
	return cfg
 | 
			
		||||
}
 | 
			
		||||
@@ -41,7 +41,6 @@ func Jaro(a, b string) float64 {
 | 
			
		||||
	return ((matches / la) + (matches / lb) + (matches-transposes)/matches) / 3.0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func JaroWinkler(a, b string, boostThreshold float64, prefixSize int) float64 {
 | 
			
		||||
	j := Jaro(a, b)
 | 
			
		||||
 | 
			
		||||
@@ -59,4 +58,4 @@ func JaroWinkler(a, b string, boostThreshold float64, prefixSize int) float64 {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return j + 0.1*prefixMatch*(1.0-j)
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										55
									
								
								extra.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								extra.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,55 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
// Check if slice contains string
 | 
			
		||||
func Contains(slice []string, val string) bool {
 | 
			
		||||
	for _, item := range slice {
 | 
			
		||||
		if item == val {
 | 
			
		||||
			return true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Max(array []float64) float64 {
 | 
			
		||||
	var max = array[0]
 | 
			
		||||
	var min = array[0]
 | 
			
		||||
	for _, value := range array {
 | 
			
		||||
		if max < value {
 | 
			
		||||
			max = value
 | 
			
		||||
		}
 | 
			
		||||
		if min > value {
 | 
			
		||||
			min = value
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return max
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Get key from map given value
 | 
			
		||||
func GetKey(inMap map[string]string, val string) string {
 | 
			
		||||
	// For every key/value pair in map
 | 
			
		||||
	for key, value := range inMap {
 | 
			
		||||
		// If value found
 | 
			
		||||
		if value == val {
 | 
			
		||||
			// Return key
 | 
			
		||||
			return key
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	// If fails, return empty string
 | 
			
		||||
	return ""
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Get slice of float64 given map[string]float64
 | 
			
		||||
func GetValuesDist(inMap map[string]float64) []float64 {
 | 
			
		||||
	// Make new slice with set capacity
 | 
			
		||||
	values := make([]float64, len(inMap))
 | 
			
		||||
	// Set index to 0
 | 
			
		||||
	index := 0
 | 
			
		||||
	for _, value := range inMap {
 | 
			
		||||
		// Set index of slice to value
 | 
			
		||||
		values[index] = value
 | 
			
		||||
		// Increment index
 | 
			
		||||
		index++
 | 
			
		||||
	}
 | 
			
		||||
	// Return completed slice
 | 
			
		||||
	return values
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										5
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								go.mod
									
									
									
									
									
								
							@@ -2,4 +2,7 @@ module pak
 | 
			
		||||
 | 
			
		||||
go 1.15
 | 
			
		||||
 | 
			
		||||
require github.com/pelletier/go-toml v1.8.2-0.20201124181426-2e01f733df54
 | 
			
		||||
require (
 | 
			
		||||
	github.com/pelletier/go-toml v1.8.2-0.20201124181426-2e01f733df54
 | 
			
		||||
	github.com/spf13/pflag v1.0.5
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										3
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								go.sum
									
									
									
									
									
								
							@@ -1,3 +1,6 @@
 | 
			
		||||
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/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
 | 
			
		||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								install-config.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								install-config.sh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
			
		||||
sed "s/activeManager = \"\"/activeManager = \"$1\"/" pak.toml > pak-new.toml
 | 
			
		||||
install -Dm644 pak-new.toml "$2"/etc/pak.toml
 | 
			
		||||
							
								
								
									
										113
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										113
									
								
								main.go
									
									
									
									
									
								
							@@ -19,36 +19,34 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"flag"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/pelletier/go-toml"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	flag "github.com/spf13/pflag"
 | 
			
		||||
	"log"
 | 
			
		||||
	"os"
 | 
			
		||||
	"os/exec"
 | 
			
		||||
	"os/user"
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main()  {
 | 
			
		||||
	// Put all arguments into a variable
 | 
			
		||||
	args := os.Args[1:]
 | 
			
		||||
func main() {
 | 
			
		||||
 | 
			
		||||
	// Check which currentUser is running command
 | 
			
		||||
	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)")
 | 
			
		||||
	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.BoolVar(&rootCheckBypass,"root", false, "Bypass root check")
 | 
			
		||||
	flag.BoolVar(&rootCheckBypass,"r", false, "Bypass root check (shorthand)")
 | 
			
		||||
	flag.BoolVarP(&rootCheckBypass, "root", "r", false, "Bypass root check")
 | 
			
		||||
 | 
			
		||||
	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()
 | 
			
		||||
 | 
			
		||||
@@ -63,108 +61,101 @@ func main()  {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Create regex to remove all flags using ";;;" as it is uncommon to use in command line
 | 
			
		||||
	flagRegex := regexp.MustCompile(`(?m)(;;;|^)-+[^;]*;;;`)
 | 
			
		||||
	// Join args into string
 | 
			
		||||
	argsStr := strings.Join(args, ";;;")
 | 
			
		||||
	// Remove all flags from join args
 | 
			
		||||
	argsStr = flagRegex.ReplaceAllString(argsStr, "$1")
 | 
			
		||||
	// Separate args back into slice
 | 
			
		||||
	args = strings.Split(argsStr, ";;;")
 | 
			
		||||
	// Get arguments without flags
 | 
			
		||||
	args := flag.Args()
 | 
			
		||||
 | 
			
		||||
	// Define variables for config file location, and override state boolean
 | 
			
		||||
	var configFileLocation string
 | 
			
		||||
	var isOverridden bool
 | 
			
		||||
	// Get PAK_MGR_OVERRIDE environment variable
 | 
			
		||||
	override := os.Getenv("PAK_MGR_OVERRIDE")
 | 
			
		||||
 | 
			
		||||
	// Read /etc/pak.toml into new Config{}
 | 
			
		||||
	config := NewConfig("/etc/pak.toml")
 | 
			
		||||
 | 
			
		||||
	// If override is set
 | 
			
		||||
	if override != "" {
 | 
			
		||||
		// Set configFileLocation to /etc/pak.d/{override}.cfg
 | 
			
		||||
		configFileLocation = "/etc/pak.d/" + override + ".cfg"
 | 
			
		||||
	if packageManagerOverride != "" {
 | 
			
		||||
		// Set active package manager to override
 | 
			
		||||
		config.ActiveManager = packageManagerOverride
 | 
			
		||||
		// Set override state to true
 | 
			
		||||
		isOverridden = true
 | 
			
		||||
	} else {
 | 
			
		||||
		// Otherwise, set configFileLocation to default config
 | 
			
		||||
		configFileLocation = "/etc/pak.cfg"
 | 
			
		||||
		// Set override state to false
 | 
			
		||||
		isOverridden = false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Parse config file removing all comments and empty lines
 | 
			
		||||
	config, err := ioutil.ReadFile(configFileLocation)
 | 
			
		||||
	parsedConfig, _ := toml.Load(string(config))
 | 
			
		||||
 | 
			
		||||
	// Set first line of config to variable
 | 
			
		||||
	packageManagerCommand := parsedConfig.Get("packageManager").(string)
 | 
			
		||||
	//fmt.Println(packageManagerCommand) //DEBUG
 | 
			
		||||
 | 
			
		||||
	// Parse list of commands in config line 2 and set to variable as array
 | 
			
		||||
	commands := InterfaceToString(parsedConfig.Get("commands").([]interface{}))
 | 
			
		||||
	commands := config.Managers[config.ActiveManager].Commands
 | 
			
		||||
	//fmt.Println(commands) //DEBUG
 | 
			
		||||
 | 
			
		||||
	// Set the root option in config line 3 to a variable
 | 
			
		||||
	useRoot := parsedConfig.Get("useRoot").(bool)
 | 
			
		||||
	useRoot := config.Managers[config.ActiveManager].UseRoot
 | 
			
		||||
	//fmt.Println(useRoot) //DEBUG
 | 
			
		||||
 | 
			
		||||
	// Set command to use to invoke root at config line 4 to a variable
 | 
			
		||||
	rootCommand := parsedConfig.Get("rootCommand").(string)
 | 
			
		||||
	rootCommand := config.RootCommand
 | 
			
		||||
	//fmt.Println(rootCommand) //DEBUG
 | 
			
		||||
 | 
			
		||||
	// Parse list of shortcuts in config and line 5 set to variable as an array
 | 
			
		||||
	shortcuts := InterfaceToString(parsedConfig.Get("shortcuts").([]interface{}))
 | 
			
		||||
	shortcuts := config.Managers[config.ActiveManager].Shortcuts
 | 
			
		||||
	//fmt.Println(shortcuts) //DEBUG
 | 
			
		||||
 | 
			
		||||
	// Parse list of shortcuts in config line 6 and set to variable as array
 | 
			
		||||
	shortcutMappings := InterfaceToString(parsedConfig.Get("shortcutMappings").([]interface{}))
 | 
			
		||||
	//fmt.Println(shortcutMappings) //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, useRoot, rootCommand, commands, shortcuts, shortcutMappings, isOverridden)
 | 
			
		||||
		printHelpMessage(config.ActiveManager, useRoot, rootCommand, commands, shortcuts, isOverridden)
 | 
			
		||||
		os.Exit(0)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Create distance slice to store JaroWinkler distance values
 | 
			
		||||
	var distance []float64
 | 
			
		||||
	distance := map[string]float64{}
 | 
			
		||||
	// Appends JaroWinkler distance between each available command and the first argument to an array
 | 
			
		||||
	for _,command := range commands {
 | 
			
		||||
		distance = append(distance, JaroWinkler(command, args[0], 1, 0))
 | 
			
		||||
	for command := range commands {
 | 
			
		||||
		distance[command] = JaroWinkler(command, args[0], 1, 0)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Deals with shortcuts
 | 
			
		||||
	for index, shortcut := range shortcuts {
 | 
			
		||||
	for shortcut, mapping := range shortcuts {
 | 
			
		||||
		// If the first argument is a shortcut and similarTo does not already contain its mapping, append it
 | 
			
		||||
		if args[0] == shortcut && !Contains(similarTo, shortcutMappings[index]) {
 | 
			
		||||
			similarTo = append(similarTo, shortcutMappings[index])
 | 
			
		||||
		if args[0] == shortcut && !Contains(similarTo, mapping) {
 | 
			
		||||
			similarTo = append(similarTo, mapping)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Compares each distance to the max of the distance slice and appends the closest command to similarTo
 | 
			
		||||
	for index, element := range distance {
 | 
			
		||||
	for command, cmdDist := range distance {
 | 
			
		||||
		// If current element is the closest to the first argument
 | 
			
		||||
		if element == Max(distance) {
 | 
			
		||||
		if cmdDist == Max(GetValuesDist(distance)) {
 | 
			
		||||
			// Append command at same index as distance to similarTo
 | 
			
		||||
			similarTo = append(similarTo, commands[index])
 | 
			
		||||
			similarTo = append(similarTo, commands[command])
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 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 "" } }
 | 
			
		||||
	printOverridden := func() string {
 | 
			
		||||
		if isOverridden {
 | 
			
		||||
			return "(overridden)"
 | 
			
		||||
		} else {
 | 
			
		||||
			return ""
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	// 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(GetKey(commands, similarTo[0])), "using", strings.Title(config.ActiveManager), printOverridden())
 | 
			
		||||
	// 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 useRoot { 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]}...)
 | 
			
		||||
	cmdArr = append(cmdArr, config.ActiveManager, similarTo[0])
 | 
			
		||||
	// If greater than 2 arguments, append them to cmdArr
 | 
			
		||||
	if len(args) >= 2 { cmdArr = append(cmdArr, strings.Join(args[1:], " ")) }
 | 
			
		||||
	if len(args) >= 2 {
 | 
			
		||||
		cmdArr = append(cmdArr, strings.Join(args[1:], " "))
 | 
			
		||||
	}
 | 
			
		||||
	// Create space separated string from cmdArr
 | 
			
		||||
	cmdStr := strings.Join(cmdArr, " ")
 | 
			
		||||
	// Instantiate exec.Command object with command sh, flag -c, and cmdStr
 | 
			
		||||
@@ -180,4 +171,4 @@ func main()  {
 | 
			
		||||
		fmt.Println("Error received from child process")
 | 
			
		||||
		log.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										127
									
								
								pak.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										127
									
								
								pak.toml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,127 @@
 | 
			
		||||
activeManager = ""
 | 
			
		||||
rootCommand = "sudo"
 | 
			
		||||
 | 
			
		||||
[managers]
 | 
			
		||||
 | 
			
		||||
  [managers.apt]
 | 
			
		||||
    useRoot = true
 | 
			
		||||
  [managers.apt.commands]
 | 
			
		||||
    install = "install"
 | 
			
		||||
    remove = "remove"
 | 
			
		||||
    search = "search"
 | 
			
		||||
    list = "list"
 | 
			
		||||
    refresh = "update"
 | 
			
		||||
    upgrade = "upgrade"
 | 
			
		||||
    download = "download"
 | 
			
		||||
    clean = "clean"
 | 
			
		||||
  [managers.apt.shortcuts]
 | 
			
		||||
    arm = "autoremove"
 | 
			
		||||
 | 
			
		||||
  [managers.aptitude]
 | 
			
		||||
    useRoot = true
 | 
			
		||||
  [managers.aptitude.commands]
 | 
			
		||||
    install = "install"
 | 
			
		||||
    remove = "remove"
 | 
			
		||||
    download = "download"
 | 
			
		||||
    refresh = "update"
 | 
			
		||||
    upgrade = "upgrade"
 | 
			
		||||
    full-upgrade = "full-upgrade"
 | 
			
		||||
    safe-upgrade = "safe-upgrade"
 | 
			
		||||
    clean = "clean"
 | 
			
		||||
    hold = "hold"
 | 
			
		||||
    unhold = "unhold"
 | 
			
		||||
    mark-auto = "markauto"
 | 
			
		||||
    unmark-auto = "unmarkauto"
 | 
			
		||||
    why = "why"
 | 
			
		||||
    whynot = "whynot"
 | 
			
		||||
    reinstall = "reinstall"
 | 
			
		||||
    search = "search"
 | 
			
		||||
    show = "show"
 | 
			
		||||
    list = "search '~i'"
 | 
			
		||||
    changelog = "changelog"
 | 
			
		||||
 | 
			
		||||
  [managers.brew]
 | 
			
		||||
    useRoot = false
 | 
			
		||||
  [managers.brew.commands]
 | 
			
		||||
    install = "install"
 | 
			
		||||
    remove = "remove"
 | 
			
		||||
    reinstall = "reinstall"
 | 
			
		||||
    cask-install = "cask install"
 | 
			
		||||
    cask-remove = "cask remove"
 | 
			
		||||
    cask-reinstall = "cask reinstall"
 | 
			
		||||
    cask = "cask"
 | 
			
		||||
    list = "list"
 | 
			
		||||
    refresh = "update"
 | 
			
		||||
    upgrade = "upgrade"
 | 
			
		||||
    search = "search"
 | 
			
		||||
    doctor = "doctor"
 | 
			
		||||
    edit = "edit"
 | 
			
		||||
 | 
			
		||||
  [managers.snap]
 | 
			
		||||
    useRoot = true
 | 
			
		||||
  [managers.snap.commands]
 | 
			
		||||
    install = "install"
 | 
			
		||||
    remove = "remove"
 | 
			
		||||
    upgrade = "refresh"
 | 
			
		||||
    downgrade = "revert"
 | 
			
		||||
    run = "run"
 | 
			
		||||
    download = "download"
 | 
			
		||||
 | 
			
		||||
  [managers.npm]
 | 
			
		||||
    useRoot = false
 | 
			
		||||
  [managers.npm.commands]
 | 
			
		||||
    whoami = "whoami"
 | 
			
		||||
    view = "view"
 | 
			
		||||
    version = "version"
 | 
			
		||||
    update = "update"
 | 
			
		||||
    test = "test"
 | 
			
		||||
    stop = "stop"
 | 
			
		||||
    start = "start"
 | 
			
		||||
    publish = "publish"
 | 
			
		||||
    prune = "prune"
 | 
			
		||||
    deprecate = "deprecate"
 | 
			
		||||
    doctor = "doctor"
 | 
			
		||||
    logout = "logout"
 | 
			
		||||
    login = "login"
 | 
			
		||||
    list = "list"
 | 
			
		||||
    link = "link"
 | 
			
		||||
    rebuild = "rebuild"
 | 
			
		||||
    repo = "repo"
 | 
			
		||||
    search = "search"
 | 
			
		||||
    docs = "docs"
 | 
			
		||||
    create = "create"
 | 
			
		||||
    remove = "uninstall"
 | 
			
		||||
    install = "install"
 | 
			
		||||
 | 
			
		||||
  [managers.pacman]
 | 
			
		||||
    useRoot = true
 | 
			
		||||
  [managers.pacman.commands]
 | 
			
		||||
    install = "-S"
 | 
			
		||||
    remove = "-R"
 | 
			
		||||
    upgrade = "-Syu"
 | 
			
		||||
    update = "-Su"
 | 
			
		||||
    refresh = "-Sy"
 | 
			
		||||
    autoremove = "-Rss $(pacman -Qdtq)"
 | 
			
		||||
    search = "-Ss"
 | 
			
		||||
    list = "-Qi"
 | 
			
		||||
    file-refresh = "-Fy"
 | 
			
		||||
    file-search = "-Fx"
 | 
			
		||||
  [managers.pacman.shortcuts]
 | 
			
		||||
    arm = "-Rss $(pacman -Qdtq)"
 | 
			
		||||
 | 
			
		||||
  [managers.yay]
 | 
			
		||||
    useRoot = false
 | 
			
		||||
  [managers.yay.commands]
 | 
			
		||||
    install = "-S"
 | 
			
		||||
    remove = "-R"
 | 
			
		||||
    upgrade = "-Syu"
 | 
			
		||||
    update = "-Su"
 | 
			
		||||
    refresh = "-Sy"
 | 
			
		||||
    autoremove = "-Rss $(yay -Qdtq)"
 | 
			
		||||
    search = "-Ss"
 | 
			
		||||
    list = "-Qi"
 | 
			
		||||
    file-refresh = "-Fy"
 | 
			
		||||
    file-search = "-Fx"
 | 
			
		||||
    interactive = ""
 | 
			
		||||
  [managers.yay.shortcuts]
 | 
			
		||||
  arm = "-Rss $(yay -Qdtq)"
 | 
			
		||||
@@ -1,7 +0,0 @@
 | 
			
		||||
# 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"]
 | 
			
		||||
@@ -1,7 +0,0 @@
 | 
			
		||||
# 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"]
 | 
			
		||||
@@ -1,7 +0,0 @@
 | 
			
		||||
# 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"]
 | 
			
		||||
@@ -1,7 +0,0 @@
 | 
			
		||||
# 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"]
 | 
			
		||||
@@ -1,7 +0,0 @@
 | 
			
		||||
# 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"]
 | 
			
		||||
@@ -1,7 +0,0 @@
 | 
			
		||||
# 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"]
 | 
			
		||||
@@ -1,7 +0,0 @@
 | 
			
		||||
# 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"]
 | 
			
		||||
							
								
								
									
										49
									
								
								slices.go
									
									
									
									
									
								
							
							
						
						
									
										49
									
								
								slices.go
									
									
									
									
									
								
							@@ -1,49 +0,0 @@
 | 
			
		||||
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:]...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Check if slice contains string
 | 
			
		||||
func Contains(slice []string, val string) bool {
 | 
			
		||||
	for _, item := range slice {
 | 
			
		||||
		if item == val {
 | 
			
		||||
			return true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Max(array []float64) float64 {
 | 
			
		||||
	var max = array[0]
 | 
			
		||||
	var min = array[0]
 | 
			
		||||
	for _, value := range array {
 | 
			
		||||
		if max < value {
 | 
			
		||||
			max = value
 | 
			
		||||
		}
 | 
			
		||||
		if min > value {
 | 
			
		||||
			min = value
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return max
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Find(slice []string, val string) int {
 | 
			
		||||
	for i, item := range slice {
 | 
			
		||||
		if item == val {
 | 
			
		||||
			return i
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return -1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func InterfaceToString(interfaceSlice []interface{}) []string {
 | 
			
		||||
	returnedSlice := make([]string, len(interfaceSlice))
 | 
			
		||||
	for index, element := range interfaceSlice {
 | 
			
		||||
		returnedSlice[index] = fmt.Sprint(element)
 | 
			
		||||
	}
 | 
			
		||||
	return returnedSlice
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										25
									
								
								usage.go
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								usage.go
									
									
									
									
									
								
							@@ -2,28 +2,39 @@ package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Print help screen
 | 
			
		||||
func printHelpMessage(packageManagerCommand string, useRoot bool, rootCommand string, commands []string, shortcuts []string, shortcutMappings []string, isOverridden bool) {
 | 
			
		||||
func printHelpMessage(packageManagerCommand string, useRoot bool, rootCommand string, commands map[string]string, shortcuts map[string]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 useRoot { fmt.Println("Using root with command:", rootCommand) } else { fmt.Println("Not using root") }
 | 
			
		||||
	if isOverridden {
 | 
			
		||||
		fmt.Println(" (overridden)")
 | 
			
		||||
	} else {
 | 
			
		||||
		fmt.Print("\n")
 | 
			
		||||
	}
 | 
			
		||||
	if useRoot {
 | 
			
		||||
		fmt.Println("Using root with command:", rootCommand)
 | 
			
		||||
	} else {
 | 
			
		||||
		fmt.Println("Not using root")
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println()
 | 
			
		||||
	fmt.Println("Usage: pak <command> [package]")
 | 
			
		||||
	fmt.Println("Example: pak in hello")
 | 
			
		||||
	fmt.Println()
 | 
			
		||||
	fmt.Println("The available commands are:")
 | 
			
		||||
	fmt.Println(strings.Join(commands, "\n"))
 | 
			
		||||
	for command, mapping := range commands {
 | 
			
		||||
		fmt.Println(command+":", mapping)
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println()
 | 
			
		||||
	fmt.Println("The available shortcuts are:")
 | 
			
		||||
	for index, element := range shortcuts { fmt.Println(element + ":", shortcutMappings[index]) }
 | 
			
		||||
	for shortcut, mapping := range shortcuts {
 | 
			
		||||
		fmt.Println(shortcut+":", mapping)
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println()
 | 
			
		||||
	fmt.Println("The available flags are:")
 | 
			
		||||
	fmt.Println("--help, -h: Shows this help screen")
 | 
			
		||||
	fmt.Println("--root, -r: Bypasses root user check")
 | 
			
		||||
	fmt.Println()
 | 
			
		||||
	fmt.Println("Pak uses a string distance algorithm, so `pak in` is valid as is `pak inst` or `pak install`")
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user