2023-01-12 05:11:26 +00:00
|
|
|
package main
|
|
|
|
|
2023-01-13 23:04:41 +00:00
|
|
|
import (
|
2023-01-14 06:02:25 +00:00
|
|
|
"flag"
|
2023-01-13 23:04:41 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.jolheiser.com/eget/disk"
|
|
|
|
"go.jolheiser.com/eget/forge"
|
2023-01-14 06:02:25 +00:00
|
|
|
"go.jolheiser.com/eget/meta"
|
2023-01-13 23:04:41 +00:00
|
|
|
)
|
|
|
|
|
2023-01-12 05:11:26 +00:00
|
|
|
var Version = "develop"
|
|
|
|
|
|
|
|
func main() {
|
2023-01-14 06:02:25 +00:00
|
|
|
fs := flag.NewFlagSet("eget", flag.ExitOnError)
|
|
|
|
fs.Usage = func() {
|
|
|
|
fmt.Fprintln(fs.Output(), "eget <package>")
|
|
|
|
fs.PrintDefaults()
|
|
|
|
}
|
2023-01-18 18:27:34 +00:00
|
|
|
versionFlag := fs.Bool("version", false, "Show version and exit")
|
|
|
|
fs.BoolVar(versionFlag, "v", *versionFlag, "--version")
|
2023-01-14 06:02:25 +00:00
|
|
|
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")
|
2023-01-15 03:31:58 +00:00
|
|
|
syncFlag := fs.Bool("sync", false, "Sync packages")
|
2023-01-14 06:02:25 +00:00
|
|
|
if err := fs.Parse(os.Args[1:]); err != nil {
|
|
|
|
fs.Usage()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-18 18:27:34 +00:00
|
|
|
if *versionFlag {
|
|
|
|
fmt.Printf("eget %s\n", Version)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-15 03:31:58 +00:00
|
|
|
if *syncFlag {
|
|
|
|
if err := sync(); err != nil {
|
2023-01-14 06:02:25 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if fs.NArg() < 1 {
|
|
|
|
fs.Usage()
|
2023-01-13 23:04:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f forge.Forger
|
|
|
|
var err error
|
2023-01-15 03:36:47 +00:00
|
|
|
m, err := meta.Read()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-01-14 06:02:25 +00:00
|
|
|
uri := fs.Arg(0)
|
2023-01-13 23:04:41 +00:00
|
|
|
|
2023-01-15 03:36:47 +00:00
|
|
|
if *updateFlag && strings.Index(uri, "/") == -1 {
|
|
|
|
if pkg, ok := m.Packages[uri]; ok {
|
|
|
|
uri = pkg.Repo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 23:04:41 +00:00
|
|
|
f, err = forge.NewGitea(uri)
|
|
|
|
if strings.HasPrefix(uri, "github") {
|
|
|
|
f, err = forge.NewGitHub(uri)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-01-14 06:02:25 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-15 03:31:58 +00:00
|
|
|
asset, err := forge.LatestRelease(f)
|
2023-01-13 23:04:41 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-01-14 06:02:25 +00:00
|
|
|
if *updateFlag {
|
2023-01-15 03:31:58 +00:00
|
|
|
if strings.EqualFold(asset.Version, m.Packages[name].Version) {
|
2023-01-14 06:02:25 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-01-15 03:31:58 +00:00
|
|
|
if err := meta.Upsert(name, uri, asset.Version); err != nil {
|
2023-01-13 23:04:41 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2023-01-14 06:02:25 +00:00
|
|
|
|
|
|
|
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]
|
2023-01-12 05:11:26 +00:00
|
|
|
}
|
2023-01-15 03:31:58 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|