refactor: major project restructure
This commit is contained in:
+12
-48
@@ -1,63 +1,27 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/mathismqn/godeez/internal/config"
|
||||
"github.com/mathismqn/godeez/internal/db"
|
||||
"github.com/mathismqn/godeez/internal/utils"
|
||||
"github.com/mathismqn/godeez/internal/app"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
cfgDir string
|
||||
musicDir string
|
||||
appDir string
|
||||
cfgFile string
|
||||
cfgPath string
|
||||
appCtx *app.Context
|
||||
)
|
||||
|
||||
var RootCmd = &cobra.Command{
|
||||
Use: "godeez",
|
||||
Short: "GoDeez is a tool to download music from Deezer",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
cmd.Help()
|
||||
Use: "godeez",
|
||||
Short: "GoDeez is a tool to download music from Deezer",
|
||||
SilenceUsage: true,
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
var err error
|
||||
appCtx, err = app.NewContext(cfgPath)
|
||||
|
||||
return err
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.godeez)")
|
||||
cobra.OnInitialize(func() {
|
||||
initDirs()
|
||||
config.Init(cfgFile, cfgDir)
|
||||
db.Init(cfgDir)
|
||||
})
|
||||
}
|
||||
|
||||
func initDirs() {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not get home directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
cfgDir = filepath.Join(home, ".godeez")
|
||||
if err := utils.EnsureDir(cfgDir); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not create app directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
musicDir = filepath.Join(home, "Music")
|
||||
if err := utils.EnsureDir(musicDir); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not create music directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
appDir = path.Join(musicDir, "GoDeez")
|
||||
if err := utils.EnsureDir(appDir); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not create GoDeez directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
RootCmd.PersistentFlags().StringVar(&cfgPath, "config", "", "config file (default is $HOME/.godeez)")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user