-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmajortom.go
452 lines (404 loc) · 11.6 KB
/
majortom.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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"sort"
"strings"
"github.com/gookit/color"
)
const APP_NAME = "majortom"
const APP_VERSION = "1.4.0"
type ConfigDataT struct {
Locations map[string]string `json:"locations"`
}
type ColorT struct {
colorRGB color.RGBStyle
color256 color.Color256
color16 color.Color
}
var colorShortcut = ColorT{
colorRGB: *color.HEXStyle("#ff8000"),
color256: color.C256(208), // Orange
color16: color.Magenta,
}
var colorPath = ColorT{
colorRGB: *color.HEXStyle("#00ffff"),
color256: color.C256(87), // Cyan
color16: color.Cyan,
}
var colorPathDNE = ColorT{
colorRGB: *color.HEXStyle("#808080"),
color256: color.C256(246), // Gray
color16: color.Red,
}
var colorCurrent = ColorT{
colorRGB: *color.HEXStyle("#ffff00"),
color256: color.C256(190), // Yellow
color16: color.Yellow,
}
var colorError = ColorT{
colorRGB: *color.HEXStyle("#ff4040"),
color256: color.C256(198), // Red
color16: color.Red,
}
const (
ColorMode16 = iota
ColorMode256
ColorMode16m
ColorModeNone
)
var colorMode int = ColorMode16
var EXIT_CODE_SUCCESS = 0
var EXIT_CODE_FAIL = 1
var ENV_VAR_CONFIG = "MAJORTOM_CONFIG"
var DEFAULT_CONFIG_PATH = "~/.config/majortom/majortom_config.json"
func main() {
flag.Usage = func() {
w := flag.CommandLine.Output() // may be os.Stderr - but not necessarily
executable := os.Args[0]
fmt.Fprintf(w, "Usage of %s:\n", executable)
fmt.Fprintf(w, "%s [-color=<color mode>|-no-color] <shortcut>\n", APP_NAME)
fmt.Fprintf(w, "%s [-color=<color mode>|-no-color] [-a|-d] <shortcut>\n", APP_NAME)
fmt.Fprintf(w, "%s -h\n", APP_NAME)
fmt.Fprintf(w, "%s -init\n", APP_NAME)
flag.PrintDefaults()
fmt.Fprintf(
w,
"NOTE:\n"+
" majortom is meant to be invoked by the to() helper shell script function.\n"+
" Calling majortom directly will not change your working directory.\n"+
"NOTE:\n"+
" Set the environment variable %s to point to %s's configuration file.\n"+
" If not set, the configuration file path will default to: %s\n"+
"\n",
ENV_VAR_CONFIG, APP_NAME, DEFAULT_CONFIG_PATH)
configPath := os.Getenv(ENV_VAR_CONFIG)
if configPath == "" {
fmt.Fprintf(w, "Currently %s is not set.\n", ENV_VAR_CONFIG)
} else {
fmt.Fprintf(w, "Currently %s is set to \"%s\".\n", ENV_VAR_CONFIG, os.Getenv(ENV_VAR_CONFIG))
}
configPath = getConfigPath()
fmt.Fprintf(w, "Expecting config file to be at: %s\n", configPath)
}
optVersion := flag.Bool("version", false,
"Show version.")
optAdd := flag.Bool("a", false,
"Add current directory path as <shortcut>.")
optDelete := flag.Bool("d", false,
"Delete <shortcut>.")
optInit := flag.Bool("init", false,
"Initialize (create) config file. (Only if config does not exist)")
optNoColor := flag.Bool("no-color", false,
"Disable colorization")
optColor := flag.String("color", "16m",
"Set color mode. Can be set to any of: 16, 256, 16m.")
flag.Parse()
setColorMode(*optNoColor, *optColor)
if *optVersion {
// Show version and exit
fmt.Printf("%s %s\n", APP_NAME, APP_VERSION)
os.Exit(EXIT_CODE_SUCCESS)
}
if *optInit {
// Initialize (create) config file and exit
initConfig()
os.Exit(EXIT_CODE_SUCCESS)
}
args := flag.Args()
if len(args) > 1 {
// Too many args.
fmt.Println("Too many arguments.")
os.Exit(EXIT_CODE_FAIL)
}
config := loadConfig()
if len(args) == 0 {
// With no arguments, show the current list of shortcuts and exit
showShortcuts(config)
os.Exit(EXIT_CODE_SUCCESS)
}
if *optAdd {
// Add a new shortcut
config = addShortcut(config, args[0])
saveConfig(config)
os.Exit(EXIT_CODE_SUCCESS)
}
if *optDelete {
// Delete a shortcut
config = deleteShortcut(config, args[0])
saveConfig(config)
os.Exit(EXIT_CODE_SUCCESS)
}
// Lookup the requested shortcut, and return it prepended by ":"
path := getPath(config, args[0])
path = expandHome(path)
fmt.Printf(":%s\n", path)
os.Exit(EXIT_CODE_SUCCESS)
}
func setColorMode(optNoColor bool, optColor string) {
if optNoColor {
colorMode = ColorModeNone
return
}
switch optColor {
case "16":
colorMode = ColorMode16
case "256":
colorMode = ColorMode256
case "16m":
colorMode = ColorMode16m
}
}
func addShortcut(config ConfigDataT, shortcut string) ConfigDataT {
currentPath, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(EXIT_CODE_FAIL)
}
currentPath = abbreviateHome(currentPath)
config.Locations[shortcut] = currentPath
fmt.Printf("Adding shortcut \"%s\"...\n", shortcut)
return config
}
func deleteShortcut(config ConfigDataT, shortcut string) ConfigDataT {
if _, ok := config.Locations[shortcut]; ok {
delete(config.Locations, shortcut)
} else {
colorPrintFLn(
colorError,
"Shortcut \"%s\" does not exist.",
shortcut)
os.Exit(EXIT_CODE_FAIL)
}
fmt.Printf("Deleting shortcut \"%s\"...\n", shortcut)
return config
}
// Get the network path associated with the requested shortcut.
//
// - The shortcut can be abbreviated.
// - If no match is found then exit as failure.
// - If the requested shortcut does not unambiguously match a SINGLE defined shortcut, then
// print a list of the matching shortcuts and exit as failure.
//
func getPath(config ConfigDataT, shortcut string) string {
paths := make([]string, 0)
matched_keys := make([]string, 0)
for key, path := range config.Locations {
if key == shortcut {
// Always return the path if the key EXACTLY matches the requested shortcut
return path
}
if strings.HasPrefix(key, shortcut) {
paths = append(paths, path)
matched_keys = append(matched_keys, key)
}
}
if len(paths) == 0 {
colorPrintFLn(
colorError,
"No match found for shortcut \"%s\". Run \"to\" with no arguments for a list of shortcuts.",
shortcut)
os.Exit(EXIT_CODE_FAIL)
}
if len(paths) > 1 {
message := "Matched multiple shortcuts: "
for i, key := range matched_keys {
message += colorSprintF(colorShortcut, "%s", key)
if i < len(matched_keys)-1 {
message += ", "
}
}
fmt.Println(message)
os.Exit(EXIT_CODE_FAIL)
}
// Return the (single) matching path
return paths[0]
}
// Print all configured shortcuts
func showShortcuts(config ConfigDataT) {
currentPath, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(EXIT_CODE_FAIL)
}
var maxLen int
shortcuts := make([]string, 0)
for shortcut := range config.Locations {
if len(shortcut) > maxLen {
maxLen = len(shortcut)
}
shortcuts = append(shortcuts, shortcut)
}
if len(shortcuts) == 0 {
fmt.Println(
"No shortcuts exist yet. Use the \"-a <shortcut>\" command " +
"to create your first shortcut.")
return
}
fmt.Println("Available shortcuts:")
sort.Strings(shortcuts)
for _, shortcut := range shortcuts {
path := config.Locations[shortcut]
pathAbsolute := expandHome(path)
if pathAbsolute == currentPath {
colorPrintF(colorCurrent, "▶ %-*s ", maxLen, shortcut)
} else {
colorPrintF(colorShortcut, " %-*s ", maxLen, shortcut)
}
if _, err := os.Stat(pathAbsolute); !os.IsNotExist(err) {
// Path exists
colorPrintFLn(colorPath, "%s", path)
} else {
// Path does not exist
if colorMode == ColorModeNone {
fmt.Printf("%s (DOES NOT EXIST)\n", path)
} else {
colorPrintFLn(colorPathDNE, "%s", path)
}
}
}
}
// Replace '~' in requested path with the current user's home path.
func expandHome(path string) string {
usr, _ := user.Current()
homeDir := usr.HomeDir
if path == "~" {
// In case of "~", which won't be caught by the "else if"
return homeDir
} else if strings.HasPrefix(path, "~/") {
// Use strings.HasPrefix so we don't match paths like
// "/something/~/something/"
return filepath.Join(homeDir, path[2:])
}
return path
}
// Replace the current user's home path within the requested path with `~`.
func abbreviateHome(path string) string {
usr, _ := user.Current()
dir := usr.HomeDir
return strings.Replace(path, dir, "~", 1)
}
// Load the config file and return it as a ConfigDataT object.
// Exits on failure.
func loadConfig() ConfigDataT {
pathConfig := getConfigPath()
if _, err := os.Stat(pathConfig); os.IsNotExist(err) {
colorPrintFLn(
colorError,
"The %s config file (%s) does not exist. You can create it using the -init command.",
APP_NAME,
pathConfig)
os.Exit(EXIT_CODE_FAIL)
}
jsonFile, err := os.Open(pathConfig)
if err != nil {
fmt.Println(err)
os.Exit(EXIT_CODE_FAIL)
}
defer jsonFile.Close()
var config ConfigDataT
byteValue, _ := ioutil.ReadAll(jsonFile)
json.Unmarshal(byteValue, &config)
return config
}
func saveConfig(config ConfigDataT) {
pathConfig := getConfigPath()
file, _ := json.MarshalIndent(config, "", " ")
_ = ioutil.WriteFile(pathConfig, file, 0644)
}
// Initialize the configuration file (IF it does not already exist).
//
// This function will NEVER overwrite an existing config file.
//
func initConfig() {
pathConfig := getConfigPath()
if _, err := os.Stat(pathConfig); !os.IsNotExist(err) {
colorPrintFLn(
colorError,
"The %s config file (%s) already exists. The -init option cannot be used to reinitialize"+
" an existing config file. If you really want to re-create the config file then"+
" delete it manually and re-run this command.",
APP_NAME,
pathConfig)
os.Exit(EXIT_CODE_FAIL)
}
// Make a blank config struct
config := ConfigDataT{}
config.Locations = map[string]string{}
// Create missing config directory (and its parents), if they don't exist
pathConfig = getConfigPath()
parent := filepath.Dir(pathConfig)
if _, err := os.Stat(parent); os.IsNotExist(err) {
// parent does not exist
fmt.Printf("Creating directory: %s\n", parent)
err := os.MkdirAll(parent, 0755)
if err != nil {
fmt.Println(err)
os.Exit(EXIT_CODE_FAIL)
}
}
saveConfig(config)
fmt.Printf(
"A new %s config file has been initialized (created) at:\n %s\n",
APP_NAME,
pathConfig)
}
// Get the path of the current config file (with ~ expanded).
func getConfigPath() string {
path := os.Getenv(ENV_VAR_CONFIG)
if path == "" {
path = DEFAULT_CONFIG_PATH
}
path = expandHome(path)
return path
}
// Print a colorized formatted string
func colorPrintF(textColor ColorT, format string, args ...interface{}) {
switch colorMode {
case ColorMode16m:
textColor.colorRGB.Printf(format, args...)
case ColorMode16:
textColor.color16.Printf(format, args...)
case ColorMode256:
textColor.color256.Printf(format, args...)
case ColorModeNone:
fmt.Printf(format, args...)
}
}
// Print a colorized formatted string, with a terminating linefeed AFTER the color reset escape code.
//
// The output of this application is captured and echoed by a shell script, and the shell doesn't
// recognize that echo'd content ends in a linefeed IF that linefeed occurs BEFORE the
// escape codes used to clear the color. To fix that issue we use this wrapper function which
// will inject a terminating linefeed AFTER the color reset escape code.
//
func colorPrintFLn(textColor ColorT, format string, args ...interface{}) {
switch colorMode {
case ColorMode16m:
fmt.Printf("%s\n", textColor.colorRGB.Sprintf(format, args...))
case ColorMode16:
fmt.Printf("%s\n", textColor.color16.Sprintf(format, args...))
case ColorMode256:
fmt.Printf("%s\n", textColor.color256.Sprintf(format, args...))
case ColorModeNone:
fmt.Printf("%s\n", fmt.Sprintf(format, args...))
}
}
// String print a colorized formatted string
func colorSprintF(textColor ColorT, format string, args ...interface{}) string {
switch colorMode {
case ColorMode16m:
return textColor.colorRGB.Sprintf(format, args...)
case ColorMode16:
return textColor.color16.Sprintf(format, args...)
case ColorMode256:
return textColor.color256.Sprintf(format, args...)
default:
return fmt.Sprintf(format, args...)
}
}