/* 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: `A longer description that spans multiple lines and likely contains examples and usage of using your application. For example: Cobra is a CLI library for Go that empowers applications. This application is a tool to generate the needed files to quickly create a Cobra application.`, 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 (default is 8080)") rootCmd.PersistentFlags().StringP("listen", "l", "127.0.0.1", "address to listen on (default is 127.0.0.1)") rootCmd.PersistentFlags().StringP("base-path", "b", "", "the base path, used when behind reverse proxy (default is \"\")") rootCmd.PersistentFlags().StringP("ytdlp-path", "y", "yt-dlp", "the path to the yt-dlp binary, used when it is not in $PATH (default is yt-dlp)") // 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()) } }