ytdl-web/cmd/root.go

99 lines
2.7 KiB
Go
Raw Permalink Normal View History

2023-04-14 11:58:32 -04:00
/*
Copyright © 2023 Evan Fiordeliso <evan.fiordeliso@gmail.com>
2023-04-14 11:58:32 -04:00
*/
package cmd
import (
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
2023-05-23 18:44:05 -04:00
"go.fifitido.net/ytdl-web/config"
"go.fifitido.net/ytdl-web/web"
"golang.org/x/exp/slog"
2023-04-14 11:58:32 -04:00
)
var (
cfgFile string
2023-05-23 18:44:05 -04:00
cfg *config.Config
2023-04-14 11:58:32 -04:00
rootCmd = &cobra.Command{
Use: "ytdl-web",
Short: "A web frontend for yt-dlp",
Long: `YTDL Web
2023-04-14 11:58:32 -04:00
A web application that grabs the links to videos from over a
thousand websites using the yt-dlp project under the hood.`,
Run: func(cmd *cobra.Command, args []string) {
2023-05-23 18:44:05 -04:00
if err := web.Serve(cfg); err != nil {
slog.Error("Error when serving website", slog.String("error", err.Error()))
}
},
}
)
2023-04-14 11:58:32 -04:00
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
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-05-23 21:52:00 -04:00
rootCmd.PersistentFlags().IntP("port", "p", 0, "port to listen on")
rootCmd.PersistentFlags().StringP("listen", "l", "", "address to listen on")
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
// 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"))
// trunk-ignore-end(golangci-lint/errcheck)
2023-04-14 11:58:32 -04:00
}
func initConfig() {
2023-05-23 18:44:05 -04:00
var err error
if cfgFile != "" {
2023-05-23 18:44:05 -04:00
cfg, err = config.LoadConfig(cfgFile)
} else {
2023-05-23 18:44:05 -04:00
cfg, err = config.LoadConfig()
}
if err != nil {
slog.Error("Error loading configuration", slog.String("error", err.Error()))
os.Exit(1)
}
2023-05-23 18:44:05 -04:00
initLogging()
2023-04-14 11:58:32 -04:00
2023-05-23 18:44:05 -04:00
slog.Info("Configuration loaded")
}
func initLogging() {
var handler slog.Handler
if cfg.IsProduction() {
handler = slog.HandlerOptions{
AddSource: true,
Level: slog.LevelWarn,
}.NewJSONHandler(os.Stdout)
} else {
handler = slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
}.NewTextHandler(os.Stdout)
}
2023-05-23 18:44:05 -04:00
slog.SetDefault(slog.New(handler))
}