2023-04-14 11:58:32 -04:00
|
|
|
/*
|
2023-04-14 17:14:27 -04:00
|
|
|
Copyright © 2023 Evan Fiordeliso <evan.fiordeliso@gmail.com>
|
2023-04-14 11:58:32 -04:00
|
|
|
*/
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-04-14 17:14:27 -04:00
|
|
|
"fmt"
|
2023-04-14 11:58:32 -04:00
|
|
|
"os"
|
2023-04-14 17:14:27 -04:00
|
|
|
"strings"
|
2023-04-14 11:58:32 -04:00
|
|
|
|
2023-04-14 17:14:27 -04:00
|
|
|
"github.com/adrg/xdg"
|
2023-04-14 11:58:32 -04:00
|
|
|
"github.com/spf13/cobra"
|
2023-04-14 17:14:27 -04:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"go.fifitido.net/ytdl-web/web"
|
|
|
|
"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-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",
|
|
|
|
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.`,
|
2023-04-14 17:14:27 -04:00
|
|
|
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() {
|
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-04-14 17:14:27 -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-14 11:58:32 -04:00
|
|
|
|
2023-04-14 17:14:27 -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"))
|
|
|
|
// trunk-ignore-end(golangci-lint/errcheck)
|
|
|
|
|
|
|
|
viper.SetDefault("port", 8080)
|
|
|
|
viper.SetDefault("listen", "127.0.0.1")
|
|
|
|
viper.SetDefault("base_path", "")
|
2023-04-14 11:58:32 -04:00
|
|
|
}
|
|
|
|
|
2023-04-14 17:14:27 -04:00
|
|
|
func initConfig() {
|
|
|
|
if cfgFile != "" {
|
|
|
|
viper.SetConfigFile(cfgFile)
|
|
|
|
} else {
|
|
|
|
viper.AddConfigPath(xdg.ConfigHome)
|
|
|
|
viper.SetConfigType("yml")
|
|
|
|
viper.SetConfigName("config")
|
|
|
|
}
|
|
|
|
|
|
|
|
viper.SetEnvPrefix("ytdl")
|
|
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer("_", ""))
|
|
|
|
viper.AutomaticEnv()
|
2023-04-14 11:58:32 -04:00
|
|
|
|
2023-04-14 17:14:27 -04:00
|
|
|
if err := viper.ReadInConfig(); err == nil {
|
|
|
|
fmt.Println("Using config file:", viper.ConfigFileUsed())
|
|
|
|
}
|
|
|
|
}
|