-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
191 lines (156 loc) · 3.31 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
package main
import (
"flag"
"fmt"
"strings"
"github.com/newtoallofthis123/x/db"
"github.com/newtoallofthis123/x/utils"
)
var (
output = flag.String("o", "", "Output file")
help = flag.Bool("h", false, "Show help")
version = flag.Bool("v", false, "Show version")
list = flag.Bool("l", false, "List out all parsed tasks and exit")
add = flag.Bool("a", false, "Add a task to the database")
rename = flag.Bool("r", false, "Rename a task in the database")
del = flag.Bool("d", false, "Delete a task from the database")
sup = flag.Bool("s", false, "Suppress output")
sync = flag.Bool("sync", false, "Sync the database with the current config")
edit = flag.Bool("e", false, "Edit a task")
config = flag.String("c", "exec.conf", "Specify a config file, defaults to exec.conf")
)
func printHelp() {
fmt.Println("=============")
fmt.Println("X v.0.1.2")
fmt.Println("=============")
flag.PrintDefaults()
}
func main() {
flag.Parse()
if *help {
printHelp()
return
}
if *version {
fmt.Println("X v.0.1.3")
return
}
cmd := strings.Join(flag.Args(), " ")
cmd = strings.TrimSpace(cmd)
utils.InitPaths()
if !*add && !*edit && !*del && !*list && !*sync && cmd == "" && !*rename {
printHelp()
return
}
db, err := db.MakeDb(utils.GetDbPath())
if err != nil {
panic(err)
}
err = db.Init()
if err != nil {
panic(err)
}
if *add {
if cmd == "" {
panic("No task provided")
}
name := strings.Split(cmd, " ")[0]
c := strings.Join(strings.Split(cmd, " ")[1:], " ")
sp := strings.Split(c, ",")
cm := strings.Join(sp, "&&")
err = db.AddTask(name, cm)
if err != nil {
panic(err)
}
fmt.Println("Task added")
return
}
if *rename {
if cmd == "" {
panic("No task provided")
}
name := strings.Split(cmd, " ")[0]
ren := strings.Split(cmd, " ")[1]
task, ok := db.GetTask(name)
if !ok {
panic("Task not found")
}
err = db.DeleteTaskByName(name)
if err != nil {
panic(err)
}
err = db.AddTask(ren, task.Cmd)
if err != nil {
panic(err)
}
fmt.Println("Task renamed")
return
}
if *del {
if cmd == "" {
panic("No task provided")
}
err = db.DeleteTask(cmd)
if err != nil {
panic(err)
}
fmt.Println("Task deleted")
return
}
if *edit {
if cmd == "" {
panic("No task provided")
}
name := strings.Split(cmd, " ")[0]
c := strings.Join(strings.Split(cmd, " ")[1:], " ")
sp := strings.Split(c, ",")
cm := strings.Join(sp, "&&")
err = db.UpdateTask(name, cm)
if err != nil {
panic(err)
}
fmt.Println("Task updated")
return
}
cmds, err := utils.CompileTasks(utils.GetConfigPaths(*config), &db)
if err != nil {
panic(err)
}
if *list {
for k, v := range cmds {
fmt.Print(k + ": [" + strings.Join(v, ",") + "]\n")
}
return
}
if *sync {
confirm := ""
fmt.Print("Are you sure you want to sync the database with the current config? (y/n): ")
fmt.Scanln(&confirm)
if confirm != "y" {
return
}
err = db.Truncate()
if err != nil {
panic(err)
}
for k, v := range cmds {
err = db.AddTask(k, strings.Join(v, "&&"))
if err != nil {
panic(err)
}
}
fmt.Println("Database synced")
return
}
if *sup {
*output = "!"
}
if cmds[cmd] != nil {
err = utils.Run(*output, cmds[cmd])
if err != nil {
panic(err)
}
} else {
panic("Task not found")
}
}