/
main.go
179 lines (150 loc) · 4.03 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
172
173
174
175
176
177
178
179
package main
import (
"flag"
"fmt"
nested "github.com/antonfisher/nested-logrus-formatter"
"github.com/elivlo/SpotifyHistorySaver/login"
"github.com/elivlo/SpotifyHistorySaver/models"
"github.com/elivlo/SpotifyHistorySaver/spotifySaver"
"github.com/gobuffalo/envy"
"github.com/gobuffalo/packr/v2"
"github.com/gobuffalo/pop/v5"
"github.com/sirupsen/logrus"
"os"
"os/signal"
"strings"
"sync"
)
const (
// EnvClientID is the env variable name for spotify client id
EnvClientID = "CLIENT_ID"
// EnvClientSecret is the env variable name for spotify client secret
EnvClientSecret = "CLIENT_SECRET"
// GoEnv is the env variable that defines in which stage the app is running
// (development/production/test)
GoEnv = "GO_ENV"
// CallbackURI is the URL used to log in to the spotify account
CallbackURI = "http://localhost:8080/callback"
)
var (
log *logrus.Entry
env string
clientID string
clientSecret string
createDb = flag.Bool("create_db", false, "create_db: will create the database")
migrate = flag.Bool("migrate", false, "migrate: will migrate the current schema into db")
loginFlag = flag.Bool("login", false, "login: will get you an OAuth2 token for further usage")
)
// init logging
func initLogger(logger *logrus.Logger) {
logger.SetLevel(logrus.InfoLevel)
logger.SetFormatter(&nested.Formatter{
FieldsOrder: []string{"component", "category"},
HideKeys: true,
})
log = logger.WithField("component", "SpotifyPlaybackSaver")
log.Info("Setup SpotifyPlaybackSaver...")
}
// load env variables
func initEnvVariables() (string, string, error) {
cID, err := envy.MustGet(EnvClientID)
if err != nil || cID == "" {
return cID, "", fmt.Errorf("env key: %s not set", EnvClientID)
}
cSec, err := envy.MustGet(EnvClientSecret)
if err != nil || cSec == "" {
return cID, cSec, fmt.Errorf("env key: %s not set", EnvClientSecret)
}
env = envy.Get(GoEnv, "development")
return cID, cSec, nil
}
func createDB(c *pop.Connection) error {
err := pop.CreateDB(c)
if err != nil && strings.Contains(err.Error(), "database exists") {
return fmt.Errorf("could not connect to database: %v", err)
}
return nil
}
func migrateDB(c *pop.Connection) error {
box, err := pop.NewMigrationBox(packr.New("migrations", "./migrations"), c)
if err != nil {
return fmt.Errorf("could not load migrations: %s", err)
}
err = box.Up()
if err != nil {
return fmt.Errorf("could not migrate: %s", err)
}
return nil
}
func loginAccount(auth login.Auth) error {
log.Info("Start login to your account...")
token := auth.Login()
if !token.Valid() {
return fmt.Errorf("could not get valid token: %v", token)
}
err := auth.SaveToken(login.TokenFileName, token)
if err != nil {
return fmt.Errorf("could not save token to file: %v", err)
}
return nil
}
func startSubCommands(db *pop.Connection, auth login.Auth) (bool, error) {
flag.Parse()
if *createDb {
return false, createDB(db)
}
if *migrate {
return false, migrateDB(db)
}
if *loginFlag {
return false, loginAccount(auth)
}
return true, nil
}
func startApp(s spotifySaver.InterfaceSpotifySaver) error {
log.Info("Start listening to your spotify history...")
var wg sync.WaitGroup
err := s.LoadToken(spotifySaver.TokenFileName)
if err != nil {
return fmt.Errorf("could not load token: %v", err)
}
s.Authenticate(CallbackURI, clientID, clientSecret)
stop := make(chan bool, 1)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
stop <- true
}
}()
wg.Add(1)
go s.StartLastSongsWorker(&wg, stop)
wg.Wait()
log.Info("Shutting down...")
return nil
}
func init() {
initLogger(logrus.New())
}
func main() {
var err error
clientID, clientSecret, err = initEnvVariables()
if err != nil {
log.Fatal(err)
}
ready, err := startSubCommands(models.DB, login.NewLogin(CallbackURI, clientID, clientSecret))
if err != nil {
log.Error(err)
}
if !ready {
return
}
s, err := spotifySaver.NewSpotifySaver(log, env)
if err != nil {
log.Fatal(err)
}
err = startApp(s)
if err != nil {
log.Fatal(err)
}
}