-
Notifications
You must be signed in to change notification settings - Fork 56
/
config.go
182 lines (153 loc) · 3.94 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
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
"github.com/BurntSushi/toml"
"github.com/skratchdot/open-golang/open"
)
const AuthURL = "http://slackcat.chat/configure"
// Slack team and channel read from file
type Config struct {
Teams map[string]string `toml:"teams"`
DefaultTeam string `toml:"default_team"`
DefaultChannel string `toml:"default_channel"`
}
// NewConfig returns new default config
func NewConfig() *Config {
return &Config{
Teams: make(map[string]string),
}
}
// ReadConfig returns config read from file
func ReadConfig(path string) *Config {
config := NewConfig()
lines, err := readLines(path)
failOnError(err, "unable to read config")
// simple config file
if len(lines) == 1 {
config.Teams["default"] = lines[0]
config.DefaultTeam = "default"
return config
}
// advanced config file
body := strings.Join(lines, "\n")
_, err = toml.Decode(body, &config)
failOnError(err, "failed to parse config")
return config
}
func (c *Config) Write(path string) {
cfgdir := basedir(path)
// create config dir if not exist
if _, err := os.Stat(cfgdir); err != nil {
err = os.MkdirAll(cfgdir, 0755)
if err != nil {
exitErr(fmt.Errorf("failed to initialize config dir [%s]: %s", cfgdir, err))
}
}
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
exitErr(fmt.Errorf("failed to open config for writing: %s", err))
}
writer := toml.NewEncoder(file)
err = writer.Encode(c)
if err != nil {
exitErr(fmt.Errorf("failed to write config: %s", err))
}
}
func (c *Config) parseChannelOpt(channel string) (string, string, error) {
// use default channel if none provided
if channel == "" {
if c.DefaultChannel == "" {
return "", "", fmt.Errorf("no channel provided")
}
return c.DefaultTeam, c.DefaultChannel, nil
}
// if channel is prefixed with a team
if strings.Contains(channel, ":") {
s := strings.Split(channel, ":")
return s[0], s[1], nil
}
// use default team with provided channel
return c.DefaultTeam, channel, nil
}
// determine config path from environment
func getConfigPath() (path string, exists bool) {
userHome, ok := os.LookupEnv("HOME")
if !ok {
exitErr(fmt.Errorf("$HOME not set"))
}
path = fmt.Sprintf("%s/.slackcat", userHome) // default path
if xdgSupport() {
xdgHome, ok := os.LookupEnv("XDG_CONFIG_HOME")
if !ok {
xdgHome = fmt.Sprintf("%s/.config", userHome)
}
path = fmt.Sprintf("%s/slackcat/config", xdgHome)
}
if _, err := os.Stat(path); err == nil {
exists = true
}
return path, exists
}
func basedir(path string) string {
parts := strings.Split(path, "/")
return strings.Join((parts[0 : len(parts)-1]), "/")
}
// Test for environemnt supporting XDG spec
func xdgSupport() bool {
re := regexp.MustCompile("^XDG_*")
for _, e := range os.Environ() {
if re.FindAllString(e, 1) != nil {
return true
}
}
return false
}
func configureOA() {
var nick, token string
var config *Config
cfgPath, cfgExists := getConfigPath()
if !cfgExists {
config = NewConfig()
} else {
config = ReadConfig(cfgPath)
}
fmt.Printf("nickname for team: ")
fmt.Scanf("%s", &nick)
if nick == "" {
exitErr(fmt.Errorf("no name provided"))
}
output("creating token request for slackcat")
open.Run(AuthURL)
output("Use the below URL to authorize slackcat if browser fails to launch")
output(AuthURL)
fmt.Printf("token issued: ")
fmt.Scanf("%s", &token)
if token == "" {
exitErr(fmt.Errorf("no token provided"))
}
// creating a new config file
if !cfgExists {
config.DefaultTeam = nick
}
config.Teams[nick] = token
config.Write(cfgPath)
output(fmt.Sprintf("added team to config file at %s", cfgPath))
}
func readLines(path string) (lines []string, err error) {
file, err := os.Open(path)
if err != nil {
return lines, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if scanner.Text() != "" {
lines = append(lines, scanner.Text())
}
}
return lines, nil
}