ytdl-web/cmd/root.go

85 lines
2.4 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 (
"fmt"
2023-04-14 11:58:32 -04:00
"os"
"github.com/adrg/xdg"
2023-04-14 11:58:32 -04:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.fifitido.net/ytdl-web/web"
"golang.org/x/exp/slog"
2023-04-14 11:58:32 -04:00
)
var (
cfgFile string
2023-04-14 11:58:32 -04:00
rootCmd = &cobra.Command{
Use: "ytdl-web",
Short: "A web frontend for yt-dlp",
Long: `A longer description that spans multiple lines and likely contains
2023-04-14 11:58:32 -04:00
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()))
}
},
}
)
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
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 \"\")")
2023-04-15 15:45:27 -04:00
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)")
2023-04-14 11:58:32 -04:00
// 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"))
2023-04-15 15:45:27 -04:00
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", "")
2023-04-15 15:45:27 -04:00
viper.SetDefault("ytdlp_path", "yt-dlp")
2023-04-14 11:58:32 -04:00
}
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()
2023-04-14 11:58:32 -04:00
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}