Skip to content

Commit

Permalink
style: valid border 'style' parameter, rename fs utils
Browse files Browse the repository at this point in the history
  • Loading branch information
sincerefly committed Jul 4, 2024
1 parent da716cd commit 4e0e103
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
9 changes: 8 additions & 1 deletion cmd/cmds_style.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/sincerefly/capybara/base/log"
"github.com/sincerefly/capybara/cmd/border"
"github.com/spf13/cobra"
)
Expand All @@ -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]'")
}
},
}
19 changes: 4 additions & 15 deletions service/border/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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}
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
}
Expand Down
27 changes: 13 additions & 14 deletions utils/dirfile.go → utils/fsutil/fs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package fsutil

import (
"github.com/spf13/afero"
Expand All @@ -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
Expand Down

0 comments on commit 4e0e103

Please sign in to comment.