-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
65 lines (53 loc) · 1.71 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
package main
import (
"fmt"
"os"
"path"
"runtime"
"github.com/StAndrewsRadio/starbot-admin/cmd/program"
"github.com/StAndrewsRadio/starbot-admin/vars"
"github.com/bwmarrin/discordgo"
"github.com/jessevdk/go-flags"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
)
var GlobalOptions struct {
Debug bool `short:"d" long:"debug" description:"Displays debug messages"`
LogFile string `short:"l" long:"log-file" description:"Writes output to the file in addition to stdout"`
}
func logWrapper(_, _ int, format string, a ...interface{}) {
logrus.Debugf(format, a)
}
func init() {
logrus.SetReportCaller(true)
logrus.SetFormatter(&logrus.TextFormatter{CallerPrettyfier: func(frame *runtime.Frame) (string, string) {
return "", ""
}})
frameLogger := func(frame *runtime.Frame) (string, string) {
filename := path.Base(frame.File)
return fmt.Sprintf("%s()", frame.Function), fmt.Sprintf("%s:%d", filename, frame.Line)
}
for num, arg := range os.Args {
if arg == "-d" || arg == "--debug" {
logrus.SetLevel(logrus.DebugLevel)
logrus.Debug("Debug logging enabled!")
logrus.SetFormatter(&logrus.TextFormatter{CallerPrettyfier: frameLogger})
} else if arg == "-l" || arg == "--log-file" {
logrus.AddHook(lfshook.NewHook(os.Args[num+1], &logrus.TextFormatter{CallerPrettyfier: frameLogger}))
logrus.Infof("Logging to \"%s\".", os.Args[num+1])
}
}
discordgo.Logger = logWrapper
logrus.Debug("Loading parser...")
vars.Parser = flags.NewParser(&GlobalOptions, flags.Default)
program.RegisterCommands()
}
func main() {
if _, err := vars.Parser.Parse(); err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
os.Exit(1)
}
}
}