-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
171 lines (143 loc) · 3.88 KB
/
main.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
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/SeanLatimer/RecruitPool/handlers"
"github.com/SeanLatimer/RecruitPool/modules"
twitch "github.com/gempir/go-twitch-irc"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
homedir "github.com/mitchellh/go-homedir"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
// Verbose logging
var Verbose = false
var logger *logrus.Logger
const configName = ".recruitpool"
func init() {
pflag.BoolVarP(&Verbose, "verbose", "V", false, "Verbose logging")
}
func main() {
pflag.Parse()
setupLogging()
setupConfigDefaults()
initConfigDir()
if viper.GetString("AuthToken") == "CHANGEME" {
firstRun()
}
client := twitch.NewClient(viper.GetString("Username"), viper.GetString("AuthToken"))
modules.RecruitPool.SetLogger(logger)
messageHandler := &handlers.MessageHandler{
Logger: logger,
Client: client,
}
client.OnNewMessage(messageHandler.Handle)
client.Join(viper.GetString("Channel"))
err := client.Connect()
if err != nil {
logger.Errorf("Error connecting", err)
}
}
func firstRun() {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Recruit Pool First Run")
fmt.Println("---------------------")
fmt.Println()
fmt.Println("Please enter your username")
fmt.Print("Username: ")
username, _ := reader.ReadString('\n')
// convert CRLF to LF
username = strings.Replace(username, "\r\n", "", -1)
viper.Set("Username", username)
// Get AuthToken
fmt.Println()
fmt.Println("Please enter your oauth token")
fmt.Print("OAuthToken: ")
authToken, _ := reader.ReadString('\n')
// convert CRLF to LF
authToken = strings.Replace(authToken, "\r\n", "", -1)
if !strings.HasPrefix(authToken, "oauth:") {
authToken = "oauth:" + authToken
}
viper.Set("AuthToken", authToken)
// Get channel
fmt.Println()
fmt.Println("Please enter your channel")
fmt.Print("Channel: ")
channel, _ := reader.ReadString('\n')
// convert CRLF to LF
channel = strings.Replace(channel, "\r\n", "", -1)
viper.Set("Channel", channel)
home, err := homedir.Dir()
if err != nil {
logger.Fatalf("Error getting home directory", err)
}
configPath := fmt.Sprint(home, "/.recruitpool.yaml")
err = viper.SafeWriteConfigAs(configPath)
if err != nil {
logger.Fatalf("Error saving config", err)
}
logger.Info("Config saved to: ", configPath)
}
func setupConfigDefaults() {
viper.SetDefault("AuthToken", "CHANGEME")
viper.SetDefault("Channel", "CHANGEME")
}
func initConfigDir() {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
logger.Fatalf("Error getting home directory", err)
}
// Search config in home directory with name (without extension).
viper.SetConfigType("yaml")
viper.AddConfigPath(home)
viper.SetConfigName(configName)
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
logger.Info("Using config file:", viper.ConfigFileUsed())
}
}
func setupLogging() {
if logger != nil {
return
}
logger = logrus.New()
if Verbose {
logger.Level = logrus.InfoLevel
} else {
logger.Level = logrus.WarnLevel
}
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
logger.Fatal(err)
}
path := filepath.Join(dir, "recruitpool.log")
rotatedLogsDir := filepath.Join(dir, "logs")
if _, err := os.Stat(rotatedLogsDir); os.IsNotExist(err) {
err = os.MkdirAll(rotatedLogsDir, 0755)
if err != nil {
logger.Fatalf("Failed to create rotated logs directory", err)
}
}
pathWithTimeStamp := filepath.Join(rotatedLogsDir, "recruitpool-%Y%m%d%H%M.log")
writer, err := rotatelogs.New(
pathWithTimeStamp,
rotatelogs.WithLinkName(path),
rotatelogs.WithMaxAge(time.Duration(86400)*time.Second),
rotatelogs.WithRotationTime(time.Duration(604800)*time.Second),
)
if err != nil {
logger.Fatal(err)
}
logger.Hooks.Add(lfshook.NewHook(
writer,
&logrus.TextFormatter{},
))
}