⛩️ Powerful yet Minimal Nix Dependency Manager
flake flakes home-manager nixos go nix dependency dependencies

feat(yae): add optional schema field

fuwn.net 4b64fdd0 9f6bf390

verified
+102 -63
+1 -1
flake.nix
··· 54 54 inherit meta; 55 55 56 56 pname = name; 57 - version = "2024.10.14"; 57 + version = "2024.10.25"; 58 58 src = pkgs.lib.cleanSource ./.; 59 59 vendorHash = "sha256-XQEB2vgiztbtLnc7BR4WTouPI+2NDQXXFUNidqmvbac="; 60 60 buildInputs = [ pkgs.musl ];
+1 -1
internal/commands/add.go
··· 50 50 } 51 51 } 52 52 53 - func Add(sources *yae.Sources) func(c *cli.Context) error { 53 + func Add(sources *yae.Environment) func(c *cli.Context) error { 54 54 return func(c *cli.Context) error { 55 55 if c.Args().Len() != 2 { 56 56 return fmt.Errorf("invalid number of arguments")
+1 -1
internal/commands/drop.go
··· 7 7 "github.com/urfave/cli/v2" 8 8 ) 9 9 10 - func Drop(sources *yae.Sources) func(c *cli.Context) error { 10 + func Drop(sources *yae.Environment) func(c *cli.Context) error { 11 11 return func(c *cli.Context) error { 12 12 if c.Args().Len() == 0 { 13 13 return fmt.Errorf("invalid number of arguments")
+1 -1
internal/commands/init.go
··· 8 8 "github.com/urfave/cli/v2" 9 9 ) 10 10 11 - func Init(sources *yae.Sources) func(c *cli.Context) error { 11 + func Init(sources *yae.Environment) func(c *cli.Context) error { 12 12 return func(c *cli.Context) error { 13 13 if _, err := os.Stat(c.String("sources")); err == nil { 14 14 return fmt.Errorf("sources file already exists")
+3 -3
internal/commands/update.go
··· 28 28 } 29 29 } 30 30 31 - func Update(sources *yae.Sources) func(c *cli.Context) error { 31 + func Update(sources *yae.Environment) func(c *cli.Context) error { 32 32 return func(c *cli.Context) error { 33 33 updates := []string{} 34 34 force := c.Bool("force-hashed") 35 35 forcePinned := c.Bool("force-pinned") 36 36 37 37 if c.Args().Len() == 0 { 38 - for name, source := range *sources { 38 + for name, source := range sources.Sources { 39 39 if updated, err := source.Update(sources, name, force, forcePinned); err != nil { 40 40 return err 41 41 } else if updated { ··· 44 44 } 45 45 } else { 46 46 name := c.Args().Get(0) 47 - source := (*sources)[name] 47 + source := (*sources).Sources[name] 48 48 49 49 if updated, err := source.Update(sources, name, force, forcePinned); err != nil { 50 50 return err
+92
internal/yae/environment.go
··· 1 + package yae 2 + 3 + import ( 4 + "encoding/json" 5 + "fmt" 6 + "os" 7 + ) 8 + 9 + type Environment struct { 10 + Schema string 11 + Sources map[string]Source 12 + } 13 + 14 + func (s *Environment) Add(name string, d Source) error { 15 + if s.Exists(name) { 16 + return fmt.Errorf("source already exists") 17 + } 18 + 19 + (*s).Sources[name] = d 20 + 21 + return nil 22 + } 23 + 24 + func (s *Environment) Exists(name string) bool { 25 + _, ok := (*s).Sources[name] 26 + 27 + return ok 28 + } 29 + 30 + func (s *Environment) Drop(url string) { 31 + delete((*s).Sources, url) 32 + } 33 + 34 + func (s *Environment) Save(path string) error { 35 + file, err := os.Create(path) 36 + 37 + if err != nil { 38 + return err 39 + } 40 + 41 + defer file.Close() 42 + 43 + sourcesData, err := json.Marshal(s.Sources) 44 + 45 + if err != nil { 46 + return err 47 + } 48 + 49 + var jsonData map[string]json.RawMessage 50 + 51 + if err := json.Unmarshal(sourcesData, &jsonData); err != nil { 52 + return err 53 + } 54 + 55 + if s.Schema != "" { 56 + jsonData["$schema"] = json.RawMessage(fmt.Sprintf(`"%s"`, s.Schema)) 57 + } 58 + 59 + encoder := json.NewEncoder(file) 60 + 61 + encoder.SetIndent("", " ") 62 + 63 + return encoder.Encode(jsonData) 64 + } 65 + 66 + func (s *Environment) Load(path string) error { 67 + file, err := os.Open(path) 68 + 69 + if err != nil { 70 + return err 71 + } 72 + 73 + defer file.Close() 74 + 75 + var rawData map[string]json.RawMessage 76 + 77 + if err := json.NewDecoder(file).Decode(&rawData); err != nil { 78 + return err 79 + } 80 + 81 + if schema, ok := rawData["$schema"]; ok { 82 + json.Unmarshal(schema, &s.Schema) 83 + } 84 + 85 + delete(rawData, "$schema") 86 + 87 + if filteredData, err := json.Marshal(rawData); err != nil { 88 + return err 89 + } else { 90 + return json.Unmarshal(filteredData, &s.Sources) 91 + } 92 + }
+2 -2
internal/yae/source.go
··· 21 21 Force bool `json:"force,omitempty"` 22 22 } 23 23 24 - func (source *Source) Update(sources *Sources, name string, force bool, forcePinned bool) (bool, error) { 24 + func (source *Source) Update(sources *Environment, name string, force bool, forcePinned bool) (bool, error) { 25 25 log.Infof("checking %s", name) 26 26 27 27 updated := false ··· 85 85 updated = true 86 86 } 87 87 88 - (*sources)[name] = *source 88 + (*sources).Sources[name] = *source 89 89 90 90 return updated, nil 91 91 }
-53
internal/yae/sources.go
··· 1 - package yae 2 - 3 - import ( 4 - "encoding/json" 5 - "fmt" 6 - "os" 7 - ) 8 - 9 - type Sources map[string]Source 10 - 11 - func (s *Sources) Add(name string, d Source) error { 12 - if s.Exists(name) { 13 - return fmt.Errorf("source already exists") 14 - } 15 - 16 - (*s)[name] = d 17 - 18 - return nil 19 - } 20 - 21 - func (s *Sources) Exists(name string) bool { 22 - _, ok := (*s)[name] 23 - 24 - return ok 25 - } 26 - 27 - func (s *Sources) Drop(url string) { 28 - delete((*s), url) 29 - } 30 - 31 - func (s *Sources) Save(path string) error { 32 - file, err := os.Create(path) 33 - 34 - if err != nil { 35 - return err 36 - } 37 - 38 - encoder := json.NewEncoder(file) 39 - 40 - encoder.SetIndent("", " ") 41 - 42 - return encoder.Encode(s) 43 - } 44 - 45 - func (s *Sources) Load(path string) error { 46 - file, err := os.Open(path) 47 - 48 - if err != nil { 49 - return err 50 - } 51 - 52 - return json.NewDecoder(file).Decode(s) 53 - }
+1 -1
yae.go
··· 12 12 ) 13 13 14 14 func main() { 15 - sources := yae.Sources{} 15 + sources := yae.Environment{} 16 16 17 17 if err := (&cli.App{ 18 18 Name: "yae",