package main import ( "flag" "fmt" "os" "strings" "go.jolheiser.com/eget/disk" "go.jolheiser.com/eget/forge" "go.jolheiser.com/eget/meta" ) var Version = "develop" func main() { fs := flag.NewFlagSet("eget", flag.ExitOnError) fs.Usage = func() { fmt.Fprintln(fs.Output(), "eget ") 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") syncFlag := fs.Bool("sync", false, "Sync packages") if err := fs.Parse(os.Args[1:]); err != nil { fs.Usage() return } if *syncFlag { if err := sync(); err != nil { panic(err) } return } if fs.NArg() < 1 { fs.Usage() return } var f forge.Forger var err error m, err := meta.Read() if err != nil { panic(err) } uri := fs.Arg(0) if *updateFlag && strings.Index(uri, "/") == -1 { if pkg, ok := m.Packages[uri]; ok { uri = pkg.Repo } } 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.LatestRelease(f) if err != nil { panic(err) } if *updateFlag { if strings.EqualFold(asset.Version, m.Packages[name].Version) { 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, uri, asset.Version); 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] } func sync() error { m, err := meta.Read() if err != nil { return err } var f forge.Forger for name, pkg := range m.Packages { fmt.Printf("syncing %q\n", name) if err := os.RemoveAll(disk.Path(name)); err != nil { return err } f, err = forge.NewGitea(pkg.Repo) if strings.HasPrefix(pkg.Repo, "github") { f, err = forge.NewGitHub(pkg.Repo) } if err != nil { return err } asset, err := forge.TagRelease(f, pkg.Version) if err != nil { return err } if err := disk.Install(name, asset); err != nil { return err } } return nil }