-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
204 lines (182 loc) · 5.85 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
//nolint:gci
import (
"io"
"os"
"time"
"github.com/jmillerv/go-dj/helpers"
"github.com/jmillerv/go-dj/cache"
"github.com/jmillerv/go-dj/content"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"zgo.at/zcache"
)
const (
configFile = "config.yml"
configOverride = "GODJ_CONFIG_OVERRIDE"
logFile = "/tmp/godj.log"
logPermissions = 0o666
)
func main() { //nolint:funlen,cyclop,gocognit // main function can be longer & more complex.
app := &cli.App{ //nolint:exhaustivestruct,exhaustruct
Name: "Go DJ",
Usage: "Daemon that schedules audio programming content",
Version: "0.0.1",
Commands: cli.Commands{
{ //nolint:exhaustruct
Name: "start",
Aliases: []string{"s"},
Usage: "start",
UsageText: "starts the daemon from the config",
Action: func(c *cli.Context) { //nolint:revive
var config string
log.Info("creating schedule from config")
if os.Getenv(configOverride) != "" {
config = os.Getenv(configOverride)
} else {
config = configFile
}
scheduler, err := content.NewScheduler(config)
if err != nil {
log.WithError(err).Error("content.NewScheduler::unable to run go-dj")
}
ttl, err := time.ParseDuration(scheduler.Content.PlayedPodcastTTL)
if err != nil {
log.WithError(err).Error("unable to parse played podcast ttl")
}
// create cache
cache.PodcastPlayedCache = zcache.New(ttl, ttl)
// hydrate podcast
content.HydratePodcastCache()
if content.Shuffled {
log.Info("playing shuffled content")
err = scheduler.Shuffle()
if err != nil {
log.WithError(err).Error("scheduler.Shuffle::unable to run go-dj")
}
return
}
// run content normally
err = scheduler.Run()
if err != nil {
log.WithError(err).Error("scheduler.Run::unable to run go-dj")
}
},
Flags: []cli.Flag{
cli.BoolFlag{ //nolint:exhaustivestruct,exhaustruct
Name: "random",
Usage: "Start your radio station w/ randomized schedule",
Required: false,
Hidden: false,
Destination: &content.Shuffled,
},
cli.BoolFlag{ //nolint:exhaustivestruct,exhaustruct
Name: "pod-oldest",
Usage: "podcasts will play starting with the oldest first",
Required: false,
Hidden: false,
Destination: &content.PodcastPlayerOrderOldest,
},
cli.BoolFlag{ //nolint:exhaustivestruct,exhaustruct
Name: "pod-random",
Usage: "podcasts will play in a random order",
Required: false,
Hidden: false,
Destination: &content.PodcastPlayOrderRandom,
},
},
},
{ //nolint:exhaustruct
Name: "clear-cache",
Aliases: []string{"clear"},
Usage: "./go-dj clear-cache",
UsageText: "deletes the in memory and locally saved podcast cache",
Action: func(c *cli.Context) { //nolint:revive
log.Info("clearing cache")
// set the config
var config string
if os.Getenv(configOverride) != "" {
config = os.Getenv(configOverride)
} else {
config = configFile
}
scheduler, err := content.NewScheduler(config)
if err != nil {
log.WithError(err).Error("content.NewScheduler::unable to create scheduler from config file")
}
ttl, err := time.ParseDuration(scheduler.Content.PlayedPodcastTTL)
if err != nil {
log.WithError(err).Error("unable to parse played podcast ttl")
}
// create cache
cache.PodcastPlayedCache = zcache.New(ttl, ttl)
err = cache.ClearPodcastPlayedCache()
if err != nil {
log.WithError(err).Error("unable to clear podcast played cache")
}
},
},
{ //nolint:exhaustruct
Name: "install-dependencies",
Aliases: []string{"deps"},
Usage: "./go-dj install-dependencies",
UsageText: "installs necessary dependencies to run go-dj",
Action: func(c *cli.Context) { //nolint:revive
packages := []string{"libasound2-dev", "libudev-dev", "pkg-config"}
missingPackages := []string{}
for _, pkg := range packages {
if !helpers.PackageIsInstalled(pkg) {
missingPackages = append(missingPackages, pkg)
}
}
if len(missingPackages) > 0 {
log.Info("The following packages are missing:")
for _, pkg := range missingPackages {
log.Info("-", pkg)
}
log.Info("Installing missing packages...")
for _, pkg := range missingPackages {
err := helpers.InstallPackage(pkg)
if err != nil {
log.WithError(err).Error("Error installing package:")
return
}
log.Infof("Package %s installed successfully.\n", pkg)
}
} else {
log.Info("All required packages are already installed.")
}
},
},
},
Author: "Jeremiah Miller",
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
// init sets global variables.
func init() {
content.Shuffled = false
content.PodcastPlayerOrderOldest = false
content.PodcastPlayOrderRandom = false
initLogger()
}
// initLogger creates the multiwriter, determines the log format for each destination, and sets the logfile location.
// at a later stage, it may be desirable to have different formats for standard out vs the log file.
// An example of how to do that can be found here https://github.com/sirupsen/logrus/issues/784#issuecomment-403765306
func initLogger() {
// create a new file for logs
logs, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, logPermissions)
if err != nil {
log.WithError(err).Error("unable to open log file")
}
// open the multiwriter
multiWrite := io.MultiWriter(os.Stdout, logs)
log.SetFormatter(&log.TextFormatter{ //nolint:exhaustruct // don't need this full enumerated
ForceColors: true,
FullTimestamp: true,
TimestampFormat: time.RFC822,
})
log.SetOutput(multiWrite)
}