-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
154 lines (125 loc) · 3.04 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
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"al.essio.dev/pkg/tools/internal/version"
"al.essio.dev/pkg/tools/pathlist"
)
const (
programme = "pathctl"
)
var (
helpMode bool
versionMode bool
listMode bool
noprefixMode bool
dropMode bool
)
var (
envVar string
)
var cmdHandlers map[string]func(d pathlist.List)
func init() {
flag.BoolVar(&helpMode, "help", false, "display this help and exit.")
flag.BoolVar(&versionMode, "version", false, "output version information and exit.")
flag.BoolVar(&dropMode, "D", false, "drop the path before adding it again to the list.")
flag.BoolVar(&noprefixMode, "noprefix", false, "output the variable contents only.")
flag.BoolVar(&listMode, "L", false, "use a newline character as path list separator.")
flag.StringVar(&envVar, "E", "PATH", "input environment variable.")
flag.Usage = usage
flag.CommandLine.SetOutput(os.Stderr)
cmdHandlers = func() map[string]func(pathlist.List) {
hAppend := func(d pathlist.List) {
if dropMode {
d.Drop(flag.Arg(1))
}
d.Append(flag.Arg(1))
}
hDrop := func(d pathlist.List) { d.Drop(flag.Arg(1)) }
hPrepend := func(d pathlist.List) {
if dropMode {
d.Drop(flag.Arg(1))
}
d.Prepend(flag.Arg(1))
}
return map[string]func(pathlist.List){
"append": hAppend,
"drop": hDrop,
"prepend": hPrepend,
// aliases
"a": hAppend,
"d": hDrop,
"p": hPrepend,
}
}()
}
func main() {
log.SetFlags(0)
log.SetPrefix(fmt.Sprintf("%s: ", programme))
log.SetOutput(os.Stderr)
flag.Parse()
handleHelpAndVersionModes()
dirs := pathlist.New()
dirs.LoadEnv(envVar)
if flag.NArg() < 1 {
printPathList(dirs)
os.Exit(0)
}
if handler, ok := cmdHandlers[flag.Arg(0)]; ok {
handler(dirs)
printPathList(dirs)
} else {
log.Fatalf("unrecognized command: %s", flag.Arg(0))
}
}
func printPathList(d pathlist.List) {
var sb = strings.Builder{}
sb.Reset()
printPrefix := !noprefixMode
switch {
case listMode:
sb.WriteString(strings.Join(d.Slice(), "\n"))
break
case printPrefix:
sb.WriteString(fmt.Sprintf("%s=", envVar))
fallthrough
default:
sb.WriteString(d.String())
}
fmt.Println(sb.String())
}
func handleHelpAndVersionModes() {
if !helpMode && !versionMode {
return
}
switch {
case helpMode:
usage()
case versionMode:
version.PrintWithCopyright()
}
os.Exit(0)
}
func usage() {
s := fmt.Sprintf(`Usage: %s [COMMAND [PATH]]
Make the management of the PATH environment variable
simple, fast, and predictable.
Commands:
append, a append a path to the end of the list.
drop, d drop a path.
prepend, p prepend a path to the list.
Options:
`, programme)
_, _ = fmt.Fprintln(os.Stderr, s)
flag.PrintDefaults()
_, _ = fmt.Fprintln(os.Stderr, `
When used with the -D flag, the commands append and prepend
drop PATH before adding it again to the list. This behaviour
guarantees that PATH is added as either the first or the last
element of the path list.
If COMMAND is not provided, it prints the contents of the PATH
environment variable.`)
}