forked from xen0n/go-workwx
/
flags.go
143 lines (116 loc) · 3.24 KB
/
flags.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
package commands
import (
"fmt"
"net/http"
"os"
"github.com/urfave/cli/v2"
"github.com/bbang94/go-workwx"
)
const (
flagCorpID = "corpid"
flagCorpSecret = "corpsecret"
flagAgentID = "agentid"
flagWebhookKey = "webhook-key"
flagQyapiHostOverride = "qyapi-host-override"
flagTLSKeyLogFile = "tls-key-logfile"
flagName = "name"
flagOwner = "owner"
flagUser = "user"
flagChatID = "chatid"
flagMessageType = "message-type"
flagSafe = "safe"
flagToUser = "to-user"
flagToUserShort = "u"
flagToParty = "to-party"
flagToPartyShort = "p"
flagToTag = "to-tag"
flagToTagShort = "t"
flagToChat = "to-chat"
flagToChatShort = "c"
flagMediaID = "media-id"
flagThumbMediaID = "thumb-media-id"
flagDescription = "desc"
flagTitle = "title"
flagAuthor = "author"
flagURL = "url"
flagPicURL = "pic-url"
flagButtonText = "button-text"
flagSourceContentURL = "source-content-url"
flagDigest = "digest"
flagMediaType = "media-type"
flagMentionUser = "mention-user"
flagMentionMobile = "mention-mobile"
flagMentionMobileShort = "m"
)
type cliOptions struct {
CorpID string
CorpSecret string
AgentID int64
WebhookKey string
QYAPIHostOverride string
TLSKeyLogFile string
}
func mustGetConfig(c *cli.Context) *cliOptions {
return &cliOptions{
CorpID: c.String(flagCorpID),
CorpSecret: c.String(flagCorpSecret),
AgentID: c.Int64(flagAgentID),
WebhookKey: c.String(flagWebhookKey),
QYAPIHostOverride: c.String(flagQyapiHostOverride),
TLSKeyLogFile: c.String(flagTLSKeyLogFile),
}
}
//
// impl cliOptions
//
func (c *cliOptions) makeHTTPClient() *http.Client {
if c.TLSKeyLogFile == "" {
return http.DefaultClient
}
f, err := os.OpenFile(c.TLSKeyLogFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
fmt.Printf("can't open TLS key log file for writing: %+v\n", err)
panic(err)
}
fmt.Fprintf(f, "# SSL/TLS secrets log file, generated by go\n")
return &http.Client{
Transport: newTransportWithKeyLog(f),
}
}
func (c *cliOptions) makeWorkwxClient() *workwx.Workwx {
if c.CorpID == "" {
panic("corpid must be set")
}
if c.CorpSecret == "" {
panic("corpsecret must be set")
}
if c.AgentID == 0 {
panic("agentid must be set (for now; may later lift the restriction)")
}
httpClient := c.makeHTTPClient()
if c.QYAPIHostOverride != "" {
// wtf think of a way to change this
return workwx.New(c.CorpID,
workwx.WithQYAPIHost(c.QYAPIHostOverride),
workwx.WithHTTPClient(httpClient),
)
}
return workwx.New(c.CorpID, workwx.WithHTTPClient(httpClient))
}
func (c *cliOptions) MakeWorkwxApp() *workwx.WorkwxApp {
return c.makeWorkwxClient().WithApp(c.CorpSecret, c.AgentID)
}
func (c *cliOptions) makeWebhookClient() *workwx.WebhookClient {
if c.WebhookKey == "" {
panic("webhook key must be set")
}
httpClient := c.makeHTTPClient()
if c.QYAPIHostOverride != "" {
// wtf think of a way to change this
return workwx.NewWebhookClient(c.WebhookKey,
workwx.WithQYAPIHost(c.QYAPIHostOverride),
workwx.WithHTTPClient(httpClient),
)
}
return workwx.NewWebhookClient(c.WebhookKey, workwx.WithHTTPClient(httpClient))
}