refactor: update some stuff

This commit is contained in:
Mathis Maquenne
2024-10-15 02:27:37 +02:00
parent 2d136a9486
commit 6b8b17aacb
4 changed files with 18 additions and 22 deletions
+2 -2
View File
@@ -21,7 +21,7 @@ var downloadCmd = &cobra.Command{
}
func init() {
rootCmd.AddCommand(downloadCmd)
RootCmd.AddCommand(downloadCmd)
downloadCmd.PersistentFlags().StringVarP(&outputDir, "output", "o", "", "output directory (default is current directory)")
downloadCmd.PersistentFlags().StringVarP(&quality, "quality", "q", "", "download quality [mp3_128, mp3_320, flac, best] (default is best)")
}
@@ -38,7 +38,7 @@ func validateInput() {
"best": true,
}
if !validQualities[quality] {
fmt.Fprintf(os.Stderr, "invalid quality option: %s\n", quality)
fmt.Fprintf(os.Stderr, "Error: invalid quality option: %s\n", quality)
os.Exit(1)
}
}
+14 -18
View File
@@ -12,20 +12,16 @@ import (
var cfgFile string
var rootCmd = &cobra.Command{
var RootCmd = &cobra.Command{
Use: "godeez",
Short: "GoDeez is a tool to download music from Deezer",
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "could not execute command: %v\n", err)
os.Exit(1)
}
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
func init() {
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.godeez)")
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.godeez)")
cobra.OnInitialize(initConfig)
}
@@ -35,17 +31,17 @@ func initConfig() {
} else {
homedir, err := os.UserHomeDir()
if err != nil {
fmt.Fprintf(os.Stderr, "could not get home directory: %v\n", err)
fmt.Fprintf(os.Stderr, "Error: could not get home directory: %v\n", err)
os.Exit(1)
}
path := path.Join(homedir, ".godeez")
if _, err := os.Stat(path); os.IsNotExist(err) {
fmt.Printf("config file not found, creating one at %s\n", path)
fmt.Printf("Config file not found, creating one at %s\n", path)
content := []byte("license_token: \nsecret_key: \niv: \n")
content := []byte("arl_cookie = ''\nlicense_token = ''\nsecret_key = ''\niv = '0001020304050607'\n")
if err := os.WriteFile(path, content, 0644); err != nil {
fmt.Fprintf(os.Stderr, "could not create config file: %v\n", err)
fmt.Fprintf(os.Stderr, "Error: could not create config file: %v\n", err)
os.Exit(1)
}
}
@@ -57,26 +53,26 @@ func initConfig() {
viper.SetConfigType("toml")
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
fmt.Fprintf(os.Stderr, "could not read config file: %v\n", err)
fmt.Fprintf(os.Stderr, "Error: could not read config file: %v\n", err)
os.Exit(1)
}
cfg := &config.Cfg
if err := viper.Unmarshal(&cfg); err != nil {
fmt.Fprintf(os.Stderr, "could not unmarshal config: %v\n", err)
fmt.Fprintf(os.Stderr, "Error: could not unmarshal config file: %v\n", err)
os.Exit(1)
}
if cfg.LicenseToken == "" {
fmt.Fprintln(os.Stderr, "license_token is not set in config file")
fmt.Fprintln(os.Stderr, "Error: license_token is not set in config file")
os.Exit(1)
}
if cfg.SecretKey == "" {
fmt.Fprintln(os.Stderr, "secret_key is not set in config file")
fmt.Fprintln(os.Stderr, "Error: secret_key is not set in config file")
os.Exit(1)
}
if cfg.IV == "" {
fmt.Fprintln(os.Stderr, "iv is not set in config file")
fmt.Fprintln(os.Stderr, "Error: iv is not set in config file")
os.Exit(1)
}
}