-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (68 loc) · 2.02 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
// Package main provides the main fuctionality for Surbot.
package main
import (
"flag"
"fmt"
"os"
"github.com/spf13/viper"
"gitlab.com/sajfer/surbot/pkg/surbot"
)
type envConfig struct {
Token string `mapstructure:"TOKEN"`
YoutubeAPI string `mapstructure:"YOUTUBE_API"`
SpotifyClientID string `mapstructure:"SPOTIFY_CLIENTID"`
SpotifyClientSecret string `mapstructure:"SPOTIFY_CLIENTSECRET"`
}
// Variables used for command line parameters
var (
Prefix string
EnvConfigs *envConfig
)
func readFromEnvFile() {
viper.SetConfigFile(".env")
viper.SetConfigType("env")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("could not read config, %v\n", err.Error())
}
}
func readEnv(envConfig *envConfig) {
viper.SetEnvPrefix("sur")
viper.AutomaticEnv()
err := viper.BindEnv("token")
if err != nil {
fmt.Printf("could not bind variable, %v\n", err.Error())
}
err = viper.BindEnv("youtube_api")
if err != nil {
fmt.Printf("could not bind variable, %v\n", err.Error())
}
err = viper.BindEnv("spotify_clientid")
if err != nil {
fmt.Printf("could not bind variable, %v\n", err.Error())
}
err = viper.BindEnv("spotify_clientsecret")
if err != nil {
fmt.Printf("could not bind variable, %v\n", err.Error())
}
envConfig.Token = viper.GetString("token")
envConfig.YoutubeAPI = viper.GetString("youtube_api")
envConfig.SpotifyClientID = viper.GetString("spotify_clientid")
envConfig.SpotifyClientSecret = viper.GetString("spotify_clientsecret")
}
func main() {
EnvConfigs = new(envConfig)
if _, err := os.Stat(".env"); err == nil {
readFromEnvFile()
if err := viper.Unmarshal(EnvConfigs); err != nil {
fmt.Printf("could not read envs, %v\n", err.Error())
}
} else {
readEnv(EnvConfigs)
}
flag.StringVar(&Prefix, "p", "!", "Bot Prefix")
flag.Parse()
fmt.Printf("token: %v\n", EnvConfigs.Token)
bot := surbot.NewSurbot(EnvConfigs.Token, EnvConfigs.YoutubeAPI, EnvConfigs.SpotifyClientID, EnvConfigs.SpotifyClientSecret, Prefix)
bot.StartServer()
}