Skip to content

Commit 98d3874

Browse files
committed
Add database schemas and enhance configuration
Add schemas for SlackQuestion, StackExchangeQuestion and StackExchangeUser Read configuration into map[string]string instead into structs Add SessionRefresh for commands depending on configuration Add option to enable or disable functionality of Slack Overflow
1 parent 269ec75 commit 98d3874

14 files changed

+333
-177
lines changed

slack/slack.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package slack
22

3+
var slack *Client
4+
35
// Client Slack API Client resource
46
type Client struct {
57
channel string
@@ -9,7 +11,8 @@ type Client struct {
911

1012
// Load slack
1113
func Load() *Client {
12-
return &Client{}
14+
slack = &Client{}
15+
return slack
1316
}
1417

1518
// SetToken set Slack API Token

slackoverflow/application.go

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ type Application struct {
4747
func (so *Application) Run() {
4848
so.Banner()
4949

50-
// Check configuration
51-
if !so.config.Validate() {
52-
so.config.Reconfigure()
53-
}
54-
5550
// Handle call
5651
if _, err := parser.Parse(); err != nil {
5752
// Failure was fine since -h or --help flag was provided
@@ -103,16 +98,22 @@ func (so *Application) Close(code int) {
10398
// SessionRefresh refresh session and makes sure that all deps are loaded
10499
func (so *Application) SessionRefresh() {
105100

101+
// Check configuration
102+
if !so.config.IsConfigured() {
103+
Emergency("You must execute 'slackoverflow reconfigure' or correct errors in ~/.slackoverflow/slackoverflow.yaml")
104+
}
105+
106106
// Set Log Level from -v or -d flag default to config.Data.SlackOverflow.LogLevel
107107
UpdateLogLevel()
108108

109109
// Load Slack Client
110-
if so.Slack != nil {
110+
if so.Slack == nil {
111111
// Configure slack
112112
so.Slack = slack.Load()
113113
so.Slack.SetHost(so.config.Slack.APIHost)
114114
so.Slack.SetToken(so.config.Slack.Token)
115115
so.Slack.SetChannel(so.config.Slack.Channel)
116+
Debug("Slack Client is loaded.")
116117
} else {
117118
Debug("Slack Client is already loaded.")
118119
}
@@ -128,42 +129,49 @@ func (so *Application) SessionRefresh() {
128129
Ok("SQLite3 Database loaded: %s", so.databaseFile)
129130

130131
// Check does the StackQuestion table exist
131-
err = so.SQLite3.VerifyTable("StackQuestion")
132+
err = so.SQLite3.VerifyTable("StackExchangeQuestion")
132133
if err != nil {
133-
Emergency("Table StackQuestion: %q", err)
134+
Emergency("Table StackExchangeQuestion: %q", err)
134135
}
135-
Ok("Table: StackQuestion exists.")
136+
Ok("Table: StackExchangeQuestion exists.")
136137

137-
// Check does the StackQuestionLink table exist
138-
err = so.SQLite3.VerifyTable("StackQuestionLink")
138+
// Check does the SlackQuestion table exist
139+
err = so.SQLite3.VerifyTable("SlackQuestion")
139140
if err != nil {
140-
Emergency("Table StackQuestionLink: %q", err)
141+
Emergency("Table SlackQuestion: %q", err)
141142
}
142-
Ok("Table: StackQuestionLink exists.")
143+
Ok("Table: SlackQuestion exists.")
143144

144-
// Check does the StackUser table exist
145-
err = so.SQLite3.VerifyTable("StackUser")
145+
// Check does the StackExchangeUser table exist
146+
err = so.SQLite3.VerifyTable("StackExchangeUser")
146147
if err != nil {
147-
Emergency("Table StackUser: %q", err)
148+
Emergency("Table StackExchangeUser: %q", err)
148149
}
149-
Ok("Table: StackQuestionLink exists.")
150-
150+
Ok("Table: StackExchangeUser exists.")
151151
} else {
152-
Ok("Table: StackUser exists.")
152+
Ok("SQLite3 Database is already loaded.")
153153
}
154154

155155
// Load Stack Exchange Client
156156
// Load Slack Client
157-
if so.StackExchange != nil {
157+
if so.StackExchange == nil {
158158
// Configure slack
159159
so.StackExchange = stackexchange.Load()
160-
so.StackExchange.SetHost(so.config.StackExchange.APIVersion)
160+
so.StackExchange.SetHost(so.config.StackExchange.APIHost)
161+
so.StackExchange.SetAPIVersion(so.config.StackExchange.APIVersion)
162+
so.StackExchange.SetAPIKey(so.config.StackExchange.Key)
161163

164+
Debug("Stack Exchange Client is loaded.")
162165
} else {
163-
Debug("Slack Client is already loaded.")
166+
Debug("Stack Exchange Client is already loaded.")
164167
}
165168
}
166169

170+
// Debugging Either is debugging enabled or not
171+
func (so *Application) Debugging() bool {
172+
return so.logLevel == 100
173+
}
174+
167175
// Start session
168176
func Start() *Application {
169177
gid := os.Getegid()

slackoverflow/cmd-config.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ type cmdConfig struct{}
1212

1313
func (a *cmdConfig) Execute(args []string) error {
1414

15+
// Refresh the session before running this command and make sure that Slack Overflow is configured
16+
slackoverflow.SessionRefresh()
17+
1518
selfConfig := std.NewTable("SlackOverflow Configuration", " ")
1619
selfConfig.AddRow("Log Level", slackoverflow.config.SlackOverflow.LogLevel)
1720
selfConfig.AddRow("Number of Questions to watch", strconv.Itoa(slackoverflow.config.SlackOverflow.Watch))
@@ -43,8 +46,8 @@ func (a *cmdConfig) Execute(args []string) error {
4346

4447
stackexchange.AddRow("Key", slackoverflow.config.StackExchange.Key)
4548

46-
stackexchange.AddRow("Site", slackoverflow.config.StackExchange.Parameters.Site)
47-
stackexchange.AddRow("Tagged", slackoverflow.config.StackExchange.Parameters.Tagged)
49+
stackexchange.AddRow("Site", slackoverflow.config.StackExchange.SearchAdvanced["site"])
50+
stackexchange.AddRow("Tagged", slackoverflow.config.StackExchange.SearchAdvanced["tagged"])
4851
stackexchange.Print()
4952

5053
return nil

slackoverflow/cmd-reconfigure.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ type cmdReconfigure struct{}
88

99
func (a *cmdReconfigure) Execute(args []string) error {
1010

11-
doReconfigure := std.AskForConfirmation("This will overwrite current configuration, Are you sure you want to continue?")
11+
doReconfigure := std.AskForConfirmation("Start interactive confguration?")
1212
if doReconfigure {
13-
slackoverflow.config.Reconfigure()
13+
slackoverflow.config.ReconfigureAll()
1414
Ok("Reconfiguration done")
1515
} else {
1616
Warning("Reconfiguration canceled!")

slackoverflow/cmd-restart.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@ package slackoverflow
33
// slackoverflow restart
44
// Restart SlackOverflow daemon.
55
type cmdRestart struct{}
6+
7+
func (a *cmdRestart) Execute(args []string) error {
8+
9+
// Refresh the session before running this command and make sure that Slack Overflow is configured
10+
slackoverflow.SessionRefresh()
11+
12+
return nil
13+
}

slackoverflow/cmd-slack.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ type cmdSlackChannels struct{}
99

1010
// Execute
1111
func (slack *cmdSlackChannels) Execute(args []string) error {
12-
// Refresh the session before running this command
12+
13+
// Refresh the session before running this command and make sure that Slack Overflow is configured
14+
slackoverflow.SessionRefresh()
15+
1316
slackoverflow.Slack.ListChannels()
1417
return nil
1518
}

slackoverflow/cmd-start.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@ package slackoverflow
33
// slackoverflow start
44
// Start SlackOverflow daemon.
55
type cmdStart struct{}
6+
7+
func (a *cmdStart) Execute(args []string) error {
8+
9+
// Refresh the session before running this command and make sure that Slack Overflow is configured
10+
slackoverflow.SessionRefresh()
11+
12+
return nil
13+
}

slackoverflow/cmd-watch.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@ package slackoverflow
33
// slackoverflow watch
44
// SlackOverflow .
55
type cmdWatch struct{}
6+
7+
func (a *cmdWatch) Execute(args []string) error {
8+
9+
// Refresh the session before running this command and make sure that Slack Overflow is configured
10+
slackoverflow.SessionRefresh()
11+
12+
return nil
13+
}

0 commit comments

Comments
 (0)