/* Copyright © 2023 Evan Fiordeliso */ package cmd import ( "fmt" "os" "github.com/adrg/xdg" "github.com/spf13/cobra" "github.com/spf13/viper" "go.fifitido.net/ytdl-web/web" "golang.org/x/exp/slog" ) var ( cfgFile string rootCmd = &cobra.Command{ Use: "ytdl-web", Short: "A web frontend for yt-dlp", Long: `YTDL Web 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) { if err := web.Serve(); err != nil { slog.Error("Error when serving website", slog.String("error", err.Error())) } }, } ) 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)") rootCmd.PersistentFlags().IntP("port", "p", 8080, "port to listen on") rootCmd.PersistentFlags().StringP("listen", "l", "127.0.0.1", "address to listen on") rootCmd.PersistentFlags().StringP("base-path", "b", "", "the base path, used when behind reverse proxy") rootCmd.PersistentFlags().StringP("ytdlp-path", "y", "yt-dlp", "the path to the yt-dlp binary, used when it is not in $PATH") // trunk-ignore-begin(golangci-lint/errcheck): Ignoring errors viper.BindPFlag("port", rootCmd.PersistentFlags().Lookup("port")) viper.BindPFlag("listen", rootCmd.PersistentFlags().Lookup("listen")) viper.BindPFlag("base_path", rootCmd.PersistentFlags().Lookup("base-path")) viper.BindPFlag("ytdlp_path", rootCmd.PersistentFlags().Lookup("ytdlp-path")) // trunk-ignore-end(golangci-lint/errcheck) viper.SetDefault("port", 8080) viper.SetDefault("listen", "127.0.0.1") viper.SetDefault("base_path", "") viper.SetDefault("ytdlp_path", "yt-dlp") } func initConfig() { slog.Info("Initializing configuration") if cfgFile != "" { viper.SetConfigFile(cfgFile) } else { viper.AddConfigPath(xdg.ConfigHome) viper.SetConfigType("yml") viper.SetConfigName("config") } viper.SetEnvPrefix("ytdl") viper.AutomaticEnv() if err := viper.ReadInConfig(); err == nil { fmt.Println("Using config file:", viper.ConfigFileUsed()) } }