diff --git a/README.md b/README.md index 3229b0f..62c65dd 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ USAGE: gtree [global options] command [command options] [arguments...] VERSION: - 1.9.2 / revision 136b147 + 1.9.3 / revision 136b147 COMMANDS: output, o, out Outputs tree from markdown. @@ -529,13 +529,11 @@ $ cat testdata/sample9.md - main.go ~/github.com/ddddddO/gtree $ cat testdata/sample9.md | gtree verify --strict -2023/07/25 01:04:04 Extra paths exist: example/noexist example/noexist/xxx Required paths does not exist: example/like_cli/kkk -exit status 1 ``` diff --git a/cmd/gtree/error.go b/cmd/gtree/error.go index 664d469..ea52c06 100644 --- a/cmd/gtree/error.go +++ b/cmd/gtree/error.go @@ -7,6 +7,7 @@ const ( exitCodeErrOpen exitCodeErrOutput exitCodeErrMkdir + exitCodeErrVerify ) func exitErrOpts(err error) cli.ExitCoder { @@ -24,3 +25,7 @@ func exitErrOutput(err error) cli.ExitCoder { func exitErrMkdir(err error) cli.ExitCoder { return cli.Exit(err, exitCodeErrMkdir) } + +func exitErrVerify(err error) cli.ExitCoder { + return cli.Exit(err, exitCodeErrVerify) +} diff --git a/cmd/gtree/main.go b/cmd/gtree/main.go index 125c9da..6bab723 100644 --- a/cmd/gtree/main.go +++ b/cmd/gtree/main.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "log" "os" "time" @@ -206,7 +205,7 @@ func main() { } if err := app.Run(os.Args); err != nil { - log.Fatal(err) + fmt.Fprint(os.Stderr, err) } } @@ -328,10 +327,7 @@ func actionVerify(c *cli.Context) error { } if err := verify(in, options); err != nil { - if errors.As(err, >ree.VerifyError{}) { - return errors.New(fmt.Sprintf("\n%s", err.Error())) - } - return err + return exitErrVerify(err) } return nil } diff --git a/simple_tree_verifier.go b/simple_tree_verifier.go index 07f40dc..83aa27a 100644 --- a/simple_tree_verifier.go +++ b/simple_tree_verifier.go @@ -6,11 +6,12 @@ import ( "io/fs" "os" "path/filepath" + "strings" ) func newVerifierSimple(dir string, strict bool) verifierSimple { targetDir := "." - if len(targetDir) != 0 { + if len(dir) != 0 { targetDir = dir } @@ -48,7 +49,7 @@ func (dv *defaultVerifierSimple) verifyRoot(root *Node) ([]string, []string, err } dirsFilesystem := map[string]struct{}{} - existDirs := []string{} + extraDirs := []string{} rootPath := root.path() fileSystem := os.DirFS(filepath.Join(dv.targetDir, rootPath)) err := fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error { @@ -64,7 +65,7 @@ func (dv *defaultVerifierSimple) verifyRoot(root *Node) ([]string, []string, err if _, ok := dirsMarkdown[dir]; !ok { // Markdownに無いパスがディレクトリに有る => strictモードでエラー - existDirs = append(existDirs, dir) + extraDirs = append(extraDirs, dir) } dirsFilesystem[dir] = struct{}{} @@ -82,7 +83,7 @@ func (dv *defaultVerifierSimple) verifyRoot(root *Node) ([]string, []string, err } } - return existDirs, noExistDirs, nil + return extraDirs, noExistDirs, nil } func (dv *defaultVerifierSimple) recursive(node *Node, dirs map[string]struct{}) error { @@ -129,5 +130,5 @@ func (v VerifyError) Error() string { if len(v.noExists) != 0 { msg += fmt.Sprintf("Required paths does not exist:\n%s", tabPrefix(v.noExists)) } - return msg + return strings.TrimSuffix(msg, "\n") } diff --git a/verify.png b/verify.png index cad36dc..5a9f3d2 100644 Binary files a/verify.png and b/verify.png differ