aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-10-25 02:56:14 -0700
committerFuwn <[email protected]>2024-10-25 03:12:22 -0700
commit6bc492fe65c567dced04771c9729702d1c7d5154 (patch)
tree65a3d5c4ad125dfde8981d0f9ab97815bcae7aaf
parentd5324b441724b3af301079a2d514b979f5af4958 (diff)
downloadyae-6bc492fe65c567dced04771c9729702d1c7d5154.tar.xz
yae-6bc492fe65c567dced04771c9729702d1c7d5154.zip
feat(yae): add optional schema field
-rw-r--r--flake.nix2
-rw-r--r--internal/commands/add.go2
-rw-r--r--internal/commands/drop.go2
-rw-r--r--internal/commands/init.go2
-rw-r--r--internal/commands/update.go6
-rw-r--r--internal/yae/environment.go92
-rw-r--r--internal/yae/source.go4
-rw-r--r--internal/yae/sources.go53
-rw-r--r--yae.go2
9 files changed, 102 insertions, 63 deletions
diff --git a/flake.nix b/flake.nix
index ed52a70..ad3bdd3 100644
--- a/flake.nix
+++ b/flake.nix
@@ -54,7 +54,7 @@
inherit meta;
pname = name;
- version = "2024.10.14";
+ version = "2024.10.25";
src = pkgs.lib.cleanSource ./.;
vendorHash = "sha256-XQEB2vgiztbtLnc7BR4WTouPI+2NDQXXFUNidqmvbac=";
buildInputs = [ pkgs.musl ];
diff --git a/internal/commands/add.go b/internal/commands/add.go
index 1b32533..07bee28 100644
--- a/internal/commands/add.go
+++ b/internal/commands/add.go
@@ -50,7 +50,7 @@ func AddFlags() []cli.Flag {
}
}
-func Add(sources *yae.Sources) func(c *cli.Context) error {
+func Add(sources *yae.Environment) func(c *cli.Context) error {
return func(c *cli.Context) error {
if c.Args().Len() != 2 {
return fmt.Errorf("invalid number of arguments")
diff --git a/internal/commands/drop.go b/internal/commands/drop.go
index f30afb6..788b62b 100644
--- a/internal/commands/drop.go
+++ b/internal/commands/drop.go
@@ -7,7 +7,7 @@ import (
"github.com/urfave/cli/v2"
)
-func Drop(sources *yae.Sources) func(c *cli.Context) error {
+func Drop(sources *yae.Environment) func(c *cli.Context) error {
return func(c *cli.Context) error {
if c.Args().Len() == 0 {
return fmt.Errorf("invalid number of arguments")
diff --git a/internal/commands/init.go b/internal/commands/init.go
index 4d41553..c4bc138 100644
--- a/internal/commands/init.go
+++ b/internal/commands/init.go
@@ -8,7 +8,7 @@ import (
"github.com/urfave/cli/v2"
)
-func Init(sources *yae.Sources) func(c *cli.Context) error {
+func Init(sources *yae.Environment) 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")
diff --git a/internal/commands/update.go b/internal/commands/update.go
index fa7e09f..5889d3a 100644
--- a/internal/commands/update.go
+++ b/internal/commands/update.go
@@ -28,14 +28,14 @@ func UpdateFlags() []cli.Flag {
}
}
-func Update(sources *yae.Sources) func(c *cli.Context) error {
+func Update(sources *yae.Environment) 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 {
+ for name, source := range sources.Sources {
if updated, err := source.Update(sources, name, force, forcePinned); err != nil {
return err
} else if updated {
@@ -44,7 +44,7 @@ func Update(sources *yae.Sources) func(c *cli.Context) error {
}
} else {
name := c.Args().Get(0)
- source := (*sources)[name]
+ source := (*sources).Sources[name]
if updated, err := source.Update(sources, name, force, forcePinned); err != nil {
return err
diff --git a/internal/yae/environment.go b/internal/yae/environment.go
new file mode 100644
index 0000000..739f069
--- /dev/null
+++ b/internal/yae/environment.go
@@ -0,0 +1,92 @@
+package yae
+
+import (
+ "encoding/json"
+ "fmt"
+ "os"
+)
+
+type Environment struct {
+ Schema string
+ Sources map[string]Source
+}
+
+func (s *Environment) Add(name string, d Source) error {
+ if s.Exists(name) {
+ return fmt.Errorf("source already exists")
+ }
+
+ (*s).Sources[name] = d
+
+ return nil
+}
+
+func (s *Environment) Exists(name string) bool {
+ _, ok := (*s).Sources[name]
+
+ return ok
+}
+
+func (s *Environment) Drop(url string) {
+ delete((*s).Sources, url)
+}
+
+func (s *Environment) Save(path string) error {
+ file, err := os.Create(path)
+
+ if err != nil {
+ return err
+ }
+
+ defer file.Close()
+
+ sourcesData, err := json.Marshal(s.Sources)
+
+ if err != nil {
+ return err
+ }
+
+ var jsonData map[string]json.RawMessage
+
+ if err := json.Unmarshal(sourcesData, &jsonData); err != nil {
+ return err
+ }
+
+ if s.Schema != "" {
+ jsonData["$schema"] = json.RawMessage(fmt.Sprintf(`"%s"`, s.Schema))
+ }
+
+ encoder := json.NewEncoder(file)
+
+ encoder.SetIndent("", " ")
+
+ return encoder.Encode(jsonData)
+}
+
+func (s *Environment) Load(path string) error {
+ file, err := os.Open(path)
+
+ if err != nil {
+ return err
+ }
+
+ defer file.Close()
+
+ var rawData map[string]json.RawMessage
+
+ if err := json.NewDecoder(file).Decode(&rawData); err != nil {
+ return err
+ }
+
+ if schema, ok := rawData["$schema"]; ok {
+ json.Unmarshal(schema, &s.Schema)
+ }
+
+ delete(rawData, "$schema")
+
+ if filteredData, err := json.Marshal(rawData); err != nil {
+ return err
+ } else {
+ return json.Unmarshal(filteredData, &s.Sources)
+ }
+}
diff --git a/internal/yae/source.go b/internal/yae/source.go
index 625a0f8..735e4d4 100644
--- a/internal/yae/source.go
+++ b/internal/yae/source.go
@@ -21,7 +21,7 @@ type Source struct {
Force bool `json:"force,omitempty"`
}
-func (source *Source) Update(sources *Sources, name string, force bool, forcePinned bool) (bool, error) {
+func (source *Source) Update(sources *Environment, name string, force bool, forcePinned bool) (bool, error) {
log.Infof("checking %s", name)
updated := false
@@ -85,7 +85,7 @@ func (source *Source) Update(sources *Sources, name string, force bool, forcePin
updated = true
}
- (*sources)[name] = *source
+ (*sources).Sources[name] = *source
return updated, nil
}
diff --git a/internal/yae/sources.go b/internal/yae/sources.go
deleted file mode 100644
index f8cb2b0..0000000
--- a/internal/yae/sources.go
+++ /dev/null
@@ -1,53 +0,0 @@
-package yae
-
-import (
- "encoding/json"
- "fmt"
- "os"
-)
-
-type Sources map[string]Source
-
-func (s *Sources) Add(name string, d Source) error {
- if s.Exists(name) {
- return fmt.Errorf("source already exists")
- }
-
- (*s)[name] = d
-
- return nil
-}
-
-func (s *Sources) Exists(name string) bool {
- _, ok := (*s)[name]
-
- return ok
-}
-
-func (s *Sources) Drop(url string) {
- delete((*s), url)
-}
-
-func (s *Sources) Save(path string) error {
- file, err := os.Create(path)
-
- if err != nil {
- return err
- }
-
- encoder := json.NewEncoder(file)
-
- encoder.SetIndent("", " ")
-
- return encoder.Encode(s)
-}
-
-func (s *Sources) Load(path string) error {
- file, err := os.Open(path)
-
- if err != nil {
- return err
- }
-
- return json.NewDecoder(file).Decode(s)
-}
diff --git a/yae.go b/yae.go
index c9e1c21..0b1c358 100644
--- a/yae.go
+++ b/yae.go
@@ -12,7 +12,7 @@ import (
)
func main() {
- sources := yae.Sources{}
+ sources := yae.Environment{}
if err := (&cli.App{
Name: "yae",