2023-04-14 11:58:32 -04:00
|
|
|
/*
|
2024-05-17 20:56:38 -04:00
|
|
|
Copyright © 2024 Evan Fiordeliso <evan.fiordeliso@gmail.com>
|
2023-04-14 11:58:32 -04:00
|
|
|
*/
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-04-16 00:51:25 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-04-14 11:58:32 -04:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2023-04-14 17:14:27 -04:00
|
|
|
"github.com/spf13/viper"
|
2023-05-23 18:44:05 -04:00
|
|
|
"go.fifitido.net/ytdl-web/config"
|
2023-04-14 17:14:27 -04:00
|
|
|
"golang.org/x/exp/slog"
|
2023-04-14 11:58:32 -04:00
|
|
|
)
|
|
|
|
|
2023-04-14 17:14:27 -04:00
|
|
|
var (
|
|
|
|
cfgFile string
|
2023-05-23 18:44:05 -04:00
|
|
|
cfg *config.Config
|
2023-04-14 11:58:32 -04:00
|
|
|
|
2023-04-14 17:14:27 -04:00
|
|
|
rootCmd = &cobra.Command{
|
|
|
|
Use: "ytdl-web",
|
|
|
|
Short: "A web frontend for yt-dlp",
|
2024-04-16 00:51:25 -04:00
|
|
|
Long: `YTDL Web is a web application that grabs the links to videos
|
|
|
|
from over a thousand websites using the yt-dlp project under the hood.`,
|
2023-04-14 17:14:27 -04:00
|
|
|
}
|
|
|
|
)
|
2023-04-14 11:58:32 -04:00
|
|
|
|
2024-04-16 00:51:25 -04:00
|
|
|
func Execute() error {
|
|
|
|
return rootCmd.Execute()
|
2023-04-14 11:58:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2023-04-14 17:14:27 -04:00
|
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
|
|
|
|
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $XDG_CONFIG_HOME/ytdl-web/config.yml)")
|
2023-04-14 11:58:32 -04:00
|
|
|
|
2023-08-14 18:14:08 -04:00
|
|
|
rootCmd.PersistentFlags().IntP("port", "p", 8080, "port to listen on")
|
2023-05-23 21:52:00 -04:00
|
|
|
rootCmd.PersistentFlags().StringP("listen", "l", "", "address to listen on")
|
2023-04-15 18:07:48 -04:00
|
|
|
rootCmd.PersistentFlags().StringP("base-path", "b", "", "the base path, used when behind reverse proxy")
|
2023-05-23 21:52:00 -04:00
|
|
|
rootCmd.PersistentFlags().StringP("ytdlp-path", "y", "", "the path to the yt-dlp binary, used when it is not in $PATH")
|
2023-05-23 18:44:05 -04:00
|
|
|
rootCmd.PersistentFlags().BoolP("cookies-enabled", "C", false, "whether cookies are enabled")
|
|
|
|
rootCmd.PersistentFlags().StringP("cookies", "c", "", "the path to the cookies file")
|
2023-04-14 11:58:32 -04:00
|
|
|
|
2023-04-14 17:14:27 -04:00
|
|
|
// trunk-ignore-begin(golangci-lint/errcheck): Ignoring errors
|
2023-05-23 18:44:05 -04:00
|
|
|
viper.BindPFlag("http.port", rootCmd.PersistentFlags().Lookup("port"))
|
|
|
|
viper.BindPFlag("http.listen", rootCmd.PersistentFlags().Lookup("listen"))
|
|
|
|
viper.BindPFlag("http.basePath", rootCmd.PersistentFlags().Lookup("base-path"))
|
|
|
|
viper.BindPFlag("ytdlp.binaryPath", rootCmd.PersistentFlags().Lookup("ytdlp-path"))
|
|
|
|
viper.BindPFlag("cookies.enabled", rootCmd.PersistentFlags().Lookup("cookies-enabled"))
|
|
|
|
viper.BindPFlag("cookies.filePath", rootCmd.PersistentFlags().Lookup("cookies"))
|
2023-04-14 17:14:27 -04:00
|
|
|
// trunk-ignore-end(golangci-lint/errcheck)
|
2023-04-14 11:58:32 -04:00
|
|
|
}
|
|
|
|
|
2023-04-14 17:14:27 -04:00
|
|
|
func initConfig() {
|
2023-05-23 18:44:05 -04:00
|
|
|
var err error
|
2023-04-14 17:14:27 -04:00
|
|
|
if cfgFile != "" {
|
2023-05-23 18:44:05 -04:00
|
|
|
cfg, err = config.LoadConfig(cfgFile)
|
2023-04-14 17:14:27 -04:00
|
|
|
} else {
|
2023-05-23 18:44:05 -04:00
|
|
|
cfg, err = config.LoadConfig()
|
|
|
|
}
|
|
|
|
|
2024-04-16 00:51:25 -04:00
|
|
|
notFound := &viper.ConfigFileNotFoundError{}
|
|
|
|
switch {
|
|
|
|
case err != nil && !errors.As(err, notFound):
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
case err != nil && errors.As(err, notFound):
|
|
|
|
// The config file is optional, we shouldn't exit when the config is not found
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
2023-04-14 17:14:27 -04:00
|
|
|
}
|
2023-05-23 18:44:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func initLogging() {
|
|
|
|
var handler slog.Handler
|
|
|
|
|
|
|
|
if cfg.IsProduction() {
|
|
|
|
handler = slog.HandlerOptions{
|
|
|
|
AddSource: true,
|
2023-08-12 23:44:21 -04:00
|
|
|
Level: slog.LevelInfo,
|
2023-05-23 18:44:05 -04:00
|
|
|
}.NewJSONHandler(os.Stdout)
|
|
|
|
} else {
|
|
|
|
handler = slog.HandlerOptions{
|
|
|
|
AddSource: true,
|
|
|
|
Level: slog.LevelDebug,
|
|
|
|
}.NewTextHandler(os.Stdout)
|
2023-04-14 17:14:27 -04:00
|
|
|
}
|
2023-05-23 18:44:05 -04:00
|
|
|
|
|
|
|
slog.SetDefault(slog.New(handler))
|
2023-04-14 17:14:27 -04:00
|
|
|
}
|