-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
161 lines (144 loc) · 4.34 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
159
160
161
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/joho/godotenv"
)
type subcommand struct {
name string
description string
fs *flag.FlagSet
handler func()
}
type subcommandMap []subcommand
var (
flagSetTrack = flag.NewFlagSet("track", flag.ExitOnError)
flagSetActivities = flag.NewFlagSet("activities", flag.ExitOnError)
flagSetIssues = flag.NewFlagSet("issues", flag.ExitOnError)
flagSetProjects = flag.NewFlagSet("projects", flag.ExitOnError)
flagSetTimelog = flag.NewFlagSet("timelog", flag.ExitOnError)
flagSetNewIssue = flag.NewFlagSet("newissue", flag.ExitOnError)
flagForce bool
flagFull bool
flagIncludeClosed bool
flagParentIssueId uint
flagProjectId uint
flagIssueId int
flagPivot string
flagDecimalTime bool
flagActivityId uint
flagComments string
flagTimeSince string
flagTimeUntil string
flagTitle string
flagTrackerId uint
context *redmineClient
subcommands = []subcommand{
{
name: "activities",
description: "list the available activities",
fs: flagSetActivities,
handler: doListActivities,
},
{
name: "issues",
description: "list the issues in a project",
fs: flagSetIssues,
handler: doListIssues,
},
{
name: "newissue",
description: "creates a new issue",
fs: flagSetNewIssue,
handler: doNewIssue,
},
{
name: "projects",
description: "list the projects in the instance",
fs: flagSetProjects,
handler: doListProjects,
},
{
name: "status",
description: "show the status of the running task",
fs: nil,
handler: doStatus,
},
{
name: "statuses",
description: "lists the issue statuses",
fs: nil,
handler: doIssueStatuses,
},
{
name: "timelog",
description: "list the time entries in a project or an issue",
fs: flagSetTimelog,
handler: doListTimelog,
},
{
name: "track",
description: "track time",
fs: flagSetTrack,
handler: doTrack,
},
{
name: "trackers",
description: "lists the trackers",
fs: nil,
handler: doTrackers,
},
}
)
func init() {
godotenv.Load()
flagSetTrack.IntVar(&flagIssueId, "issue", 0, "The issue ID")
flagSetTrack.UintVar(&flagActivityId, "activity", 0, "The activity ID")
flagSetTrack.StringVar(&flagComments, "comment", "", "The comment to assign")
flagSetTrack.BoolVar(&flagForce, "force", false, "Delete the spool in case of conflict")
flagSetIssues.UintVar(&flagProjectId, "project", 0, "The project ID")
flagSetIssues.BoolVar(&flagIncludeClosed, "closed", false, "Include closed issues")
flagSetTimelog.UintVar(&flagProjectId, "project", 0, "The project to get entries for")
flagSetTimelog.IntVar(&flagIssueId, "issue", 0, "The issue to get entries for")
flagSetTimelog.BoolVar(&flagDecimalTime, "decimal", false, "Use decimal time when reporting")
flagSetTimelog.StringVar(&flagTimeSince, "since", "", "Since when include entries")
flagSetTimelog.StringVar(&flagTimeUntil, "until", "", "Until when include entries")
flagSetTimelog.StringVar(&flagPivot, "pivot", "", "Group entries by a specific field")
flagSetProjects.BoolVar(&flagFull, "full", false, "show extra information per project")
flagSetNewIssue.UintVar(&flagProjectId, "project", 0, "The project to get entries for")
flagSetNewIssue.UintVar(&flagParentIssueId, "parent", 0, "The parent issue for this issue")
flagSetNewIssue.UintVar(&flagTrackerId, "tracker", 0, "The tracker to use for this issue")
flagSetNewIssue.StringVar(&flagTitle, "title", "", "The title for this issue")
}
func main() {
var err error
url := os.Getenv("REDMINE_API_URL")
key := os.Getenv("REDMINE_API_KEY")
if context, err = newContext(url, key); err != nil {
panic(err)
}
if len(os.Args) < 2 {
printUsage()
return
}
for _, subcommand := range subcommands {
if subcommand.name == os.Args[1] {
if subcommand.fs != nil {
subcommand.fs.Parse(os.Args[2:])
}
subcommand.handler()
return
}
}
printUsage()
}
func printUsage() {
fmt.Printf("%s <command> <flags>\n", filepath.Base(os.Args[0]))
fmt.Println("Commands:")
for _, cmd := range subcommands {
fmt.Printf(" %-12s %s\n", cmd.name, cmd.description)
}
fmt.Println()
}