-
Notifications
You must be signed in to change notification settings - Fork 1
/
grue.go
156 lines (144 loc) · 3.17 KB
/
grue.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
package main
import (
"errors"
"flag"
"fmt"
"os"
"sort"
"github.com/c-14/grue/config"
)
const version = "0.3.1-next"
func usage() string {
return `usage: grue [--help] {add|delete|fetch|import|init_cfg|list|rename} ...
Subcommands:
add <name> <url>
delete <name>
fetch [-init] [name]
import <config>
init_cfg
list [name] [--full]
rename <old> <new>`
}
func add(args []string, conf *config.GrueConfig) error {
if len(args) != 2 {
return errors.New("usage: grue add <name> <url>")
}
var name string = args[0]
var uri string = args[1]
return conf.AddAccount(name, uri)
}
func del(args []string, conf *config.GrueConfig) error {
if len(args) != 1 {
return errors.New("usage: grue delete <name>")
}
name := args[0]
if err := conf.DeleteAccount(name); err != nil {
return err
}
return DeleteHistory(name)
}
func fetch(args []string, conf *config.GrueConfig) error {
var initFlag bool
fetchCmd := flag.NewFlagSet("fetch", flag.ContinueOnError)
fetchCmd.BoolVar(&initFlag, "init", false, "Don't send emails, only initialize database of read entries")
if err := fetchCmd.Parse(os.Args[2:]); err != nil {
return err
}
if len(fetchCmd.Args()) == 0 {
return fetchFeeds(conf, initFlag)
}
return fetchName(conf, fetchCmd.Arg(0), initFlag)
}
func list(args []string, conf *config.GrueConfig) error {
const (
fmtShort = "%s\t%s\n"
fmtFull = "%s:\n%s\n"
)
var full bool
var listCmd = flag.NewFlagSet("list", flag.ContinueOnError)
listCmd.BoolVar(&full, "full", false, "Show full account info")
if err := listCmd.Parse(args); err != nil {
return err
}
if len(listCmd.Args()) == 0 {
var keys []string
for k := range conf.Accounts {
keys = append(keys, k)
}
sort.Strings(keys)
if full {
for _, k := range keys {
fmt.Printf(fmtFull, k, conf.Accounts[k])
}
return nil
}
for _, k := range keys {
fmt.Printf(fmtShort, k, conf.Accounts[k].URI)
}
return nil
}
name := listCmd.Args()[0]
if cfg, ok := conf.Accounts[name]; ok {
if full {
fmt.Printf(fmtFull, name, cfg)
} else {
fmt.Printf(fmtShort, name, cfg.URI)
}
}
return nil
}
func rename(args []string, conf *config.GrueConfig) error {
if len(args) != 2 {
return errors.New("usage: grue rename <old> <new>")
}
old := args[0]
new := args[1]
if err := conf.RenameAccount(old, new); err != nil {
return err
}
return RenameHistory(old, new)
}
func main() {
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, usage())
os.Exit(EX_USAGE)
}
conf, err := config.ReadConfig()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(EX_TEMPFAIL)
}
defer conf.Unlock()
switch cmd := os.Args[1]; cmd {
case "add":
err = add(os.Args[2:], conf)
case "delete":
err = del(os.Args[2:], conf)
case "fetch":
err = fetch(os.Args[2:], conf)
case "import":
err = config.ImportCfg(os.Args[2:])
case "init_cfg":
break
case "list":
err = list(os.Args[2:], conf)
break
case "rename":
err = rename(os.Args[2:], conf)
case "-v":
fallthrough
case "--version":
fmt.Println(version)
case "-h":
fallthrough
case "--help":
fmt.Println(usage())
default:
fmt.Fprintln(os.Stderr, usage())
conf.Unlock()
os.Exit(EX_USAGE)
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}