-
Notifications
You must be signed in to change notification settings - Fork 1
/
usher.go
167 lines (144 loc) · 3.54 KB
/
usher.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
package main
import (
"fmt"
"log"
"github.com/alecthomas/kong"
"github.com/gavincarr/usher"
)
var CLI struct {
Init struct {
Domain string `arg name:"domain" help:"Domain to be used for new database."`
} `cmd help:"Initialise new usher database for domain."`
Ls struct {
// Glob string `arg optional name:"glob" help:"Code glob of mappings to list."`
} `cmd help:"List current mappings in the usher database."`
Add struct {
Url string `arg name:"url" help:"Url to redirect to."`
Code string `arg optional name:"code" help:"Code to be used for mapping."`
} `cmd help:"Add a new mapping to the usher database."`
Update struct {
Url string `arg name:"url" help:"Url to redirect to."`
Code string `arg name:"code" help:"Code to be updated."`
} `cmd help:"Update the url for an existing mapping in the usher database."`
Rm struct {
Code string `arg name:"code" help:"Code of mapping to remove from the database."`
} `cmd help:"Remove a mapping from the usher database."`
Push struct {
} `cmd help:"Push mappings to the configured backend."`
Root struct {
} `cmd help:"Print the location of the usher root directory."`
Config struct {
} `cmd help:"Print the location of the usher config file."`
DB struct {
} `cmd help:"Print the location of the usher database file."`
}
func main() {
log.SetFlags(0)
ctx := kong.Parse(&CLI)
switch ctx.Command() {
case "init <domain>":
db, err := usher.NewDB(CLI.Init.Domain)
if err != nil {
log.Fatal(err)
}
created, err := db.Init()
if err != nil {
log.Fatal(err)
}
if created {
fmt.Printf("Created new database %q\n", db.DBPath)
} else {
fmt.Printf("Database %q already exists\n", db.DBPath)
}
case "ls":
db, err := usher.NewDB("")
if err != nil {
log.Fatal(err)
}
entries, err := db.List("")
if err != nil {
log.Fatal(err)
}
for _, e := range entries {
fmt.Printf("%-12s %s\n", e.Code, e.Url)
}
case "add <url> <code>":
db, err := usher.NewDB("")
if err != nil {
log.Fatal(err)
}
_, err = db.Add(CLI.Add.Url, CLI.Add.Code)
if err != nil {
if err == usher.ErrCodeExists {
log.Fatalf("Error: code %q already exists in usher database\n", CLI.Add.Code)
} else {
log.Fatal(err)
}
}
case "add <url>":
db, err := usher.NewDB("")
if err != nil {
log.Fatal(err)
}
code, err := db.Add(CLI.Add.Url, "")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Added mapping with code %q\n", code)
case "update <url> <code>":
db, err := usher.NewDB("")
if err != nil {
log.Fatal(err)
}
err = db.Update(CLI.Update.Url, CLI.Update.Code)
if err != nil {
if err == usher.ErrNotFound {
log.Fatalf("Error: code %q not found in usher database\n", CLI.Update.Code)
} else {
log.Fatal(err)
}
}
case "rm <code>":
db, err := usher.NewDB("")
if err != nil {
log.Fatal(err)
}
err = db.Remove(CLI.Rm.Code)
if err != nil {
if err == usher.ErrNotFound {
log.Fatalf("Error: code %q not found in usher database\n", CLI.Rm.Code)
} else {
log.Fatal(err)
}
}
case "root":
db, err := usher.NewDB("")
if err != nil {
log.Fatal(err)
}
fmt.Println(db.Root)
case "config":
db, err := usher.NewDB("")
if err != nil {
log.Fatal(err)
}
fmt.Println(db.ConfigPath)
case "db":
db, err := usher.NewDB("")
if err != nil {
log.Fatal(err)
}
fmt.Println(db.DBPath)
case "push":
db, err := usher.NewDB("")
if err != nil {
log.Fatal(err)
}
err = db.Push()
if err != nil {
log.Fatal(err)
}
default:
log.Fatalf("unknown command %q\n", ctx.Command())
}
}