-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
82 lines (69 loc) · 1.72 KB
/
main.go
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
package main
import (
"bytes"
"fmt"
"os"
shellquote "github.com/kballard/go-shellquote"
)
type options struct {
buildFlags string
installables []string
}
type nixVersion struct {
Major, Minor, Patch uint64
}
func getNixVersion() (nixVersion, error) {
var version nixVersion
cmd := Command("nix", "--version")
var outb bytes.Buffer
cmd.Stdout = &outb
if err := cmd.Run(); err != nil {
return version, err
}
_, err := fmt.Sscanf(outb.String(), "nix (Nix) %d.%d.%d", &version.Major, &version.Minor, &version.Patch)
if err != nil {
return version, err
}
return version, nil
}
func parseFlags(args []string) (*options, error) {
var opts options
for i := 0; i < len(args); i++ {
if args[i] == "-build-flags" {
if i == len(args)-1 {
return nil, fmt.Errorf("option '-build-flags' requires an argument")
}
i++
opts.buildFlags = args[i]
continue
} else if args[i] == "-flags" {
return nil, fmt.Errorf("option '-flags' is deprecated. You can now pass all those flags directly to nix-build-uncached")
}
opts.installables = append(opts.installables, args[i])
}
return &opts, nil
}
func realMain(args []string) error {
opts, err := parseFlags(args)
if err != nil {
return err
}
buildFlags, err := shellquote.Split(opts.buildFlags)
if err != nil {
return fmt.Errorf("Value passed to -build-flags is not valid: %s", err)
}
version, err := getNixVersion()
if err != nil {
return fmt.Errorf("Failed to get nix version: %s", err)
}
if _, err := buildUncached(opts.installables, buildFlags, version); err != nil {
return fmt.Errorf("%s", err)
}
return nil
}
func main() {
if err := realMain(os.Args[1:]); err != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n", os.Args[0], err)
os.Exit(1)
}
}