-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
240 lines (203 loc) · 6.7 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
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
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"time"
"github.com/gebv/asap-tools/clickup"
clickupAPI "github.com/gebv/asap-tools/clickup/api"
"github.com/gebv/asap-tools/logger"
"github.com/gebv/asap-tools/storage"
"github.com/gebv/asap-tools/version"
"cloud.google.com/go/firestore"
"github.com/kelseyhightower/envconfig"
"go.uber.org/zap"
"google.golang.org/api/option"
"gopkg.in/yaml.v2"
)
var (
onlyShowVersionF = flag.Bool("v", false, "Only shows version.")
onlyShowHelpF = flag.Bool("help", false, "Only shows help.")
clickupCommands = flag.NewFlagSet("clickup", flag.ExitOnError)
clickupDebugExampleSpecF = clickupCommands.Bool("debug-example-spec", false, "Shows an example of a spec of sync in yaml format.")
clickupRecentActivitySyncF = clickupCommands.Bool("recent-activity-sync", false, "Regular procedure for loading changed tasks from ClickUp API and processing.")
clickupDBSyncF = clickupCommands.Bool("db-sync", false, "Foce loads all tasks from the database, loads actual data from the ClickUp API and processing. To use if the spec of sync file has been changed.")
)
func printAllFlagUsage() {
fmt.Println("Available commands and flags:")
for _, item := range []interface {
Name() string
PrintDefaults()
}{clickupCommands} {
fmt.Printf("- command %q with flags:\n", item.Name())
item.PrintDefaults()
}
fmt.Println("And global flags:")
flag.PrintDefaults()
}
func unknownCommandAndExist() {
fmt.Println("Unknown command")
printAllFlagUsage()
log.Fatalln()
}
var (
Ctx context.Context
Cfg *Config
)
func showVersion() {
fmt.Printf("AsapTools cli version %s (%s_%s) git commit %s\n", version.Version, version.Goos, version.Goarch, version.GitCommit)
fmt.Println("Build Date:", version.BuildDate)
fmt.Println("Start Date:", time.Now().UTC().Format(time.RFC3339))
fmt.Println()
fmt.Println("https://github.com/gebv/asap-tools")
}
func showHelp() {
fmt.Println()
envconfig.Usage(configNamespace, &Config{})
fmt.Println()
printAllFlagUsage()
}
func main() {
flag.Parse()
showVersion()
if *onlyShowHelpF {
showHelp()
}
if *onlyShowVersionF || *onlyShowHelpF {
return
}
Cfg = &Config{}
ParseOrPanic(Cfg)
logger.Setup(Cfg.LoggerLevel, Cfg.DevelopLogger)
defer func() {
log.Println("Bye-Bye")
}()
if len(os.Args) < 2 {
unknownCommandAndExist()
}
switch os.Args[1] {
case clickupCommands.Name():
clickupCommands.Parse(os.Args[2:])
default:
unknownCommandAndExist()
}
// TODO : handle term
Ctx = context.Background()
if clickupCommands.Parsed() {
handleClickupCommands()
}
}
func clickupShowDemoSpec() {
spec := &clickup.SyncPreferences{
MirrorTaskRules: []clickup.MirrorTaskSpecification{
{
Name: "<NameRule>",
CondAdd: &clickup.SyncRule_CondOfAdd{
IfInLists: []string{
"https://app.clickup.com/<TeamID>/v/li/<ListID>",
},
},
CondTrackChanges: &clickup.SyncRule_CondTrackChanges{
IfInLists: []string{
"https://app.clickup.com/<TeamID>/v/li/<ListID>",
},
},
SpecAdd: &clickup.SyncRule_SpecOfAdd{
AddToList: "https://app.clickup.com/<TeamID>/v/li/<ListID>",
},
},
},
GlobalMirrorTaskStatuses: clickup.MirrorTaskStatuses{
"draft": clickup.MirrorTaskStatus{},
"open": clickup.MirrorTaskStatus{
SyncEstimateAndDueDateToOrigTask: true,
SetStatusToOriginalTask: "in progress",
},
"wip": clickup.MirrorTaskStatus{
SyncEstimateAndDueDateToOrigTask: true,
SetStatusToOriginalTask: "in progress",
},
"done": clickup.MirrorTaskStatus{
SetStatusToOriginalTask: "ready",
},
},
}
specBytes, _ := yaml.Marshal(spec)
fmt.Println("Example of spec yaml file for sync ClickUp tasks.")
fmt.Println()
fmt.Println(string(specBytes))
fmt.Println()
}
func handleClickupCommands() {
if *clickupDebugExampleSpecF {
clickupShowDemoSpec()
return
}
firestoreOpts := []option.ClientOption{}
if Cfg.Firestore.CredsInlineJSON != "" {
firestoreOpts = append(firestoreOpts, option.WithCredentialsJSON([]byte(Cfg.Firestore.CredsInlineJSON)))
}
client, err := firestore.NewClient(Ctx, Cfg.Firestore.ProjectID, firestoreOpts...)
if err != nil {
zap.L().Error("Failed setup firestore client", zap.Error(err))
return
}
spec := &clickup.SyncPreferences{}
{
specBytes, err := ioutil.ReadFile(Cfg.Clickup.FileSpecSync)
if err != nil {
zap.L().Fatal("Failed read spec of sync file", zap.Error(err), zap.String("file_path", Cfg.Clickup.FileSpecSync))
}
if err := yaml.Unmarshal(specBytes, spec); err != nil {
zap.L().Fatal("Failed decode spec of sync from yaml", zap.String("file_path", Cfg.Clickup.FileSpecSync),
zap.Error(err), zap.String("file_raw", string(specBytes)))
}
}
storage := storage.NewStorage(client)
clickupStorage := clickup.NewStorage(storage)
api := clickupAPI.NewAPI(Cfg.Clickup.ApiToken)
manage := clickup.NewChangeManager(api, clickupStorage)
if *clickupDBSyncF {
teamIDs := spec.AllUsedTeamIDs()
zap.L().Info("[PROCESS_EXISTS_TASKS] processing of existing tasks in the database for teams from spec sync", zap.Any("team_ids", teamIDs))
for _, teamID := range spec.AllUsedTeamIDs() {
manage.ForceSyncForAllTasks(Ctx, spec, teamID)
}
}
if *clickupRecentActivitySyncF {
teamIDs := spec.AllUsedTeamIDs()
zap.L().Info("processing of the last changed tasks (from ClickUp API) for teams from spec sync", zap.Any("team_ids", teamIDs))
for _, teamID := range spec.AllUsedTeamIDs() {
manage.ApplyChangesInTeam(Ctx, spec, teamID)
}
}
}
type Config struct {
DevelopLogger bool `envconfig:"LOG_DEV" default:"false"`
LoggerLevel string `envconfig:"LOG_LEVEL" default:"WARN" desc:"Logging level (availabel DEBUG, INFO, WARN, ERROR)"`
Firestore *FirestoreSettings `envconfig:"FIRESTORE"`
Clickup *ClickupConfig `envconfig:"CLICKUP"`
}
type ClickupConfig struct {
ApiToken string `envconfig:"API_TOKEN" desc:"Token from ClickUp API (follow link https://app.clickup.com/settings/apps)"`
WebhookSecret string `envconfig:"WEBHOOK_SECRET" ignored:"true" desc:"Webhook secret from ClickUp API (follow link https://clickup20.docs.apiary.io/#reference/0/webhooks secion 'Signature')."` // WIP
FileSpecSync string `envconfig:"FILE_SPEC_SYNC"`
}
type FirestoreSettings struct {
CredsInlineJSON string `envconfig:"PRIVATE_KEY_INLINE_JSON" desc:"Inline json file with Google Cloud service account private key."`
ProjectID string `envconfig:"PROJECT_ID" desc:"Google Cloud project ID"`
}
const configNamespace = "ASAPTOOLS"
func ParseOrPanic(cfg *Config) {
err := envconfig.Process(configNamespace, cfg)
if err != nil {
// TODO: custom template
envconfig.Usage(configNamespace, cfg)
fmt.Println()
fmt.Println("Failed process parse config from env:", err)
os.Exit(1)
}
}