1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/charmbracelet/log"
"github.com/urfave/cli/v2"
)
func main() {
sources := Sources{}
if err := (&cli.App{
Name: "yae",
Usage: "Nix Dependency Manager",
Description: "Nix Dependency Manager",
EnableBashCompletion: true,
Authors: []*cli.Author{
{
Name: "Fuwn",
Email: "[email protected]",
},
},
Before: func(c *cli.Context) error {
if args := c.Args(); args.Len() == 1 && args.Get(0) == "init" {
return nil
}
return sources.Load(c.String("sources"))
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "sources",
Value: "./yae.json",
Usage: "Sources path",
},
&cli.BoolFlag{
Name: "debug",
Usage: "Enable debug output",
Action: func(*cli.Context, bool) error {
log.SetLevel(log.DebugLevel)
return nil
},
},
&cli.BoolFlag{
Name: "silent",
Usage: "Silence log output",
Action: func(*cli.Context, bool) error {
log.SetLevel(log.WarnLevel)
return nil
},
},
},
Copyright: fmt.Sprintf("Copyright (c) 2024-%s Fuwn", fmt.Sprint(time.Now().Year())),
ExitErrHandler: func(c *cli.Context, err error) {
if err != nil {
log.Fatal(err.Error())
}
},
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: "add",
Args: true,
ArgsUsage: "<name> <url>",
Usage: "Add a source",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "unpack",
Usage: "Unpack the source into the Nix Store",
Value: true,
},
&cli.StringFlag{
Name: "type",
Usage: "Source type",
Required: true,
Action: func(c *cli.Context, value string) error {
if value != "binary" && value != "git" {
return fmt.Errorf("invalid source type: must be 'binary' or 'git'")
}
return nil
},
},
&cli.StringFlag{
Name: "version",
Usage: "Source version used in identifying latest git source",
},
&cli.StringFlag{
Name: "tag-predicate",
Usage: "Git tag predicate used in identifying latest git source",
},
&cli.StringFlag{
Name: "trim-tag-prefix",
Usage: "A prefix to trim from remote git tags",
},
&cli.BoolFlag{
Name: "pin",
Usage: "Prevent the source from being updated",
},
&cli.BoolFlag{
Name: "force",
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"))
},
},
{
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: "update",
Args: true,
Usage: "Update one or all sources",
ArgsUsage: "[name]",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "output-updated-list",
Usage: "Output a newline-seperated list of updated sources, regardless of silent mode",
},
&cli.BoolFlag{
Name: "output-formatted-updated-list",
Usage: "Output a comma and/or ampersand list of updated sources, regardless of silent mode",
},
&cli.BoolFlag{
Name: "force-hashed",
Usage: "Force updates for non-pinned sources that have an unchanged version (recalculate hash)",
},
&cli.BoolFlag{
Name: "force-pinned",
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
},
},
},
}).Run(os.Args); err !=
|