-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
109 lines (92 loc) · 2.32 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
package main
import (
"context"
"fmt"
syslog "log"
"net/http"
"os"
"os/signal"
"time"
"github.com/F0rzend/radiot_dumper/copier"
"github.com/ilyakaznacheev/cleanenv"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
const (
configFileName = "dumper.yml"
)
type Config struct {
SourceURL string `yaml:"source_url" env:"SOURCE_URL"`
FilePrefix string `yaml:"file_prefix" env:"FILE_PREFIX"`
Schedule string `yaml:"schedule" env:"SCHEDULE"`
Duration string `yaml:"duration" env:"DURATION"`
OutputDirectory string `yaml:"output_directory" env:"OUTPUT_DIRECTORY"`
FileDateFormat string `yaml:"file_date_format" env:"FILE_DATE_FORMAT" env-default:"02_01_2006"`
Delay string `yaml:"delay" env:"DELAY" env-default:"5s"`
LogLevel string `yaml:"log_level" env:"LOG_LEVEL" env-default:"info"`
}
func Run() error {
cfg := Config{}
_, err := os.Stat(configFileName)
if os.IsNotExist(err) {
fmt.Printf("Config file %s not found, using environment variables\n", configFileName)
err = cleanenv.ReadEnv(&cfg)
} else {
fmt.Printf("Using config file %s\n", configFileName)
err = cleanenv.ReadConfig(configFileName, &cfg)
}
if err != nil {
return err
}
delay, err := time.ParseDuration(cfg.Delay)
if err != nil {
return err
}
duration, err := time.ParseDuration(cfg.Duration)
if err != nil {
return err
}
datedFileBuilder := copier.NewDatedFileBuilder(
cfg.OutputDirectory,
os.DirFS(cfg.OutputDirectory),
cfg.FilePrefix,
cfg.FileDateFormat,
)
logger := log.
Output(zerolog.ConsoleWriter{Out: os.Stderr}).
Level(Must(zerolog.ParseLevel(cfg.LogLevel))).
With().
Caller().
Logger()
streamCopier := copier.NewStreamCopier(
&http.Client{
Timeout: 0,
},
)
runner := copier.NewRunner(streamCopier)
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
ctx = logger.WithContext(ctx)
if err = runner.ScheduleRecording(
ctx,
cfg.Schedule,
duration,
cfg.SourceURL,
datedFileBuilder.GetOutput,
delay,
); err != nil {
logger.Error().Err(err).Msg("Error scheduling recording")
}
<-ctx.Done()
return nil
}
func main() {
if err := Run(); err != nil {
syslog.Fatal(err)
}
}
func Must[T any](value T, err error) T {
if err != nil {
panic(err)
}
return value
}