This repository was archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmain.go
More file actions
154 lines (129 loc) · 3.8 KB
/
main.go
File metadata and controls
154 lines (129 loc) · 3.8 KB
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
// Copyright (c) 2019 Bytedance Inc. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package main
import (
"context"
"flag"
"os"
"strings"
"github.com/jinzhu/configor"
"github.com/larksuite/botframework-go/SDK/common"
"github.com/larksuite/botframework-go/SDK/protocol"
"github.com/larksuite/botframework-go/generatecode"
)
var (
help bool
filePath string
)
func init() {
flag.BoolVar(&help, "h", false, "this help")
flag.StringVar(&filePath, "f", "", "config file path")
}
func main() {
flag.Parse()
if help {
flag.Usage()
return
}
if filePath == "" {
flag.Usage()
return
}
common.InitLogger(common.NewCommonLogger(), common.DefaultOption())
defer common.FlushLogger()
ctx := context.Background()
// read config
config := &generatecode.GenCodeInfo{}
err := configor.Load(config, filePath)
if err != nil {
common.Logger(ctx).Errorf("genConfigError error[%v]confPath[%s]", err, filePath)
return
}
// check config
if config.ServiceInfo.Path == "" ||
config.ServiceInfo.EventWebhook == "" ||
config.ServiceInfo.CardWebhook == "" ||
config.ServiceInfo.AppID == "" {
common.Logger(ctx).Errorf("config error, Path/EventWebhook/CardWebhookAppID can not be empty ")
return
}
// generate code
GenCodeGin(ctx, config)
}
func GenCodeGin(ctx context.Context, config *generatecode.GenCodeInfo) {
mainTpl := generatecode.MainTemplate{
Path: config.ServiceInfo.Path,
GenCodePath: config.ServiceInfo.GenCodePath,
EventWebhook: config.ServiceInfo.EventWebhook,
CardWebhook: config.ServiceInfo.CardWebhook,
AppID: config.ServiceInfo.AppID,
IsISVApp: config.ServiceInfo.IsISVApp,
}
var path string
if mainTpl.GenCodePath != "" {
path = mainTpl.GenCodePath
} else {
GOPATH := os.Getenv("GOPATH")
path = GOPATH + "/src/" + mainTpl.Path
}
// init path
err := generatecode.InitPath(path)
if err != nil {
common.Logger(ctx).Errorf("initPathError[%v]", err)
return
}
common.Logger(ctx).Infof("path=%s", path)
// generatecode main、callback
err = generatecode.GenerateCode(ctx, "tplmain", generatecode.TplGinMain, path, "/main.go", mainTpl, false)
if err != nil {
common.Logger(ctx).Errorf("generateCodeError[%v]", err)
return
}
err = generatecode.GenerateCode(ctx, "tplcallback", generatecode.TplGinCallback, path, "/callback.go", mainTpl, false)
if err != nil {
common.Logger(ctx).Errorf("generateCodeError[%v]", err)
return
}
// generatecode handler_event
eventTpl := &generatecode.EventTemplate{}
for _, v := range config.EventList {
if v.EventName == "AppTicket" {
eventTpl.UseAuth = true
} else if v.EventName == "Message" {
} else {
eventTpl.UseJson = true
}
eventTpl.AddEvent(v.EventName)
}
isHasDefault := false
for _, v := range config.CommandList {
if protocol.CmdDefault == strings.ToLower(v.Cmd) {
isHasDefault = true
}
eventTpl.AddBotCommand(v.Cmd, v.Description)
}
if !isHasDefault {
eventTpl.AddBotCommand(protocol.CmdDefault, protocol.DescDefault)
}
for _, v := range config.CardActionList {
eventTpl.AddCardAction(v.MethodName)
}
err = generatecode.GenerateCode(ctx, "tplregist", generatecode.TplRegist, path, "/handler_event/regist.go", eventTpl, true)
if err != nil {
common.Logger(ctx).Errorf("generateCodeError[%v]", err)
return
}
err = generatecode.GenerateCode(ctx, "tplevent", generatecode.TplEvent, path, "/handler_event/event.go", eventTpl, false)
if err != nil {
common.Logger(ctx).Errorf("generateCodeError[%v]", err)
return
}
err = generatecode.GenerateCode(ctx, "tplCard", generatecode.TplCard, path, "/handler_event/card.go", eventTpl, false)
if err != nil {
common.Logger(ctx).Errorf("generateCodeError[%v]", err)
return
}
common.Logger(ctx).Infof("Success. code in path=%s", path)
return
}