aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-10-13 01:11:14 -0700
committerFuwn <[email protected]>2024-10-13 01:11:14 -0700
commit0aeae8d523ecb762e296b938cff3e012551e27b7 (patch)
tree4b6b610e977a4bb610a219009c9e53d9481065d8
parentb9442b0b1401bed64f51af7968936ae94408e99b (diff)
downloadyae-0aeae8d523ecb762e296b938cff3e012551e27b7.tar.xz
yae-0aeae8d523ecb762e296b938cff3e012551e27b7.zip
refactor(yae): move commands and library to internal package
-rw-r--r--.gitignore2
-rw-r--r--internal/commands/add.go70
-rw-r--r--internal/commands/drop.go24
-rw-r--r--internal/commands/init.go19
-rw-r--r--internal/commands/update.go51
-rw-r--r--internal/yae/source.go (renamed from source.go)4
-rw-r--r--internal/yae/sources.go (renamed from sources.go)2
-rw-r--r--internal/yae/utilities.go (renamed from utilities.go)6
-rw-r--r--yae.go137
9 files changed, 183 insertions, 132 deletions
diff --git a/.gitignore b/.gitignore
index ee13815..6572f3d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,4 @@
-yae
+/yae
.pre-commit-config.yaml
result
yae.json
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
+ }
+}
diff --git a/source.go b/internal/yae/source.go
index 490b17c..185aba7 100644
--- a/source.go
+++ b/internal/yae/source.go
@@ -1,4 +1,4 @@
-package main
+package yae
import (
"fmt"
@@ -71,7 +71,7 @@ func (source *Source) Update(sources *Sources, name string, force bool, forcePin
log.Debugf("checking %s: sha256", name)
- sha256, err := fetchSHA256(source.URL, source.Unpack)
+ sha256, err := FetchSHA256(source.URL, source.Unpack)
if err != nil {
return updated, err
diff --git a/sources.go b/internal/yae/sources.go
index d092edd..f8cb2b0 100644
--- a/sources.go
+++ b/internal/yae/sources.go
@@ -1,4 +1,4 @@
-package main
+package yae
import (
"encoding/json"
diff --git a/utilities.go b/internal/yae/utilities.go
index fc9b339..ef9d334 100644
--- a/utilities.go
+++ b/internal/yae/utilities.go
@@ -1,4 +1,4 @@
-package main
+package yae
import (
"fmt"
@@ -7,7 +7,7 @@ import (
"strings"
)
-func fetchSHA256(url string, unpack bool) (string, error) {
+func FetchSHA256(url string, unpack bool) (string, error) {
arguments := []string{"--type", "sha256", url}
if unpack {
@@ -42,7 +42,7 @@ func command(name string, show bool, args ...string) (string, error) {
return string(out), err
}
-func lister(items []string) string {
+func Lister(items []string) string {
if len(items) == 0 {
return ""
} else if len(items) == 1 {
diff --git a/yae.go b/yae.go
index 309819a..60be017 100644
--- a/yae.go
+++ b/yae.go
@@ -3,15 +3,16 @@ package main
import (
"fmt"
"os"
- "strings"
"time"
+ "github.com/Fuwn/yae/internal/commands"
+ "github.com/Fuwn/yae/internal/yae"
"github.com/charmbracelet/log"
"github.com/urfave/cli/v2"
)
func main() {
- sources := Sources{}
+ sources := yae.Sources{}
if err := (&cli.App{
Name: "yae",
@@ -65,15 +66,9 @@ func main() {
Suggest: true,
Commands: []*cli.Command{
{
- Name: "init",
- Usage: "Initialise a new Yae environment",
- Action: 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"))
- },
+ Name: "init",
+ Usage: "Initialise a new Yae environment",
+ Action: commands.Init(&sources),
},
{
Name: "add",
@@ -119,82 +114,13 @@ func main() {
Usage: "Always force update the source, regardless of unchanged remote tag",
},
},
- Action: 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 := 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 := 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"))
- },
+ Action: commands.Add(&sources),
},
{
- Name: "drop",
- Args: true,
- Usage: "Drop a source",
- Action: 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"))
- },
+ Name: "drop",
+ Args: true,
+ Usage: "Drop a source",
+ Action: commands.Drop(&sources),
},
{
Name: "update",
@@ -219,46 +145,7 @@ func main() {
Usage: "Force updates for all sources, including pinned sources (can be used with --force-hashed)",
},
},
- Action: 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(lister(updates))
- }
-
- return nil
- },
+ Action: commands.Update(&sources),
},
},
}).Run(os.Args); err != nil {