tangled
alpha
login
or
join now
fuwn.net
/
yae
0
fork
atom
⛩️ Powerful yet Minimal Nix Dependency Manager
flake
flakes
home-manager
nixos
go
nix
dependency
dependencies
0
fork
atom
overview
issues
pulls
pipelines
feat(yae): add optional schema field
fuwn.net
2 years ago
4b64fdd0
9f6bf390
verified
This commit was signed with the committer's
known signature
.
fuwn.net
SSH Key Fingerprint:
SHA256:VPdFPyPbd6JkoMyWUdZ/kkTcIAt3sxjXD2XSAZ7FYC4=
+102
-63
9 changed files
expand all
collapse all
unified
split
flake.nix
internal
commands
add.go
drop.go
init.go
update.go
yae
environment.go
source.go
sources.go
yae.go
+1
-1
flake.nix
reviewed
···
54
54
inherit meta;
55
55
56
56
pname = name;
57
57
-
version = "2024.10.14";
57
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
reviewed
···
50
50
}
51
51
}
52
52
53
53
-
func Add(sources *yae.Sources) func(c *cli.Context) error {
53
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
reviewed
···
7
7
"github.com/urfave/cli/v2"
8
8
)
9
9
10
10
-
func Drop(sources *yae.Sources) func(c *cli.Context) error {
10
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
reviewed
···
8
8
"github.com/urfave/cli/v2"
9
9
)
10
10
11
11
-
func Init(sources *yae.Sources) func(c *cli.Context) error {
11
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
reviewed
···
28
28
}
29
29
}
30
30
31
31
-
func Update(sources *yae.Sources) func(c *cli.Context) error {
31
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
38
-
for name, source := range *sources {
38
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
47
-
source := (*sources)[name]
47
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
reviewed
···
1
1
+
package yae
2
2
+
3
3
+
import (
4
4
+
"encoding/json"
5
5
+
"fmt"
6
6
+
"os"
7
7
+
)
8
8
+
9
9
+
type Environment struct {
10
10
+
Schema string
11
11
+
Sources map[string]Source
12
12
+
}
13
13
+
14
14
+
func (s *Environment) Add(name string, d Source) error {
15
15
+
if s.Exists(name) {
16
16
+
return fmt.Errorf("source already exists")
17
17
+
}
18
18
+
19
19
+
(*s).Sources[name] = d
20
20
+
21
21
+
return nil
22
22
+
}
23
23
+
24
24
+
func (s *Environment) Exists(name string) bool {
25
25
+
_, ok := (*s).Sources[name]
26
26
+
27
27
+
return ok
28
28
+
}
29
29
+
30
30
+
func (s *Environment) Drop(url string) {
31
31
+
delete((*s).Sources, url)
32
32
+
}
33
33
+
34
34
+
func (s *Environment) Save(path string) error {
35
35
+
file, err := os.Create(path)
36
36
+
37
37
+
if err != nil {
38
38
+
return err
39
39
+
}
40
40
+
41
41
+
defer file.Close()
42
42
+
43
43
+
sourcesData, err := json.Marshal(s.Sources)
44
44
+
45
45
+
if err != nil {
46
46
+
return err
47
47
+
}
48
48
+
49
49
+
var jsonData map[string]json.RawMessage
50
50
+
51
51
+
if err := json.Unmarshal(sourcesData, &jsonData); err != nil {
52
52
+
return err
53
53
+
}
54
54
+
55
55
+
if s.Schema != "" {
56
56
+
jsonData["$schema"] = json.RawMessage(fmt.Sprintf(`"%s"`, s.Schema))
57
57
+
}
58
58
+
59
59
+
encoder := json.NewEncoder(file)
60
60
+
61
61
+
encoder.SetIndent("", " ")
62
62
+
63
63
+
return encoder.Encode(jsonData)
64
64
+
}
65
65
+
66
66
+
func (s *Environment) Load(path string) error {
67
67
+
file, err := os.Open(path)
68
68
+
69
69
+
if err != nil {
70
70
+
return err
71
71
+
}
72
72
+
73
73
+
defer file.Close()
74
74
+
75
75
+
var rawData map[string]json.RawMessage
76
76
+
77
77
+
if err := json.NewDecoder(file).Decode(&rawData); err != nil {
78
78
+
return err
79
79
+
}
80
80
+
81
81
+
if schema, ok := rawData["$schema"]; ok {
82
82
+
json.Unmarshal(schema, &s.Schema)
83
83
+
}
84
84
+
85
85
+
delete(rawData, "$schema")
86
86
+
87
87
+
if filteredData, err := json.Marshal(rawData); err != nil {
88
88
+
return err
89
89
+
} else {
90
90
+
return json.Unmarshal(filteredData, &s.Sources)
91
91
+
}
92
92
+
}
+2
-2
internal/yae/source.go
reviewed
···
21
21
Force bool `json:"force,omitempty"`
22
22
}
23
23
24
24
-
func (source *Source) Update(sources *Sources, name string, force bool, forcePinned bool) (bool, error) {
24
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
88
-
(*sources)[name] = *source
88
88
+
(*sources).Sources[name] = *source
89
89
90
90
return updated, nil
91
91
}
-53
internal/yae/sources.go
reviewed
···
1
1
-
package yae
2
2
-
3
3
-
import (
4
4
-
"encoding/json"
5
5
-
"fmt"
6
6
-
"os"
7
7
-
)
8
8
-
9
9
-
type Sources map[string]Source
10
10
-
11
11
-
func (s *Sources) Add(name string, d Source) error {
12
12
-
if s.Exists(name) {
13
13
-
return fmt.Errorf("source already exists")
14
14
-
}
15
15
-
16
16
-
(*s)[name] = d
17
17
-
18
18
-
return nil
19
19
-
}
20
20
-
21
21
-
func (s *Sources) Exists(name string) bool {
22
22
-
_, ok := (*s)[name]
23
23
-
24
24
-
return ok
25
25
-
}
26
26
-
27
27
-
func (s *Sources) Drop(url string) {
28
28
-
delete((*s), url)
29
29
-
}
30
30
-
31
31
-
func (s *Sources) Save(path string) error {
32
32
-
file, err := os.Create(path)
33
33
-
34
34
-
if err != nil {
35
35
-
return err
36
36
-
}
37
37
-
38
38
-
encoder := json.NewEncoder(file)
39
39
-
40
40
-
encoder.SetIndent("", " ")
41
41
-
42
42
-
return encoder.Encode(s)
43
43
-
}
44
44
-
45
45
-
func (s *Sources) Load(path string) error {
46
46
-
file, err := os.Open(path)
47
47
-
48
48
-
if err != nil {
49
49
-
return err
50
50
-
}
51
51
-
52
52
-
return json.NewDecoder(file).Decode(s)
53
53
-
}
+1
-1
yae.go
reviewed
···
12
12
)
13
13
14
14
func main() {
15
15
-
sources := yae.Sources{}
15
15
+
sources := yae.Environment{}
16
16
17
17
if err := (&cli.App{
18
18
Name: "yae",