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
+1 -1
View File
@@ -1,4 +1,4 @@
license_token = 'AAAAA1bcDFgHJKLmnOPqrsTUvwXYZ12o3lmNOjR2xWiQRT3v5zxL4mKoWjxL82jD7sWx93KfYGsN2IkdPxsUw9PTas7oFbgR3nY9qpZmVyHFJKLPxT7sWmqErXyTn5iKoNpRtV7bPdQZ8qLmWoJpU2nKrXz6vW'
arl_cookie = '1d9e90abb452a61b1b7463f0953e1b303c4e2e7e5fd404fde9b385f4de01c340ac0f62f1c8c1550405b0b9beded0e28c481e96a6148e8f4548351add5d7db746b2785ecf83b1768e5dd8cc73b1ad30c18d07c9cb37f5c6b9cd7a78a4de2aff11' arl_cookie = '1d9e90abb452a61b1b7463f0953e1b303c4e2e7e5fd404fde9b385f4de01c340ac0f62f1c8c1550405b0b9beded0e28c481e96a6148e8f4548351add5d7db746b2785ecf83b1768e5dd8cc73b1ad30c18d07c9cb37f5c6b9cd7a78a4de2aff11'
license_token = 'AAAAA1bcDFgHJKLmnOPqrsTUvwXYZ12o3lmNOjR2xWiQRT3v5zxL4mKoWjxL82jD7sWx93KfYGsN2IkdPxsUw9PTas7oFbgR3nY9qpZmVyHFJKLPxT7sWmqErXyTn5iKoNpRtV7bPdQZ8qLmWoJpU2nKrXz6vW'
secret_key = 'hTv1IAw19qWy9i3f' secret_key = 'hTv1IAw19qWy9i3f'
iv = '0001020304050607' iv = '0001020304050607'
+2 -2
View File
@@ -21,7 +21,7 @@ var downloadCmd = &cobra.Command{
} }
func init() { func init() {
rootCmd.AddCommand(downloadCmd) RootCmd.AddCommand(downloadCmd)
downloadCmd.PersistentFlags().StringVarP(&outputDir, "output", "o", "", "output directory (default is current directory)") 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)") 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, "best": true,
} }
if !validQualities[quality] { 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) os.Exit(1)
} }
} }
+14 -18
View File
@@ -12,20 +12,16 @@ import (
var cfgFile string var cfgFile string
var rootCmd = &cobra.Command{ var RootCmd = &cobra.Command{
Use: "godeez", Use: "godeez",
Short: "GoDeez is a tool to download music from Deezer", Short: "GoDeez is a tool to download music from Deezer",
} Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
func Execute() { },
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "could not execute command: %v\n", err)
os.Exit(1)
}
} }
func init() { 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) cobra.OnInitialize(initConfig)
} }
@@ -35,17 +31,17 @@ func initConfig() {
} else { } else {
homedir, err := os.UserHomeDir() homedir, err := os.UserHomeDir()
if err != nil { 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) os.Exit(1)
} }
path := path.Join(homedir, ".godeez") path := path.Join(homedir, ".godeez")
if _, err := os.Stat(path); os.IsNotExist(err) { 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 { 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) os.Exit(1)
} }
} }
@@ -57,26 +53,26 @@ func initConfig() {
viper.SetConfigType("toml") viper.SetConfigType("toml")
viper.AutomaticEnv() viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil { 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) os.Exit(1)
} }
cfg := &config.Cfg cfg := &config.Cfg
if err := viper.Unmarshal(&cfg); err != nil { 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) os.Exit(1)
} }
if cfg.LicenseToken == "" { 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) os.Exit(1)
} }
if cfg.SecretKey == "" { 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) os.Exit(1)
} }
if cfg.IV == "" { 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) os.Exit(1)
} }
} }
+1 -1
View File
@@ -3,5 +3,5 @@ package main
import "github.com/mathismqn/godeez/cmd" import "github.com/mathismqn/godeez/cmd"
func main() { func main() {
cmd.Execute() cmd.RootCmd.Execute()
} }