-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathconfig.go
234 lines (192 loc) · 4.41 KB
/
config.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package slackbot
import (
"encoding/json"
"log"
"os"
"os/user"
"path/filepath"
"strings"
)
// Config is the state of the build bot
type Config interface {
// Paused will prevent any commands from running
Paused() bool
// SetPaused changes paused
SetPaused(paused bool)
// DryRun will print out what it plans to do without doing it
DryRun() bool
// SetDryRun changes dry run
SetDryRun(dryRun bool)
// Save persists config
Save() error
}
type config struct {
// These must be public for json serialization.
DryRunField bool
PausedField bool
}
// Paused if paused
func (c config) Paused() bool {
return c.PausedField
}
// DryRun if dry run enabled
func (c config) DryRun() bool {
return c.DryRunField
}
// SetPaused changes paused
func (c *config) SetPaused(paused bool) {
c.PausedField = paused
}
// SetDryRun changes dry run
func (c *config) SetDryRun(dryRun bool) {
c.DryRunField = dryRun
}
func getConfigPath() (string, error) {
currentUser, err := user.Current()
if err != nil {
return "", err
}
return filepath.Join(currentUser.HomeDir, ".keybot"), nil
}
// NewConfig returns default config
func NewConfig(dryRun, paused bool) Config {
return &config{
DryRunField: dryRun,
PausedField: paused,
}
}
// ReadConfigOrDefault returns config stored or default
func ReadConfigOrDefault() Config {
cfg := readConfigOrDefault()
return &cfg
}
func readConfigOrDefault() config {
defaultConfig := config{
DryRunField: true,
PausedField: false,
}
path, err := getConfigPath()
if err != nil {
return defaultConfig
}
fileBytes, err := os.ReadFile(path)
if err != nil {
return defaultConfig
}
var cfg config
err = json.Unmarshal(fileBytes, &cfg)
if err != nil {
log.Printf("Couldn't read config file: %s\n", err)
return defaultConfig
}
return cfg
}
func (c config) Save() error {
b, err := json.Marshal(c)
if err != nil {
return err
}
path, err := getConfigPath()
if err != nil {
return err
}
err = os.WriteFile(path, b, 0644)
if err != nil {
return err
}
return nil
}
// NewShowConfigCommand returns command that shows config
func NewShowConfigCommand(config Config) Command {
return &showConfigCommand{config: config}
}
type showConfigCommand struct {
config Config
}
func (c showConfigCommand) Run(_ string, _ []string) (string, error) {
if !c.config.Paused() && !c.config.DryRun() {
return "I'm running normally.", nil
}
lines := []string{}
if c.config.Paused() {
lines = append(lines, "I'm paused.")
}
if c.config.DryRun() {
lines = append(lines, "I'm in dry run mode.")
}
return strings.Join(lines, " "), nil
}
func (c showConfigCommand) ShowResult() bool {
return true
}
func (c showConfigCommand) Description() string {
return "Shows config"
}
// NewToggleDryRunCommand returns toggle dry run command
func NewToggleDryRunCommand(config Config) Command {
return &toggleDryRunCommand{config: config}
}
type toggleDryRunCommand struct {
config Config
}
func (c *toggleDryRunCommand) Run(_ string, _ []string) (string, error) {
c.config.SetDryRun(!c.config.DryRun())
err := c.config.Save()
if err != nil {
return "", err
}
if c.config.DryRun() {
return "We are in dry run mode.", nil
}
return "We are not longer in dry run mode", nil
}
func (c toggleDryRunCommand) ShowResult() bool {
return true
}
func (c toggleDryRunCommand) Description() string {
return "Toggles the dry run mode"
}
// NewPauseCommand pauses
func NewPauseCommand(config Config) Command {
return &pauseCommand{
config: config,
pauses: true,
}
}
// NewResumeCommand resumes
func NewResumeCommand(config Config) Command {
return &pauseCommand{
config: config,
pauses: false,
}
}
type pauseCommand struct {
config Config
pauses bool
}
// Run toggles the dry run state. (Itself is never run under dry run mode)
func (c *pauseCommand) Run(_ string, _ []string) (string, error) {
log.Printf("Setting paused: %v\n", c.pauses)
c.config.SetPaused(c.pauses)
err := c.config.Save()
if err != nil {
return "", err
}
if c.config.Paused() {
return "I am paused.", nil
}
return "I have resumed.", nil
}
// ShowResult always shows results for toggling dry run
func (c pauseCommand) ShowResult() bool {
return true
}
// Description describes what it does
func (c pauseCommand) Description() string {
if c.pauses {
return "Pauses the bot"
}
return "Resumes the bot"
}