-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
156 lines (147 loc) · 3.05 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
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
package main
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path"
"strings"
)
var bindir string
func main() {
if err := run(); err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}
}
func run() error {
if len(os.Args) < 2 {
return errors.New("missing program arguments")
}
dir, err := os.Getwd()
if err != nil {
return err
}
for {
spec, err := findSpec(dir)
if err != nil {
return err
}
if spec != nil {
bindir = path.Join(dir, "bin")
return spec.exec(os.Args[1:])
}
dir = path.Dir(dir)
if dir == "" || dir == "/" {
break
}
}
return errors.New("go.tools not found")
}
func findSpec(fpath string) (spec, error) {
fpath = path.Join(fpath, "go.tools")
f, err := os.Open(fpath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil, nil
}
return nil, err
}
defer f.Close()
var s spec
sc := bufio.NewScanner(f)
sc.Split(bufio.ScanLines)
for sc.Scan() {
if err := sc.Err(); err != nil {
return s, err
}
s = append(s, sc.Text())
}
if s == nil {
return s, fmt.Errorf("%s is empty", fpath)
}
return s, nil
}
type spec []string
func (spec spec) exec(args []string) error {
for _, spec := range spec {
def := strings.Split(spec, "@")
if len(def) != 2 {
return fmt.Errorf("invalid spec `%s`, expected as: program/path@version", spec)
}
pkg, pkgver := def[0], def[1]
if !strings.HasSuffix(pkg, args[0]) {
continue
}
var vfile *os.File
var curver string
var truncate bool
vpath := path.Join(bindir, args[0]+".version")
if _, err := os.Stat(vpath); err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
if _, err = os.Stat(bindir); err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
if err = os.Mkdir(bindir, os.ModePerm); err != nil {
return err
}
}
vfile, err = os.OpenFile(vpath, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
return err
}
defer vfile.Close()
} else {
vfile, err = os.OpenFile(vpath, os.O_RDWR, 0)
if err != nil {
return err
}
defer vfile.Close()
vdata, err := io.ReadAll(vfile)
if err != nil {
return err
}
curver = strings.Trim(string(vdata), " \n")
truncate = true
}
exe := path.Join(bindir, args[0])
if _, err := os.Stat(exe); err != nil || pkgver != curver {
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
fmt.Println("Installing", spec)
if err = command("go", "install", spec); err != nil {
return err
}
if truncate {
if err = vfile.Truncate(0); err != nil {
return err
}
if _, err = vfile.Seek(0, 0); err != nil {
return err
}
}
if _, err = vfile.WriteString(pkgver); err != nil {
return err
}
}
return command(exe, args[1:]...)
}
return fmt.Errorf("not found spec for %s", args[0])
}
func command(name string, args ...string) error {
cmd := exec.Command(name, args...)
if cmd.Err != nil {
return cmd.Err
}
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "GOBIN="+bindir)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}