-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
274 lines (230 loc) · 6 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
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
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"text/tabwriter"
)
const agoUsage = `usage: ago <command> [arguments]
ago is a wrapper around the go command that adds the ability to alias packages
with short, memorable names. Only the get and install commands are affected. All
other flags and arguments are passed through to the go command.
create aliases with the alias command:
$ ago alias foo github.com/foo/bar/v2
then use the alias in place of the package name:
$ ago get foo
which is equivalent to:
$ go get github.com/foo/bar/v2
The commands are:
alias, a create/manage package aliases
get download packages and dependencies
install compile and install packages and dependencies
help display this help text
`
const aliasUsage = `usage:
create an alias:
ago alias foo github.com/foo/bar/v2
remove an alias:
ago alias rm foo
list all aliases:
ago alias list
The sub-commands are:
list, ls, l list all aliases
rm remove an alias
help display this help text
`
func main() {
if len(os.Args) < 2 {
fmt.Print(agoUsage)
return
}
aliases, err := loadAliases()
if err != nil {
fatalf("error: %v", err)
}
args := make([]string, len(os.Args))
copy(args, os.Args)
switch args[1] {
case "help":
fmt.Print(agoUsage)
return
case "get", "install":
if len(args) > 2 {
for i := 2; i < len(args); i++ {
arg := args[i]
// Find the alias with the longest matching prefix.
var alias string
var pkg string
for a, p := range aliases {
if strings.HasPrefix(arg, a) && len(a) > len(alias) {
alias = a
pkg = p
}
}
if alias == "" {
continue
}
// If the user is requesting a specific version, extract it.
var version string
if idx := strings.LastIndex(arg, "@"); idx != -1 {
version = arg[idx:]
arg = arg[:idx]
}
pkgPath := strings.TrimPrefix(arg, alias)
// If the package path starts with a major version, then we need
// to strip it off and replace it with the aliased package path.
var major string
if split := strings.SplitN(pkgPath, "/", 3); len(split) > 1 {
if split[1][0] == 'v' {
if _, err := strconv.Atoi(split[1][1:]); err == nil {
major = "/" + split[1]
if len(split) > 2 {
pkgPath = "/" + split[2]
} else {
pkgPath = ""
}
}
}
}
// If the user has requested a specific major version, and the
// aliased package path already contains a major version, then
// we need to strip it off and replace it with the requested
// major version. Unless the requested major version < 2, in
// which case we just strip it off.
if major != "" {
// Strip off the major version.
if idx := strings.LastIndex(pkg, "/v"); idx != -1 {
if _, err := strconv.Atoi(pkg[idx+2:]); err == nil {
pkg = pkg[:idx]
}
}
// If the requested major version is < 2, then set it to
// the empty string.
if len(major) == 3 && (major[2] == '0' || major[2] == '1') {
major = ""
}
}
args[i] = pkg + major + pkgPath + version
}
}
case "alias", "a":
if len(args) < 3 {
fmt.Print(aliasUsage)
return
}
switch args[2] {
case "help":
fmt.Print(aliasUsage)
return
case "list", "ls", "l":
type row struct {
alias string
pkg string
}
var rows []row
for alias, pkg := range aliases {
rows = append(rows, row{alias, pkg})
}
sort.Slice(rows, func(i, j int) bool {
return rows[i].alias < rows[j].alias
})
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintln(tw, "ALIAS\tPACKAGE")
fmt.Fprintln(tw, "-----\t-------")
for _, row := range rows {
fmt.Fprintf(tw, "%s\t%s\n", row.alias, row.pkg)
}
tw.Flush()
return
case "rm":
if len(args) < 4 {
fatalf("error: not enough arguments")
}
delete(aliases, args[3])
if err := storeAliases(aliases); err != nil {
fatalf("error: %v", err)
}
fmt.Printf("removed alias %q\n", args[3])
return
default:
if len(args) < 4 {
fatalf("error: not enough arguments")
}
aliases[args[2]] = args[3]
if err := storeAliases(aliases); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
fmt.Printf("aliased %q to %q\n", args[2], args[3])
return
}
}
fmt.Printf("> go %s\n", strings.Join(args[1:], " "))
cmd := exec.Command("go", args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
if err := cmd.Run(); err != nil {
var exitErr *exec.ExitError
if ok := errors.As(err, &exitErr); ok {
os.Exit(exitErr.ExitCode())
}
fatalf("error: %v", err)
}
}
const aliasesFile = "aliases.json"
func loadAliases() (map[string]string, error) {
f, err := os.Open(filepath.Join(configDir, aliasesFile))
if os.IsNotExist(err) {
return make(map[string]string), nil
}
if err != nil {
return nil, fmt.Errorf("open aliases file: %w", err)
}
defer f.Close()
var aliases map[string]string
if err := json.NewDecoder(f).Decode(&aliases); err != nil {
return nil, fmt.Errorf("decode aliases file: %w", err)
}
return aliases, nil
}
func storeAliases(aliases map[string]string) error {
if err := os.MkdirAll(configDir, 0755); err != nil {
return fmt.Errorf("create config dir: %w", err)
}
f, err := os.Create(filepath.Join(configDir, aliasesFile))
if err != nil {
return fmt.Errorf("create aliases file: %w", err)
}
defer f.Close()
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
if err := enc.Encode(aliases); err != nil {
return fmt.Errorf("encode aliases file: %w", err)
}
return nil
}
func fatalf(format string, args ...interface{}) {
if !strings.HasSuffix(format, "\n") {
format += "\n"
}
fmt.Fprintf(os.Stderr, format, args...)
os.Exit(1)
}
var configDir string
func init() {
if configDir = os.Getenv("AGO_CONFIG_DIR"); configDir != "" {
return
}
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
configDir = filepath.Join(home, ".ago")
}