diff options
| author | Fuwn <[email protected]> | 2024-10-13 08:11:14 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-10-13 08:11:14 +0000 |
| commit | 3dd9f302b71fafce9eaaca924017990e8a65f6d6 (patch) | |
| tree | 4b6b610e977a4bb610a219009c9e53d9481065d8 /internal/commands | |
| parent | fix(flake): module vendor hash (diff) | |
| download | yae-3dd9f302b71fafce9eaaca924017990e8a65f6d6.tar.xz yae-3dd9f302b71fafce9eaaca924017990e8a65f6d6.zip | |
refactor(yae): move commands and library to internal package
Diffstat (limited to 'internal/commands')
| -rw-r--r-- | internal/commands/add.go | 70 | ||||
| -rw-r--r-- | internal/commands/drop.go | 24 | ||||
| -rw-r--r-- | internal/commands/init.go | 19 | ||||
| -rw-r--r-- | internal/commands/update.go | 51 |
4 files changed, 164 insertions, 0 deletions
diff --git a/internal/commands/add.go b/internal/commands/add.go new file mode 100644 index 0000000..4ee15ec --- /dev/null +++ b/internal/commands/add.go @@ -0,0 +1,70 @@ +package commands + +import ( + "fmt" + "strings" + + "github.com/Fuwn/yae/internal/yae" + "github.com/urfave/cli/v2" +) + +func Add(sources *yae.Sources) func(c *cli.Context) error { + return func(c *cli.Context) error { + if c.Args().Len() != 2 { + return fmt.Errorf("invalid number of arguments") + } + + if sources.Exists(c.Args().Get(0)) { + return fmt.Errorf("source already exists") + } + + source := yae.Source{ + Unpack: c.Bool("unpack"), + Type: c.String("type"), + } + version := c.String("version") + + if version != "" { + source.URLTemplate = c.Args().Get(1) + source.Version = c.String("version") + + if strings.Contains(source.URLTemplate, "{version}") { + source.URL = strings.ReplaceAll(source.URLTemplate, "{version}", source.Version) + } + } else { + source.URL = c.Args().Get(1) + } + + if source.Type == "git" && c.String("tag-predicate") != "" { + source.TagPredicate = c.String("tag-predicate") + } + + if c.String("trim-tag-prefix") != "" { + source.TrimTagPrefix = c.String("trim-tag-prefix") + } + + if c.Bool("pin") { + source.Pinned = true + } + + if c.Bool("force") { + if source.Pinned { + return fmt.Errorf("cannot set a source to be statically forced and pinned at the same time") + } + + source.Force = true + } + + if sha256, err := yae.FetchSHA256(source.URL, c.Bool("unpack")); err != nil { + return err + } else { + source.SHA256 = sha256 + } + + if err := sources.Add(c.Args().Get(0), source); err != nil { + return err + } + + return sources.Save(c.String("sources")) + } +} diff --git a/internal/commands/drop.go b/internal/commands/drop.go new file mode 100644 index 0000000..58855df --- /dev/null +++ b/internal/commands/drop.go @@ -0,0 +1,24 @@ +package commands + +import ( + "fmt" + + "github.com/Fuwn/yae/internal/yae" + "github.com/urfave/cli/v2" +) + +func Drop(sources *yae.Sources) func(c *cli.Context) error { + return func(c *cli.Context) error { + if c.Args().Len() == 0 { + return fmt.Errorf("invalid number of arguments") + } + + if !sources.Exists(c.Args().Get(0)) { + return fmt.Errorf("source does not exist") + } + + sources.Drop(c.Args().Get(0)) + + return sources.Save(c.String("sources")) + } +} diff --git a/internal/commands/init.go b/internal/commands/init.go new file mode 100644 index 0000000..f38ec96 --- /dev/null +++ b/internal/commands/init.go @@ -0,0 +1,19 @@ +package commands + +import ( + "fmt" + "os" + + "github.com/Fuwn/yae/internal/yae" + "github.com/urfave/cli/v2" +) + +func Init(sources *yae.Sources) func(c *cli.Context) error { + return func(c *cli.Context) error { + if _, err := os.Stat(c.String("sources")); err == nil { + return fmt.Errorf("sources file already exists") + } + + return sources.Save(c.String("sources")) + } +} diff --git a/internal/commands/update.go b/internal/commands/update.go new file mode 100644 index 0000000..66ccfb3 --- /dev/null +++ b/internal/commands/update.go @@ -0,0 +1,51 @@ +package commands + +import ( + "fmt" + + "github.com/Fuwn/yae/internal/yae" + "github.com/urfave/cli/v2" +) + +func Update(sources *yae.Sources) func(c *cli.Context) error { + return func(c *cli.Context) error { + updates := []string{} + force := c.Bool("force-hashed") + forcePinned := c.Bool("force-pinned") + + if c.Args().Len() == 0 { + for name, source := range *sources { + if updated, err := source.Update(sources, name, force, forcePinned); err != nil { + return err + } else if updated { + updates = append(updates, name) + } + } + } else { + name := c.Args().Get(0) + source := (*sources)[name] + + if updated, err := source.Update(sources, name, force, forcePinned); err != nil { + return err + } else if updated { + updates = append(updates, name) + } + } + + if len(updates) > 0 { + if err := sources.Save(c.String("sources")); err != nil { + return err + } + } + + if c.Bool("output-updated-list") { + for _, update := range updates { + fmt.Println(update) + } + } else if c.Bool("output-formatted-updated-list") { + fmt.Println(yae.Lister(updates)) + } + + return nil + } +} |