/
commands.go
147 lines (125 loc) · 3.02 KB
/
commands.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
package utils
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/olekukonko/tablewriter"
)
// Commander Function that handles incoming CLI commands
func Commander(args []string) error {
version := "1.0"
switch args[1] {
case "help":
fallthrough
case "h":
fmt.Println(`
NAME:
quickcd - Change directories using shortcuts
TO CHANGE DIRECTORY USE:
qcd <shortcut-name>
VERSION:
` + version + `
COMMANDS:
add <name> <rel_or_abs_path> Add a shortcut
remove|rm <name> Remove a shortcut
list Shows a list of all available shortcuts
help, h Shows a list of commands or help for one command
version Shows the version of this program
`)
break
case "version":
fmt.Println("Version " + version)
break
case "get":
if len(args) == 4 {
name := args[2]
currentDir := args[3]
qls, err := GetQLs()
if err != nil {
return err
}
for _, ql := range qls {
if ql.Name == name {
// store the current directory as "back" QuickLink
err := AddQL(QuickLink{Name: "back", Path: currentDir})
if err != nil {
return err
}
// print the path. The bash script will change directories afterwards
fmt.Printf("%s", ql.Path)
return nil
}
}
fmt.Printf("%s", "UNDEFINED")
} else {
fmt.Println("Invalid arguments. Use: qcd <name>")
}
break
// ADD COMMAND
case "add":
if len(args) == 4 {
name := args[2]
if name == "add" || name == "list" || name == "remove" || name == "help" {
return errors.New("Sorry, you are not allowed to create a QuickLink with this name, because it's a internal keyword")
}
absFilePath, err := filepath.Abs(args[3])
if err != nil {
return err
}
err = AddQL(QuickLink{Name: name, Path: absFilePath})
if err != nil {
return err
}
fmt.Println("Successfully added QuickLink '" + name + "'")
} else {
fmt.Println(args)
fmt.Println("Invalid arguments. Use: qcd add <name> <path>")
fmt.Println("Name must only be one word")
}
break
// LIST COMMAND
case "list":
qls, err := GetQLs()
if err != nil {
return errors.New("Couldn't read the store file. Try to create a new QuickLink: qcd add <name> <path>")
}
if len(qls) == 0 {
fmt.Println("There are no quicklinks yet")
fmt.Println("Use: qcd add <name> <path>")
return nil
}
var data [][]string
for _, ql := range qls {
// ignore the "back" link because it's automatically generated
if ql.Name == "back" {
continue
}
data = append(data, []string{ql.Name, ql.Path})
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Path"})
for _, v := range data {
table.Append(v)
}
table.Render()
break
// REMOVE COMMAND
case "remove":
fallthrough
case "rm":
if len(args) == 3 {
name := args[2]
err := RemoveQL(name)
if err != nil {
return err
}
fmt.Println("Successfully removed QuickLink '" + name + "'")
} else {
fmt.Println("Invalid arguments. Use: qcd remove|rm <name>")
fmt.Println("Name must only be one word")
}
break
}
return nil
}