-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
158 lines (145 loc) · 3.9 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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"time"
"gitlab.com/hhakk/trello-cli/cmd"
"gitlab.com/hhakk/trello-cli/colors"
"gitlab.com/hhakk/trello-cli/config"
"gitlab.com/hhakk/trello-cli/session"
)
func NextFriday() string {
dt := int(time.Now().Weekday())
toFriday := dt - 5
if toFriday <= 0 {
toFriday = 7 + dt
}
nf := time.Now().AddDate(0, 0, toFriday).Format("2006-01-02")
return nf
}
func TrelloUsage() {
w := flag.CommandLine.Output()
prog := os.Args[0]
fmt.Fprintf(
w,
"Usage: "+
colors.Color(prog, colors.GREEN)+
colors.Color(" <command> ", colors.YELLOW)+
colors.Color("[OPTIONS]\n", colors.ORANGE),
)
flag.PrintDefaults()
fmt.Fprintf(w, "\n")
fmt.Fprintf(
w,
colors.Color(prog, colors.GREEN)+
colors.Color(" ls ", colors.YELLOW)+
"{cards, members} "+
colors.Color("[--user]\n", colors.ORANGE)+
" List cards or members of your board.\n"+
" --user string\n\tPrint items related to user.\n\n",
)
fmt.Fprintf(
w,
colors.Color(prog, colors.GREEN)+
colors.Color(" add ", colors.YELLOW)+
colors.Color("-n NAME -l LIST [-d DESCRIPTION] [-t DUE] [-m MEMBER]\n", colors.ORANGE)+
" Add a new card to specified list.\n"+
" Supplied arguments are fuzzy matched.\n"+
" To target user 'bob123' you may simply write '-m bob'\n"+
" -n string\n\tCard name\n"+
" -l string\n\tList name\n"+
" -t string\n\tDue date (YYYY-MM-DD), defaults to next Friday\n"+
" -m string\n\tMember username, defaults to yours\n\n",
)
fmt.Fprintf(
w,
colors.Color(prog, colors.GREEN)+
colors.Color(" rm ", colors.YELLOW)+
colors.Color("-n NAME -l LIST\n", colors.ORANGE)+
" Archive a card in the specified list.\n"+
" Supplied arguments are fuzzy matched.\n"+
" -n string\n\tCard name\n"+
" -l string\n\tList name\n",
)
}
func main() {
flag.Usage = TrelloUsage
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
// config path
cfgdir := filepath.Join(home, ".config/trello-cli")
err = os.MkdirAll(cfgdir, 0750)
if err != nil {
panic(err)
}
cfgp := filepath.Join(cfgdir, "config.json")
// ls: list cards, lists, memebers
lsType := "cards" // ls type, either cards or members
userOnly := false // whether to retrieve user specific info
lsCmd := flag.NewFlagSet("ls", flag.ExitOnError)
lsCmd.StringVar(&lsType, "t", lsType, "type of info retrieved")
lsCmd.BoolVar(&userOnly, "user", userOnly, "whether to retrieve only user info")
// add: add card
name := "" // name of new card
desc := "" // description of card
due := NextFriday() // due date
list := "" // list name
mem := "" // member mem
addCmd := flag.NewFlagSet("add", flag.ExitOnError)
addCmd.StringVar(&list, "l", list, "list name for new card")
addCmd.StringVar(&mem, "m", mem, "assign card to this member (defaults to you)")
addCmd.StringVar(&name, "n", name, "name of new card")
addCmd.StringVar(&desc, "d", desc, "description of new card")
addCmd.StringVar(&due, "t", due, "due date for new card (defaults to next friday)")
// rm: archive a card
rmCmd := flag.NewFlagSet("rm", flag.ExitOnError)
rmCmd.StringVar(&name, "n", name, "name of new card")
rmCmd.StringVar(&list, "l", list, "list name for new card")
flag.Parse()
args := flag.Args()
if len(args) < 1 {
flag.Usage()
os.Exit(2)
}
switch args[0] {
case "ls":
lsCmd.Parse(args[1:])
case "add":
addCmd.Parse(args[1:])
case "rm":
rmCmd.Parse(args[1:])
default:
flag.Usage()
os.Exit(2)
}
var cfg config.Config
err = cfg.Load(cfgp)
if err != nil {
cfg.Input()
err = cfg.Save(cfgp)
if err != nil {
panic(err)
}
}
s, err := session.Init(&cfg)
if err != nil {
panic(err)
}
if lsCmd.Parsed() {
cmd.Ls(s, lsType, userOnly)
} else if addCmd.Parsed() {
err := cmd.Add(s, list, name, desc, due, mem)
if err != nil {
panic(err)
}
} else if rmCmd.Parsed() {
err := cmd.Rm(s, list, name)
if err != nil {
panic(err)
}
}
}