slp/main.go

148 lines
3.2 KiB
Go

package main
import (
"fmt"
"os"
"strings"
"git.rawtext.club/slope-lang/slp/operators"
)
const (
helptext string = `slp - slope package manager
slp docs [module] # open a module's readme in $PAGER
slp gen # creates new module dir/skeleton
slp help # print usage information
slp install [module...] # installs module(s)
slp installed # lists all installed packages
slp list # lists all available packages
slp remove [module...] # removes module(s)
slp search [term...] # searches for modules
slp show [module...] # shows details module(s)
slp update [module...] # updates module(s)
`
)
func main() {
arg := os.Args
if len(arg) <= 1 {
fmt.Println(helptext)
return
}
if arg[1] == "--help" || arg[1] == "-h" || arg[1] == "help" {
fmt.Println(helptext)
return
}
switch arg[1] {
case "docs":
if len(arg) < 3 {
fmt.Println("The 'docs' command requires a module name as an argument: `slp docs [module]`")
}
fmt.Printf("Retrieving docs for %q", arg[2])
err := operators.ReadDocs(arg[2])
if err != nil {
fmt.Println(" \033[31m- done\033[0m")
fmt.Println(err)
os.Exit(1)
}
fmt.Print(" \033[32m- done\033[0m\n")
case "list":
err := operators.List()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
case "installed":
err := operators.ShowInstalled()
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 arg[2:] {
fmt.Printf("installing %s", mod)
err := operators.Install(mod)
if err != nil {
fmt.Println(err)
continue
}
installs = append(installs, mod)
}
fmt.Print(" \033[32m- done\033[0m\n\n")
printStat("\033[1;32m", "INSTALLED", installs)
case "remove":
removes := make([]string, 0, 5)
for _, mod := range arg[2:] {
fmt.Printf("removing %s", mod)
err := operators.Remove(mod)
if err != nil {
fmt.Println(err)
continue
}
removes = append(removes, mod)
}
fmt.Print(" \033[32m- done\033[0m\n\n")
printStat("\033[1;31m", "REMOVED", removes)
case "update":
updates := make([]string, 0, 5)
for _, mod := range arg[2:] {
fmt.Printf("updating %s", mod)
err := operators.Update(mod)
if err != nil {
fmt.Println(err)
continue
}
updates = append(updates, mod)
}
fmt.Print(" \033[32m- done\033[0m\n\n")
printStat("\033[1;36m", "UPDATED", updates)
case "search":
for _, term := range arg[2:] {
fmt.Printf("searching for %q\n\n", term)
err := operators.Search(term)
if err != nil {
fmt.Println(err)
continue
}
}
case "show":
for _, mod := range arg[2:] {
fmt.Printf("showing details for %q\n\n", mod)
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, ", "))
}
}