slp/main.go

112 lines
1.9 KiB
Go
Raw Normal View History

2017-08-31 00:50:03 +00:00
package main
import (
"fmt"
"os"
"strings"
2021-08-26 21:31:43 +00:00
"git.rawtext.club/slope-lang/slp/args"
"git.rawtext.club/slope-lang/slp/operators"
)
2017-08-31 00:50:03 +00:00
func main() {
arg := os.Args
if len(arg) <= 1 {
2021-08-26 21:31:43 +00:00
fmt.Println(` < slp - slope package manager >
2021-08-26 21:31:43 +00:00
slp +<package> installs a package
slp -<package> removes a package
slp ^<package> updates a package
2017-08-31 20:23:55 +00:00
2021-08-26 21:31:43 +00:00
slp list lists all available packages
slp gen creates a new package
2017-08-31 11:12:41 +00:00
`)
return
}
2017-08-31 12:00:45 +00:00
if arg[1] == "list" {
err := operators.List()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return
}
2017-08-31 20:23:55 +00:00
if arg[1] == "gen" {
err := operators.Generate()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return
}
2017-08-31 19:31:16 +00:00
ops, err := args.Parse(arg[1:])
2017-08-31 00:50:03 +00:00
if err != nil {
fmt.Println(err)
return
}
var (
installs []string
removes []string
updates []string
)
2017-08-31 10:55:23 +00:00
for _, op := range ops {
var err error
2021-08-26 21:31:43 +00:00
fmt.Print(" - ")
2017-08-31 22:41:57 +00:00
2017-08-31 10:55:23 +00:00
switch op.Type {
case args.INSTALL:
2017-08-31 22:41:57 +00:00
fmt.Printf("installing %s", op.Package)
2017-08-31 10:55:23 +00:00
err = operators.Install(op.Package)
installs = append(installs, op.Package)
2017-08-31 10:55:23 +00:00
case args.REMOVE:
2017-08-31 22:41:57 +00:00
fmt.Printf("removing %s", op.Package)
2017-08-31 10:55:23 +00:00
err = operators.Remove(op.Package)
removes = append(removes, op.Package)
2017-08-31 11:00:27 +00:00
case args.UPDATE:
2017-08-31 22:41:57 +00:00
fmt.Printf("updating %s", op.Package)
2017-08-31 11:00:27 +00:00
err = operators.Update(op.Package)
updates = append(updates, op.Package)
2017-08-31 10:55:23 +00:00
}
2021-08-26 21:31:43 +00:00
fmt.Print(" \033[32mdone\033[0m")
2017-08-31 22:41:57 +00:00
2017-08-31 10:55:23 +00:00
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
2017-08-31 19:31:16 +00:00
fmt.Println()
2021-08-26 21:31:43 +00:00
printStat("\033[1;32m", "INSTALLED", installs)
printStat("\033[1;31m", "REMOVED", removes)
printStat("\033[1;36m", "UPDATED", updates)
2017-08-31 19:31:16 +00:00
}
2021-08-26 21:31:43 +00:00
func printStat(color, prefix string, pkgs []string) {
count := len(pkgs)
2017-08-31 19:31:16 +00:00
if count > 0 {
2021-08-26 21:31:43 +00:00
fmt.Printf("%s%10s\033[0m", color, prefix)
2017-08-31 19:31:16 +00:00
if count > 1 {
fmt.Printf("%d packages", count)
} else {
fmt.Printf("1 package")
}
fmt.Printf(" (%s)\n", strings.Join(pkgs, ", "))
2017-08-31 19:31:16 +00:00
}
2017-08-31 00:50:03 +00:00
}