-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
107 lines (89 loc) · 2.15 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
package main
import (
"fmt"
"os"
"strings"
"github.com/Sirupsen/logrus"
"github.com/urfave/cli"
"github.com/s1kx/stackguru/config"
"github.com/s1kx/stackguru/guru"
"github.com/s1kx/stackguru/version"
)
const (
defaultConfigPath = "~/.config/stackguru.toml"
// EnvVarPrefix is the prefix for environment variables
EnvVarPrefix = "STACKGURU"
)
var (
// Version is the current package version
Version string
)
// conf is a local package variable for access to the config from all cli commands
var conf config.Config
func main() {
// Set package version for it to be accessible by subpackages
version.PackageVersion = Version
// Initialize command-line application
app := &cli.App{
Name: "stackguru-go",
Usage: "discord nootropics bot",
Version: version.PackageVersion,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
EnvVars: envVarNames("CONFIG"),
Value: defaultConfigPath,
Usage: "path to configuration (toml format)",
},
&cli.BoolFlag{
Name: "debug",
Aliases: []string{"d"},
EnvVars: envVarNames("DEBUG"),
Usage: "debug mode",
},
},
Before: initApplication,
// Commands: []*cli.Command{
// launchCmd,
// },
Action: runApplication,
}
app.Run(os.Args)
}
func envVarName(name string) string {
return fmt.Sprintf("%s_%s", EnvVarPrefix, strings.ToUpper(name))
}
func envVarNames(names ...string) []string {
res := make([]string, len(names))
for i, name := range names {
res[i] = envVarName(name)
}
return res
}
var logFormatter = logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
}
func initApplication(c *cli.Context) error {
configPath := c.String("config")
debug := c.Bool("debug")
// Configure logger.
logrus.SetFormatter(&logFormatter)
if debug {
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
// Load configuration in to package variable conf.
err := config.Load(configPath, &conf)
if err != nil {
logrus.Fatalf("Error loading configuration: %s", err)
return err
}
return nil
}
func runApplication(c *cli.Context) error {
guru.RunBot(&conf)
return nil
}