refactor: prepare the ground for watch command

This commit is contained in:
Antoine Aflalo
2024-08-27 14:19:07 -04:00
parent c6a8caf317
commit 7104121fcf
7 changed files with 49 additions and 20 deletions

View File

@@ -4,7 +4,7 @@ import (
"fmt"
"github.com/belphemur/CBZOptimizer/converter"
"github.com/belphemur/CBZOptimizer/converter/constant"
"github.com/belphemur/CBZOptimizer/manga"
"github.com/belphemur/CBZOptimizer/utils"
"github.com/spf13/cobra"
"github.com/thediveo/enumflag/v2"
"os"
@@ -38,22 +38,13 @@ func init() {
AddCommand(command)
}
// isValidFolder checks if the provided path is a valid directory
func isValidFolder(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
return info.IsDir()
}
func ConvertCbzCommand(cmd *cobra.Command, args []string) error {
path := args[0]
if path == "" {
return fmt.Errorf("path is required")
}
if !isValidFolder(path) {
if !utils.IsValidFolder(path) {
return fmt.Errorf("the path needs to be a folder")
}
@@ -95,7 +86,7 @@ func ConvertCbzCommand(cmd *cobra.Command, args []string) error {
go func() {
defer wg.Done()
for path := range fileChan {
err := manga.Optimize(chapterConverter, path, quality, override)
err := utils.Optimize(chapterConverter, path, quality, override)
if err != nil {
errorChan <- fmt.Errorf("error processing file %s: %w", path, err)
}

View File

@@ -5,6 +5,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"path/filepath"
"runtime"
)
var rootCmd = &cobra.Command{
@@ -12,15 +14,36 @@ var rootCmd = &cobra.Command{
Short: "Convert CBZ files using a specified converter",
}
func getPath() string {
return filepath.Join(map[string]string{
"windows": filepath.Join(os.Getenv("APPDATA")),
"darwin": filepath.Join(os.Getenv("HOME"), ".config"),
"linux": filepath.Join(os.Getenv("HOME"), ".config"),
}[runtime.GOOS], "CBZOptimizer")
}
func init() {
viper.SetConfigName("CBZOptimizer")
configFolder := getPath()
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(configFolder)
viper.SetEnvPrefix("CBZ")
viper.AutomaticEnv()
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
err := os.MkdirAll(configFolder, os.ModePerm)
if err != nil {
panic(fmt.Errorf("fatal error config file: %w", err))
}
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
err := viper.SafeWriteConfig()
if err != nil {
panic(fmt.Errorf("fatal error config file: %w", err))
}
} else {
panic(fmt.Errorf("fatal error config file: %w", err))
}
}
}
// Execute executes the root command.