-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgif.go
289 lines (266 loc) · 5.8 KB
/
gif.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
package main
import (
"errors"
"fmt"
"github.com/codegangsta/cli"
"github.com/evoL/gif/config"
"github.com/evoL/gif/store"
"github.com/evoL/gif/version"
"net/url"
"os"
"regexp"
"strings"
)
func main() {
typeFlags := []cli.Flag{
cli.BoolFlag{
Name: "tag, t",
Usage: "Enforces searching by tag.",
},
cli.BoolFlag{
Name: "untagged",
Usage: "Lists only images that have no tag.",
},
cli.BoolFlag{
Name: "local",
Usage: "Lists only images that are local, that is not available remotely.",
},
}
getFlags := append(
typeFlags,
cli.BoolFlag{
Name: "all, a",
Usage: "Gets all matching images.",
},
cli.StringFlag{
Name: "order, sort, s",
Usage: "Specifies the order of images. Must be one of: random, newest, oldest.",
Value: "random",
},
)
removeFlags := append(
typeFlags,
cli.BoolFlag{
Name: "all, a",
Usage: "Removes all matching images.",
},
cli.BoolFlag{
Name: "really",
Usage: "Doesn't ask for confirmation.",
},
)
exportFlags := []cli.Flag{
cli.StringFlag{
Name: "output, o",
Usage: "Target output file. Set to '-' for stdout.",
Value: "-",
},
cli.BoolFlag{
Name: "bundle",
Usage: "Export a bundle containing all images and metadata.",
},
}
importFlags := []cli.Flag{
cli.BoolFlag{
Name: "recursive, r",
Usage: "When importing directories, do it recursively.",
},
}
uploadFlags := append(
typeFlags,
cli.BoolFlag{
Name: "really",
Usage: "Doesn't ask for confirmation.",
},
)
recreateFlags := []cli.Flag{
cli.BoolFlag{
Name: "really",
Usage: "Doesn't ask for confirmation.",
},
}
app := cli.NewApp()
app.Name = "gif"
app.Usage = "a stupid gif manager"
app.Author = "Rafał Hirsz"
app.Email = "[email protected]"
app.Version = version.Version
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Value: "default",
Usage: "Path to the configuration file",
},
}
app.Commands = []cli.Command{
{
Name: "add",
Usage: "Adds an image",
Action: AddCommand,
},
{
Name: "config",
Usage: "Prints the current configuration",
Action: ConfigCommand,
},
{
Name: "export",
Usage: "Exports the database",
Action: ExportCommand,
Flags: exportFlags,
},
{
Name: "import",
Usage: "Imports multiple images into the database",
Action: ImportCommand,
Flags: importFlags,
},
{
Name: "list",
Usage: "Lists stored images",
Action: ListCommand,
Flags: typeFlags,
},
{
Name: "path",
Usage: "Lists paths to images",
Action: PathCommand,
Flags: getFlags,
},
{
Name: "remove",
Aliases: []string{"rm"},
Usage: "Removes images",
Action: RemoveCommand,
Flags: removeFlags,
},
{
Name: "tag",
Usage: "Enables to change tags for images",
Action: TagCommand,
Flags: typeFlags,
},
{
Name: "tags",
Usage: "Lists tags available in the database along with their image count",
Action: TagsCommand,
},
{
Name: "url",
Usage: "Lists URLs of images",
Action: UrlCommand,
Flags: getFlags,
},
{
Name: "upload",
Usage: "Uploads images to a server and saves the URLs for later use",
Action: UploadCommand,
Flags: uploadFlags,
},
{
Name: "recreate",
Usage: "Recreates the database from scratch",
Action: RecreateCommand,
Flags: recreateFlags,
},
}
app.Before = func(c *cli.Context) (err error) {
err = loadConfig(c.String("config"))
return
}
app.Run(os.Args)
}
func ConfigCommand(c *cli.Context) {
config.Global.Print()
}
///////////////////////////////////////////////////////////////////////////////
type locationType int
const (
invalidLocation locationType = iota
fileLocation
directoryLocation
urlLocation
)
func loadConfig(arg string) (err error) {
if arg == "default" {
err = config.Default()
} else {
err = config.Load(arg)
}
if err != nil {
fmt.Println("Error while loading the configuration file: " + err.Error())
}
return
}
func getStore() *store.Store {
s, err := store.Default()
if err != nil {
fmt.Println("Cannot create store: " + err.Error())
os.Exit(1)
}
return s
}
func typeFilter(c *cli.Context) (filter store.Filter) {
if c.Args().Present() {
arg := strings.Join(c.Args(), " ")
if !c.Bool("tag") && regexp.MustCompile("^[0-9a-f]+$").MatchString(arg) {
filter = store.IdOrTagFilter{Id: arg}
} else {
filter = store.TagFilter{Tag: arg}
}
} else if c.Bool("untagged") {
filter = store.UntaggedFilter{}
} else {
filter = store.NullFilter{}
}
if c.Bool("local") {
filter = store.LocalFilter{Filter: filter}
}
return
}
func listFilter(c *cli.Context) store.Filter {
return store.DateOrderer{
Filter: typeFilter(c),
Direction: store.Descending,
}
}
func orderAndLimit(input store.Filter, c *cli.Context) (filter store.Filter) {
switch c.String("order") {
case "random":
filter = store.RandomOrderer{Filter: input}
case "newest":
filter = store.DateOrderer{Filter: input, Direction: store.Descending}
case "oldest":
filter = store.DateOrderer{Filter: input, Direction: store.Ascending}
default:
fmt.Println("Invalid order.")
os.Exit(1)
}
if !c.Bool("all") {
filter = store.Limiter{Filter: filter, Limit: 1}
}
return
}
func parseLocation(location string) (locationType, error) {
if location == "" {
return invalidLocation, errors.New("No location specified")
}
// Check for URL
u, err := url.Parse(location)
if err == nil {
if u.Scheme == "http" || u.Scheme == "https" {
return urlLocation, nil
} else if u.Scheme != "" {
return urlLocation, errors.New("Only HTTP and HTTPS URLs are supported")
}
}
// Check for path
fileInfo, err := os.Stat(location)
if err == nil {
if fileInfo.IsDir() {
return directoryLocation, nil
}
return fileLocation, nil
}
return invalidLocation, errors.New("Invalid location")
}