forked from aframevr/slackoverflow
/
reconfigure.go
163 lines (138 loc) · 5.11 KB
/
reconfigure.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
// Copyright © 2016 -2017 A-Frame authors.
// Use of this source code is governed by a MIT License
// that can be found in the LICENSE file.
package commands
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/howi-ce/howi/addon/application/plugin/cli"
"github.com/howi-ce/howi/std/errors"
"github.com/mkungla/slackoverflow/cmd/slackoverflow/internal"
"github.com/nlopes/slack"
)
// Reconfigure command for SlackOverflow
func Reconfigure(so *internal.SlackOverflow) cli.Command {
cmd := cli.NewCommand("reconfigure")
cmd.SetShortDesc("Interactive configuration of stackoverflow.")
cmd.Do(func(w *cli.Worker) {
err := so.Load(w)
if err != nil {
w.Fail(err.Error())
return
}
ok, _ := so.IsConfigured()
w.Log.Okf("path set to %q", so.Path.Abs())
if ok {
w.Log.Warning("SlcakOverflow seems to be configured and proceeding will override existing configuration.")
printConfig(w, so)
if c := w.AskForConfirmation("Start interactive confguration?"); !c {
w.Log.Ok("Reconfigure canceled by user")
return
}
}
err = os.MkdirAll(so.Path.Abs(), os.ModePerm)
if err != nil {
w.Fail(err.Error())
return
}
slack := w.AskForConfirmation("Would you like to Configure Slack?")
if slack {
err := configureSlack(w, so)
if err != nil {
w.Fail(err.Error())
return
}
w.Log.Ok("Slack Slack API Client is configured")
}
stackexchange := w.AskForConfirmation("Would you like to Configure Stack Exchange?")
if stackexchange {
configureStackExchange(w, so)
w.Log.Ok("Slack Stack Exchange API Client is configured")
}
w.Log.Ok("configuration done")
})
return cmd
}
// ConfigureSlack for SlackOverflow
func configureSlack(w *cli.Worker, so *internal.SlackOverflow) error {
w.Log.Notice("Configuring Slack API Client")
// Set Slack Defaults
so.Config.Slack.SetAPIhost("https://slack.com/api")
so.Config.Slack.Enable()
reader := bufio.NewReader(os.Stdin)
w.Log.Line("Enter your @stackoverflow Slack BOT API Token.")
w.Log.Line("You can create a bot and get token at https://<your-team>.slack.com/apps/manage/custom-integrations")
token, _ := reader.ReadString('\n')
so.Config.Slack.SetToken(strings.TrimSpace(token))
w.Log.Line("Fetching available Slack channels.")
api := slack.New(so.Config.Slack.Token)
channels, err := api.GetChannels(true)
if err != nil {
return err
}
if len(channels) > 0 {
chlist := internal.NewTable("ID", "Name", "Created")
for _, channel := range channels {
chlist.AddRow(
channel.ID,
channel.Name,
fmt.Sprintf("%d", channel.Created),
)
}
chlist.Print()
} else {
return errors.New("Unable to fetch any channels with provided credentials")
}
w.Log.Line("Enter Channel ID which you want to post the questions")
ch, _ := reader.ReadString('\n')
ch = strings.TrimSpace(ch)
so.Config.Slack.SetChannel(ch)
for _, channel := range channels {
if channel.ID == ch {
so.Config.Slack.SetChannelName(channel.Name)
}
}
team, err := api.GetTeamInfo()
if err != nil {
return err
}
so.Config.Slack.SetTeamInfo(team)
return so.Config.Save()
}
// ConfigureStackExchange for SlackOverflow
func configureStackExchange(w *cli.Worker, so *internal.SlackOverflow) error {
w.Log.Notice("Configuring Stack Exchange API Client")
reader := bufio.NewReader(os.Stdin)
if so.Config.StackExchange.SearchAdvanced == nil {
so.Config.StackExchange.SearchAdvanced = make(map[string]string)
}
so.Config.StackExchange.Enable()
so.Config.StackExchange.SetAPIhost("https://api.stackexchange.com")
so.Config.StackExchange.SetAPIVersion("2.2")
w.Log.Line("Name one Stack Exchange site where you want to track questions from e.g: stackoverflow.")
w.Log.Line("For full list of available sites check: http://stackexchange.com/sites")
site, _ := reader.ReadString('\n')
so.Config.StackExchange.Site = strings.TrimSpace(site)
w.Log.Line("Set tag which you want to track from selected site.")
w.Log.Line("You can also set multible tags separated with (;) e.g: aframe;three.js")
tagged, _ := reader.ReadString('\n')
so.Config.StackExchange.SearchAdvanced["tagged"] = strings.TrimSpace(tagged)
// Number of questions to watch
w.Log.Line("Set the value for how many latest questions you want to track and update.")
w.Log.Line("Good value is (25) which means that besides checking new qustions in defined stack exchange site")
w.Log.Line("also last (n) questions will be checked for comment count, view count, answer count, score and is question accepted or not.")
w.Log.Line("Emoijs of these stats will be removed from older than (n) questions.")
fmt.Scan(&so.Config.StackExchange.QuestionsToWatch)
w.Log.Line("Without having Stack Exchange API APP key's you can make 300 requests per day.")
w.Log.Line("When you register for an Stack Exchange API App Key you can make 10000 requests per day")
w.Log.Line("You can register for an APP KEY here: http://stackapps.com/apps/oauth/register")
registerStackExchange := w.AskForConfirmation("Do you want to set keys now?")
if registerStackExchange {
w.Log.Line("Enter your Client Key")
clientKey, _ := reader.ReadString('\n')
so.Config.StackExchange.Key = strings.TrimSpace(clientKey)
}
return so.Config.Save()
}