-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
60 lines (48 loc) · 1.35 KB
/
flags.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
package main
import (
"fmt"
"flag"
"github.com/tehmoon/errors"
"text/template"
)
func ParseFlags() (*Options, error) {
flags := &Flags{}
flag.StringVar(&flags.Prefix, "p", "", "Prefix for s3 object keys")
flag.StringVar(&flags.Bucket, "b", "", "Bucket to fetch keys from")
flag.Uint64Var(&flags.Depth, "d", 0, "Calculate directory sizes with specified depth")
flag.StringVar(&flags.Template, "template", `directory {{ .Root }} has size {{ .Size }} and {{ .Files }} files.`, "Go text/template to use when output. Use json or json_indent functions if you want")
flag.Parse()
if flags.Bucket == "" {
return nil, errors.New("Option -b is mandatory")
}
if flags.Template == "" {
return nil, errors.New("Option -template cannot be empty")
}
flags.Template = fmt.Sprintf("%s\n", flags.Template)
tmpl, err := template.New("root").Funcs(functionTemplates).Parse(flags.Template)
if err != nil {
return nil, errors.Wrap(err, "Error parsing template")
}
options := &Options{
Depth: flags.Depth,
Prefix: flags.Prefix,
Bucket: flags.Bucket,
Human: flags.Human,
Template: tmpl,
}
return options, nil
}
type Options struct {
Depth uint64
Prefix string
Bucket string
Human bool
Template *template.Template
}
type Flags struct {
Depth uint64
Prefix string
Bucket string
Human bool
Template string
}