170 lines
4.4 KiB
Go
170 lines
4.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"git.rawtext.club/slope-lang/slp/operators"
|
|
)
|
|
|
|
const (
|
|
globalText string = " (\033[36mglobal\033[0m)"
|
|
globalPath string = "/usr/local/lib/slope/modules/"
|
|
banner string = "\033[32;1mslp - slope package manager\033[0m"
|
|
helptext string = `
|
|
slp docs [[-g]] [module] # open a module's readme in $PAGER
|
|
slp gen # creates new module dir/skeleton
|
|
slp help # print usage information
|
|
slp install [[-g]] [module...] # installs module(s)
|
|
slp installed [[-g]] # lists all installed packages
|
|
slp list # lists all available packages
|
|
slp remove [[-g]] [module...] # removes module(s)
|
|
slp search [term...] # searches for modules
|
|
slp show [module...] # shows details for module(s)
|
|
slp update [[-g]] [module...] # updates module(s)
|
|
|
|
Operations that accept a -g flag will attempt to install a module systemwide (this may require root access). A --global flag may be passed in lieu of a -g flag if desired for clarity.
|
|
|
|
The install location for global modules is: /usr/lib/slope/modules
|
|
|
|
Globals modules must be dealt with separately from local modules and cannot be combined in a single command.`
|
|
)
|
|
|
|
func main() {
|
|
arg := os.Args
|
|
if len(arg) <= 1 || arg[1] == "--help" || arg[1] == "-h" || arg[1] == "help" {
|
|
printHelp()
|
|
return
|
|
}
|
|
|
|
global := false
|
|
globalTxt := ""
|
|
modules := arg[2:]
|
|
if len(arg) > 3 && (arg[2] == "-g" || arg[2] == "--global") {
|
|
checkGlobalEnv()
|
|
global = true
|
|
modules = arg[3:]
|
|
globalTxt = operators.GlobalPath
|
|
}
|
|
|
|
switch arg[1] {
|
|
case "docs":
|
|
if len(arg) < 3 {
|
|
fmt.Println("The 'docs' command requires a module name as an argument: `slp docs [module]`")
|
|
}
|
|
err := operators.ReadDocs(modules[0], global)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
case "list":
|
|
err := operators.List()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
case "installed":
|
|
err := operators.ShowInstalled(global, globalTxt)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
case "gen":
|
|
err := operators.Generate()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
case "install":
|
|
installs := make([]string, 0, 5)
|
|
for _, mod := range modules {
|
|
fmt.Printf("installing %s%s\n", mod, globalTxt)
|
|
err := operators.Install(mod, global)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
fmt.Println()
|
|
continue
|
|
}
|
|
installs = append(installs, mod)
|
|
fmt.Print(" \033[2m└\033[0m \033[32mdone\033[0m\n\n")
|
|
}
|
|
printStat("\033[1;32m", "INSTALLED", installs)
|
|
case "remove":
|
|
removes := make([]string, 0, 5)
|
|
for _, mod := range modules {
|
|
fmt.Printf("removing %s%s\n", mod, globalTxt)
|
|
err := operators.Remove(mod, global)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
fmt.Println()
|
|
continue
|
|
}
|
|
removes = append(removes, mod)
|
|
fmt.Print(" \033[2m└\033[0m \033[32mdone\033[0m\n\n")
|
|
}
|
|
printStat("\033[1;31m", "REMOVED", removes)
|
|
case "update":
|
|
updates := make([]string, 0, 5)
|
|
for _, mod := range modules {
|
|
fmt.Printf("updating %s%s\n", mod, globalTxt)
|
|
err := operators.Update(mod, global)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
fmt.Println()
|
|
continue
|
|
}
|
|
updates = append(updates, mod)
|
|
fmt.Print(" \033[2m└\033[0m \033[32mdone\033[0m\n\n")
|
|
}
|
|
printStat("\033[1;36m", "UPDATED", updates)
|
|
case "search":
|
|
for _, term := range modules {
|
|
fmt.Printf("searching for %q\n\n", term)
|
|
err := operators.Search(term)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
continue
|
|
}
|
|
}
|
|
case "show":
|
|
for _, mod := range modules {
|
|
err := operators.Show(mod)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
continue
|
|
}
|
|
}
|
|
default:
|
|
fmt.Fprintf(os.Stderr, "Unknown command %s\n\n%s\n", arg[1], helptext)
|
|
}
|
|
}
|
|
|
|
func printStat(color, prefix string, pkgs []string) {
|
|
count := len(pkgs)
|
|
|
|
if count > 0 {
|
|
fmt.Printf("%s%10s\033[0m ", color, prefix)
|
|
|
|
if count > 1 {
|
|
fmt.Printf("%d packages", count)
|
|
} else {
|
|
fmt.Printf("1 package")
|
|
}
|
|
|
|
fmt.Printf(" (%s)\n", strings.Join(pkgs, ", "))
|
|
}
|
|
}
|
|
|
|
func checkGlobalEnv() {
|
|
err := os.MkdirAll(globalPath, 0755)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "\033[1;31mError:\033[0m Unable to access global environment.\n\nAre you root? Using a \033[3m-g\033[0m or \033[3m--global\033[0m flag accesses the /usr/local/lib heirarchy,\nwhich will require root access on most systems\n")
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func printHelp() {
|
|
fmt.Printf("%s\n%s\n", banner, helptext)
|
|
}
|