aboutsummaryrefslogtreecommitdiff
path: root/internal/commands/add.go
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-10-13 08:11:14 +0000
committerFuwn <[email protected]>2024-10-13 08:11:14 +0000
commit3dd9f302b71fafce9eaaca924017990e8a65f6d6 (patch)
tree4b6b610e977a4bb610a219009c9e53d9481065d8 /internal/commands/add.go
parentfix(flake): module vendor hash (diff)
downloadyae-3dd9f302b71fafce9eaaca924017990e8a65f6d6.tar.xz
yae-3dd9f302b71fafce9eaaca924017990e8a65f6d6.zip
refactor(yae): move commands and library to internal package
Diffstat (limited to 'internal/commands/add.go')
-rw-r--r--internal/commands/add.go70
1 files changed, 70 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"))
+ }
+}