slp/main.go

121 lines
2.0 KiB
Go
Raw Normal View History

2017-08-31 00:50:03 +00:00
package main
import (
"fmt"
"os"
"strings"
2017-08-31 19:31:16 +00:00
"github.com/fatih/color"
"github.com/Zac-Garby/plp/args"
2017-08-31 10:55:23 +00:00
"github.com/Zac-Garby/plp/operators"
)
2017-08-31 00:50:03 +00:00
func main() {
arg := os.Args
if len(arg) <= 1 {
2017-08-31 11:12:41 +00:00
fmt.Println(` < plp - Pluto Package Manager >
2017-08-31 20:23:55 +00:00
plp +<package> installs a package
plp -<package> removes a package
plp ^<package> updates a package
plp list lists all available packages
plp 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
2017-08-31 22:41:57 +00:00
fmt.Printf(" - ")
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
}
2017-08-31 22:41:57 +00:00
fmt.Print(" ")
color.Green("done")
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()
var (
green = color.New(color.FgGreen, color.Bold)
cyan = color.New(color.FgCyan, color.Bold)
red = color.New(color.FgRed, color.Bold)
)
printStat(green, "INSTALLED", installs)
printStat(red, "REMOVED", removes)
printStat(cyan, "UPDATED", updates)
}
func printStat(colour *color.Color, prefix string, pkgs []string) {
count := len(pkgs)
2017-08-31 19:31:16 +00:00
if count > 0 {
2017-08-31 19:31:45 +00:00
colour.Printf("%10s ", 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
}