109 lines
1.9 KiB
Go
109 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"go.jolheiser.com/eget/disk"
|
|
"go.jolheiser.com/eget/forge"
|
|
"go.jolheiser.com/eget/meta"
|
|
"go.jolheiser.com/eget/shell"
|
|
)
|
|
|
|
var Version = "develop"
|
|
|
|
func main() {
|
|
fs := flag.NewFlagSet("eget", flag.ExitOnError)
|
|
fs.Usage = func() {
|
|
fmt.Fprintln(fs.Output(), "eget <package>")
|
|
fs.PrintDefaults()
|
|
}
|
|
updateFlag := fs.Bool("update", false, "Update package")
|
|
fs.BoolVar(updateFlag, "u", *updateFlag, "--update")
|
|
deleteFlag := flag.Bool("delete", false, "Delete package")
|
|
fs.BoolVar(deleteFlag, "d", *deleteFlag, "--delete")
|
|
nuFlag := fs.Bool("nu", false, "Write out Nu environment")
|
|
if err := fs.Parse(os.Args[1:]); err != nil {
|
|
fs.Usage()
|
|
return
|
|
}
|
|
|
|
if *nuFlag {
|
|
nu, err := shell.Nu()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println(nu)
|
|
return
|
|
}
|
|
|
|
if fs.NArg() < 1 {
|
|
fs.Usage()
|
|
return
|
|
}
|
|
|
|
var f forge.Forger
|
|
var err error
|
|
uri := fs.Arg(0)
|
|
|
|
f, err = forge.NewGitea(uri)
|
|
if strings.HasPrefix(uri, "github") {
|
|
f, err = forge.NewGitHub(uri)
|
|
}
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
name := uriName(uri)
|
|
if *deleteFlag {
|
|
if err := os.RemoveAll(disk.Path(name)); err != nil {
|
|
panic(err)
|
|
}
|
|
if err := meta.Remove(name); err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Printf("removed %q\n", name)
|
|
return
|
|
}
|
|
|
|
asset, err := forge.Latest(f)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if *updateFlag {
|
|
m, err := meta.Read()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
if strings.EqualFold(asset.Name, m.Packages[name]) {
|
|
fmt.Printf("%q is up-to-date\n", name)
|
|
return
|
|
}
|
|
if err := os.RemoveAll(disk.Path(name)); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
if err := disk.Install(name, asset); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if err := meta.Upsert(name, asset.Name); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
verb := "installed"
|
|
if *updateFlag {
|
|
verb = "updated"
|
|
}
|
|
fmt.Printf("%s %q\n", verb, uri)
|
|
}
|
|
|
|
func uriName(uri string) string {
|
|
parts := strings.FieldsFunc(uri, func(r rune) bool { return r == '/' })
|
|
return parts[len(parts)-1]
|
|
}
|