diff --git a/cmd/optimize_command.go b/cmd/optimize_command.go index e443f9f..05210fb 100644 --- a/cmd/optimize_command.go +++ b/cmd/optimize_command.go @@ -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) } diff --git a/cmd/rootcmd.go b/cmd/rootcmd.go index 5fe0845..9ea70d9 100644 --- a/cmd/rootcmd.go +++ b/cmd/rootcmd.go @@ -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. diff --git a/converter/webp/webp_provider.go b/converter/webp/webp_provider.go index 0e09c73..5e9315c 100644 --- a/converter/webp/webp_provider.go +++ b/converter/webp/webp_provider.go @@ -1,7 +1,7 @@ package webp import ( - "github.com/belphemur/go-webpbin" + "github.com/belphemur/go-webpbin/v2" "image" "io" ) diff --git a/go.mod b/go.mod index be0d182..a9acc37 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,9 @@ go 1.23.0 require ( github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de - github.com/belphemur/go-webpbin v0.0.0-20240827154654-457b3fe588c4 + github.com/belphemur/go-webpbin/v2 v2.0.0 github.com/oliamb/cutter v0.2.2 + github.com/pablodz/inotifywaitgo v0.0.7 github.com/samber/lo v1.47.0 github.com/spf13/cobra v1.8.1 github.com/spf13/viper v1.19.0 diff --git a/go.sum b/go.sum index 1b4c188..b9e4645 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,8 @@ github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de h1:FxWPpzIjnTlhP github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de/go.mod h1:DCaWoUhZrYW9p1lxo/cm8EmUOOzAPSEZNGF2DK1dJgw= github.com/belphemur/go-binwrapper v0.0.0-20240827152605-33977349b1f0 h1:EzKgPYK90TyAOmytK7bvapqlkG/m7KWKK28mOAdQEaM= github.com/belphemur/go-binwrapper v0.0.0-20240827152605-33977349b1f0/go.mod h1:s2Dv+CfgVbNM9ucqvE5qCCC0AkI1PE2OZb7N8PPlOh4= -github.com/belphemur/go-webpbin v0.0.0-20240827154654-457b3fe588c4 h1:Xyf8bTqdR1tIGs4u3LLfUvXfPoVM6A+jHp6Vt/vqHec= -github.com/belphemur/go-webpbin v0.0.0-20240827154654-457b3fe588c4/go.mod h1:lDnzHELZjEX11ccC5WnVHwMRlbX1E2tsQxfnvG32rN4= +github.com/belphemur/go-webpbin/v2 v2.0.0 h1:Do0TTTJ6cS6lgi+R67De+jXRYe+ZOwxFqTiFggyX5p8= +github.com/belphemur/go-webpbin/v2 v2.0.0/go.mod h1:VIHXZQaIwaIYDn08w0qeJFPj1MuYt5pyJnkQALPYc5g= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -58,6 +58,8 @@ github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4 github.com/onsi/ginkgo/v2 v2.13.0/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o= github.com/onsi/gomega v1.28.1 h1:MijcGUbfYuznzK/5R4CPNoUP/9Xvuo20sXfEm6XxoTA= github.com/onsi/gomega v1.28.1/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ= +github.com/pablodz/inotifywaitgo v0.0.7 h1:1ii49dGBnRn0t1Sz7RGZS6/NberPEDQprwKHN49Bv6U= +github.com/pablodz/inotifywaitgo v0.0.7/go.mod h1:OtzRCsYTJlIr+vAzlOtauTkfQ1c25ebFuXq8tbbf8cw= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ= diff --git a/utils/file_utils.go b/utils/file_utils.go new file mode 100644 index 0000000..db8db39 --- /dev/null +++ b/utils/file_utils.go @@ -0,0 +1,12 @@ +package utils + +import "os" + +// 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() +} diff --git a/manga/optimize.go b/utils/optimize.go similarity index 98% rename from manga/optimize.go rename to utils/optimize.go index 43b303d..4b2e691 100644 --- a/manga/optimize.go +++ b/utils/optimize.go @@ -1,4 +1,4 @@ -package manga +package utils import ( "fmt"