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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"time"
"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",
},
},
Copyright: fmt.Sprintf("Copyright (c) 2024-%s Fuwn", fmt.Sprint(time.Now().Year())),
ExitErrHandler: func(c *cli.Context, err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
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.BoolFlag{
Name: "silent",
Usage: "Silence output",
},
&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",
},
},
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 sha256, err := fetchSHA256(source.URL, c.Bool("unpack"), !c.Bool("silent")); 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: "show-updated-only",
Usage: "Output a newline-seperated list of updated sources, silence other output",
},
&cli.BoolFlag{
Name: "show-updated-only-formatted",
Usage: "Output a comma and/or ampersand list of updated sources, silence other output",
},
&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 {
showAll := !c.Bool("show-updated-only") && !c.Bool("show-updated-only-formatted")
updates := []string{}
force := c.Bool("force-hashed")
forcePinned := c.Bool("force-pinned")
if c.Args().Len() == 0 {
for name, value := range sources {
if updated, err := updateSource(&sources, name, value, showAll, force, forcePinned); err != nil {
return err
} else if updated {
updates = append(updates, name)
}
}
} else {
name := c.Args().Get(0)
if updated, err := updateSource(&sources, name, sources[name], showAll, 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("show-updated-only") {
for _, update := range updates {
fmt.Println(update)
}
} else if c.Bool("show-updated-only-formatted") {
fmt.Println(lister(updates))
}
return nil
},
},
},
}).Run(os.Args); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func fetchSHA256(url string, unpack bool, show bool) (string, error) {
arguments := []string{"--type", "sha256", url}
if unpack {
arguments = append([]string{"--unpack"}, arguments...)
}
output, err := command("nix-prefetch-url", show, arguments...)
if err != nil {
return "", err
}
lines := strings.Split(output, "\n")
return strings.Trim(lines[len(lines)-2], "\n"), nil
}
func command(name string, show bool, args ...string) (string, error) {
executable, err := exec.LookPath(name)
out := []byte{}
if show {
cmd := exec.Command(executable, args...)
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
out, err = cmd.Output()
} else {
cmd := exec.Command(executable, args...)
out, err = cmd.Output()
}
return string(out), err
}
func fetchLatestGitTag(source Source, show bool) (string, error) {
if source.Type == "git" {
repository := "https://github.com/" + strings.Split(source.URL, "/")[3] + "/" + strings.Split(source.URL, "/")[4]
remotes, err := command("bash", show, "-c", fmt.Sprintf("git ls-remote %s | awk -F'/' '{print $NF}' | sort -V", repository))
if err != nil {
return "", err
}
refs := strings.Split(remotes, "\n")
var latest string
if source.TagPredicate == "" {
latest = refs[len(refs)-2]
} else {
for i := len(refs) - 2; i >= 0; i-- {
if strings.Contains(refs[i], source.TagPredicate) {
latest = refs[i]
break
}
}
}
if source.TrimTagPrefix != "" {
latest = strings.TrimPrefix(latest, source.TrimTagPrefix)
}
return latest, nil
}
return "", fmt.Errorf("source is not a git repository")
}
func updateSource(sources *Sources, name string, source Source, show bool, force bool, forcePinned bool) (bool, error) {
updated := false
if !sources.Exists(name) {
return updated, fmt.Errorf("source does not exist")
}
if source.Pinned && !forcePinned {
if show {
fmt.Println("skipped update for", name, "because it is pinned")
}
return updated, nil
}
if source.Type == "git" {
tag, err := fetchLatestGitTag(source, show)
if err != nil {
return updated, err
}
if tag != source.Version || force {
if show {
fmt.Println("updated version for", name, "from", source.Version, "to", tag)
}
source.Version = tag
updated = true
if strings.Contains(source.URLTemplate, "{version}") {
source.URL = strings.ReplaceAll(source.URLTemplate, "{version}", source.Version)
}
} else {
if show {
fmt.Println("skipped update for", name, "because the version is unchanged")
}
return updated, nil
}
}
sha256, err := fetchSHA256(source.URL, source.Unpack, show)
if err != nil {
return updated, err
}
if sha256 != source.SHA256 {
if show {
fmt.Println("updated hash for", name, "from", source.SHA256, "to", sha256)
}
source.SHA256 = sha256
updated = true
}
(*sources)[name] = source
return updated, nil
}
func lister(items []string) string {
if len(items) == 0 {
return ""
} else if len(items) == 1 {
return items[0]
} else if len(items) == 2 {
return fmt.Sprintf("%s & %s", items[0], items[1])
}
return fmt.Sprintf("%s, & %s", strings.Join(items[:len(items)-1], ", "), items[len(items)-1])
}
|