-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (148 loc) · 4.7 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
package main
import (
"io"
"os"
"time"
"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 = 0666 //nolint:gofumpt // gofumpt does weird things to this
)
func main() { //nolint:funlen,cyclop // 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{
{
Name: "start",
Aliases: []string{"s"},
Usage: "start",
UsageText: "starts the daemon from the config",
Action: func(c *cli.Context) {
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,
},
},
},
{
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) {
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")
}
},
},
},
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)
}