diff --git a/cmd/cmds_style.go b/cmd/cmds_style.go index 351a382..e6aa29b 100644 --- a/cmd/cmds_style.go +++ b/cmd/cmds_style.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/sincerefly/capybara/base/log" "github.com/sincerefly/capybara/cmd/border" "github.com/spf13/cobra" ) @@ -15,5 +16,11 @@ func init() { var borderCmd = &cobra.Command{ Use: "border", Short: "To batch add borders to images.", - Run: func(cmd *cobra.Command, args []string) {}, + Run: func(cmd *cobra.Command, args []string) { + if len(args) > 0 { + log.Warnf("border style '%s' unsupported", args[0]) + } else { + log.Warnf("border need subcommand, e.g., 'capybara border simple [parameter]'") + } + }, } diff --git a/service/border/handler.go b/service/border/handler.go index 530046f..2f1fdd3 100644 --- a/service/border/handler.go +++ b/service/border/handler.go @@ -6,8 +6,7 @@ import ( "github.com/sincerefly/capybara/constants" "github.com/sincerefly/capybara/service/border/styles" "github.com/sincerefly/capybara/structure/fileitem" - "github.com/sincerefly/capybara/utils" - "github.com/spf13/afero" + "github.com/sincerefly/capybara/utils/fsutil" "path/filepath" "slices" "strings" @@ -41,11 +40,6 @@ func NewStyleProcessor(style Style, params Parameterizable) *StyleProcessor { func (s *StyleProcessor) Run() { - style := s.style - if !slices.Contains(s.Supports(), style) { // check - log.Fatal("unsupported style") - } - fiStore, err := s.collectInputs() if err != nil { log.Fatal(err) @@ -55,7 +49,7 @@ func (s *StyleProcessor) Run() { log.Fatal(err) } - switch style { + switch s.style { case StyleSimple: params := s.params.(*styles.SimpleParameter) err = styles.NewSimpleProcessor(params, fiStore).Run() @@ -69,10 +63,6 @@ func (s *StyleProcessor) Run() { log.Info("finished") } -func (s *StyleProcessor) Supports() []Style { - return []Style{StyleSimple, StyleTextBottom} -} - func (s *StyleProcessor) SupportExtensions() []string { return []string{constants.ExtJPG, constants.ExtPNG, constants.ExtJPEG} } @@ -84,8 +74,7 @@ func (s *StyleProcessor) collectInputs() (*fileitem.Store, error) { input := s.params.Input() output := s.params.Output() - fs := afero.NewOsFs() - srcImagePaths, err := utils.GetAllFiles(fs, s.params.Input()) + srcImagePaths, err := fsutil.ListFiles(s.params.Input()) if err != nil { return nil, fmt.Errorf("load input folder failed, %v", err) } @@ -121,7 +110,7 @@ func (s *StyleProcessor) collectInputs() (*fileitem.Store, error) { // prepare output dirs func (s *StyleProcessor) prepareOutputDirs(fiStore *fileitem.Store) error { for _, outputDir := range fiStore.GetTargetPaths() { - if err := utils.MkdirAll(outputDir); err != nil { + if err := fsutil.MkdirAll(outputDir); err != nil { return err } } diff --git a/utils/dirfile.go b/utils/fsutil/fs.go similarity index 61% rename from utils/dirfile.go rename to utils/fsutil/fs.go index 942c6b4..76d1afd 100644 --- a/utils/dirfile.go +++ b/utils/fsutil/fs.go @@ -1,4 +1,4 @@ -package utils +package fsutil import ( "github.com/spf13/afero" @@ -8,36 +8,35 @@ import ( var appFs afero.Fs -func MkdirAll(path string) error { +func init() { + appFs = afero.NewOsFs() +} - if appFs == nil { - appFs = afero.NewOsFs() - } +func MkdirAll(path string) error { fs := afero.NewOsFs() return fs.MkdirAll(path, 0755) } -// GetAllFiles all files -func GetAllFiles(fs afero.Fs, root string) ([]string, error) { - var files []string - - err := afero.Walk(fs, root, func(path string, info os.FileInfo, err error) error { +func ListFiles(root string) ([]string, error) { + var paths []string + walkFn := func(path string, info os.FileInfo, err error) error { if err != nil { return err } if !info.IsDir() { - files = append(files, path) + paths = append(paths, path) } return nil - }) - + } + err := afero.Walk(appFs, root, walkFn) if err != nil { return nil, err } - return files, nil + return paths, nil } func ExecutableDir() (string, error) { + executablePath, err := os.Executable() if err != nil { return "", err