Add prepare function to authorize before progress bars

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2024-03-27 18:26:13 +01:00
parent 335802a3d7
commit c331f549fa
Signed by: luzifer
SSH key fingerprint: SHA256:/xtE5lCgiRDQr8SLxHMS92ZBlACmATUmF1crK16Ks4E
5 changed files with 59 additions and 25 deletions

45
main.go
View file

@ -20,6 +20,7 @@ var (
cfg = struct {
Config string `flag:"config,c" default:"config.yaml" description:"Configuration for the uploaders"`
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
ValidateOnly bool `flag:"validate-only" default:"false" description:"Only execute validation and prepare functions, do not upload"`
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
}{}
@ -52,10 +53,6 @@ func main() {
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")
@ -63,21 +60,9 @@ func main() {
var (
ctx = context.Background()
fileName = rconfig.Args()[1]
wg sync.WaitGroup
)
f, err := os.Open(fileName)
if err != nil {
logrus.WithError(err).Fatal("opening VoD")
}
defer f.Close() //nolint:errcheck // File is closed by process exit
stat, err := f.Stat()
if err != nil {
logrus.WithError(err).Fatal("getting VoD stat")
}
var (
barPool = pb.NewPool()
longestPrefix int
@ -88,15 +73,43 @@ func main() {
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)
}
if err = u.Prepare(ctx, uploader.UploaderOpts{
Name: c.Name,
Config: c.Settings,
}); err != nil {
logrus.WithError(err).Fatalf("preparing uploader %q", c.Name)
}
if l := len(c.Name); l > longestPrefix {
longestPrefix = l
}
}
if cfg.ValidateOnly {
return
}
if len(rconfig.Args()) < 2 { //nolint:gomnd
logrus.Fatal("Usage: publish-vod <filename>")
}
fileName := rconfig.Args()[1]
f, err := os.Open(fileName)
if err != nil {
logrus.WithError(err).Fatal("opening VoD")
}
defer f.Close() //nolint:errcheck // File is closed by process exit
stat, err := f.Stat()
if err != nil {
logrus.WithError(err).Fatal("getting VoD stat")
}
for i := range configFile.Uploaders {
wg.Add(1)

View file

@ -12,6 +12,7 @@ import (
type (
Uploader interface {
Prepare(ctx context.Context, opts UploaderOpts) error
UploadFile(ctx context.Context, opts UploaderOpts) error
ValidateConfig(config *fieldcollection.FieldCollection) error
}

View file

@ -25,6 +25,8 @@ var (
_ uploader.Uploader = Uploader{}
)
func (Uploader) Prepare(context.Context, uploader.UploaderOpts) error { return nil }
func (Uploader) UploadFile(ctx context.Context, opts uploader.UploaderOpts) error {
socket := os.Getenv("SSH_AUTH_SOCK")
conn, err := net.Dial("unix", socket)

View file

@ -27,26 +27,44 @@ var (
_ uploader.Uploader = Uploader{}
)
func (Uploader) Prepare(ctx context.Context, opts uploader.UploaderOpts) error {
_, ts, err := oauth4youtube.GetOAuth2Client(ctx, opts.Config.MustString("vaultKey", nil))
if err != nil {
return fmt.Errorf("getting oauth credentials: %w", err)
}
t, err := ts.Token()
if err != nil {
return fmt.Errorf("getting token to store: %w", err)
}
if err := vaultoauth2.SaveTokenToVault(opts.Config.MustString("vaultKey", nil), t); err != nil {
return fmt.Errorf("storing token to vault: %w", err)
}
return nil
}
func (Uploader) UploadFile(ctx context.Context, opts uploader.UploaderOpts) error {
client, ts, err := oauth4youtube.GetOAuth2Client(ctx, opts.Config.MustString("vaultKey", nil))
if err != nil {
logrus.WithError(err).Fatal("getting oauth credentials")
return fmt.Errorf("getting oauth credentials: %w", err)
}
defer func() {
t, err := ts.Token()
if err != nil {
logrus.WithError(err).Fatal("getting token to store back")
logrus.WithError(err).Error("getting token to store back")
}
if err := vaultoauth2.SaveTokenToVault(opts.Config.MustString("vaultKey", nil), t); err != nil {
logrus.WithError(err).Fatal("storing token back to vault")
logrus.WithError(err).Error("storing token back to vault")
}
}()
service, err := youtube.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
logrus.WithError(err).Fatal("creating YouTube client")
return fmt.Errorf("creating Youtube client: %w", err)
}
upload := &youtube.Video{
@ -78,7 +96,7 @@ func (Uploader) UploadFile(ctx context.Context, opts uploader.UploaderOpts) erro
Media(progressReader).
Do()
if err != nil {
logrus.WithError(err).Fatal("inserting video")
return fmt.Errorf("inserting video: %w", err)
}
opts.FinalMessage("Video uploaded: https://youtu.be/%s", resp.Id)

View file

@ -28,7 +28,7 @@ func GetOAuth2Client(ctx context.Context, vaultKey string) (client *http.Client,
ClientID: clientID,
ClientSecret: clientSecret,
Endpoint: google.Endpoint,
RedirectURL: "http://127.0.0.1:8000",
RedirectURL: "http://127.0.0.1:65285",
Scopes: []string{youtube.YoutubeUploadScope},
}
@ -39,7 +39,7 @@ func GetOAuth2Client(ctx context.Context, vaultKey string) (client *http.Client,
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { codeChan <- r.URL.Query().Get("code") })
server := &http.Server{
Addr: ":8000",
Addr: ":65285",
Handler: mux,
ReadHeaderTimeout: time.Second,
}