publish-vod/main.go

135 lines
3 KiB
Go
Raw Normal View History

2024-02-24 00:22:19 +00:00
package main
import (
"context"
"fmt"
"io"
2024-02-24 00:22:19 +00:00
"os"
"sync"
2024-02-24 00:22:19 +00:00
"git.luzifer.io/luzifer/publish-vod/pkg/config"
"git.luzifer.io/luzifer/publish-vod/pkg/uploader"
2024-02-24 00:22:19 +00:00
"github.com/cheggaaa/pb/v3"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/Luzifer/rconfig/v2"
)
var (
cfg = struct {
Config string `flag:"config,c" default:"config.yaml" description:"Configuration for the uploaders"`
2024-02-24 00:22:19 +00:00
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
}{}
version = "dev"
)
func initApp() error {
rconfig.AutoEnv(true)
if err := rconfig.ParseAndValidate(&cfg); err != nil {
return errors.Wrap(err, "parsing cli options")
}
l, err := logrus.ParseLevel(cfg.LogLevel)
if err != nil {
return errors.Wrap(err, "parsing log-level")
}
logrus.SetLevel(l)
return nil
}
func main() {
var err error
if err = initApp(); err != nil {
logrus.WithError(err).Fatal("initializing app")
}
if cfg.VersionAndExit {
logrus.WithField("version", version).Info("publish-vod")
2024-02-24 00:22:19 +00:00
os.Exit(0)
}
if len(rconfig.Args()) < 2 { //nolint:gomnd
logrus.Fatal("Usage: publish-vod <filename>")
}
configFile, err := config.Load(cfg.Config)
if err != nil {
logrus.WithError(err).Fatal("loading config")
2024-02-24 00:22:19 +00:00
}
var (
ctx = context.Background()
fileName = rconfig.Args()[1]
wg sync.WaitGroup
2024-02-24 00:22:19 +00:00
)
f, err := os.Open(fileName)
2024-02-24 00:22:19 +00:00
if err != nil {
logrus.WithError(err).Fatal("opening VoD")
2024-02-24 00:22:19 +00:00
}
defer f.Close() //nolint:errcheck // File is closed by process exit
2024-02-24 00:22:19 +00:00
stat, err := f.Stat()
2024-02-24 00:22:19 +00:00
if err != nil {
logrus.WithError(err).Fatal("getting VoD stat")
2024-02-24 00:22:19 +00:00
}
var (
barPool = pb.NewPool()
longestPrefix int
)
2024-02-24 00:22:19 +00:00
for _, c := range configFile.Uploaders {
u := uploaderByName(c.Type)
if u == nil {
logrus.Fatalf("unknown uploader %q", c.Type)
}
if err = u.ValidateConfig(c.Settings); err != nil {
logrus.WithError(err).Fatalf("validating config entry %q", c.Name)
}
2024-02-24 00:22:19 +00:00
if l := len(c.Name); l > longestPrefix {
longestPrefix = l
}
2024-02-24 00:22:19 +00:00
}
for i := range configFile.Uploaders {
wg.Add(1)
2024-02-24 00:22:19 +00:00
bar := pb.New64(stat.Size())
bar.SetTemplate(pb.Full)
barPool.Add(bar)
2024-02-24 00:22:19 +00:00
go func(c config.UploaderConfig, bar *pb.ProgressBar) {
defer wg.Done()
2024-02-24 00:22:19 +00:00
bar.Set("prefix", fmt.Sprintf(fmt.Sprintf("%%-%ds", longestPrefix), c.Name))
2024-02-24 00:22:19 +00:00
if err = uploaderByName(c.Type).UploadFile(ctx, uploader.UploaderOpts{
Name: c.Name,
Config: c.Settings,
2024-02-24 00:22:19 +00:00
Filename: fileName,
Content: io.NewSectionReader(f, 0, stat.Size()),
Size: stat.Size(),
2024-02-24 00:22:19 +00:00
ProgressBar: bar,
FinalMessage: func(format string, opts ...any) {
bar.SetTemplate(pb.ProgressBarTemplate(fmt.Sprintf(`{{ string . "prefix" }} %s`, fmt.Sprintf(format, opts...))))
},
}); err != nil {
bar.SetTemplate(pb.ProgressBarTemplate(fmt.Sprintf(`{{ string . "prefix" }} ERR: %s`, err)))
}
}(configFile.Uploaders[i], bar)
2024-02-24 00:22:19 +00:00
}
barPool.Start()
wg.Wait()
barPool.Stop()
2024-02-24 00:22:19 +00:00
}