From 9b74c18210b7f4c8f9304fa8c0d52bc2f3ee6f0c Mon Sep 17 00:00:00 2001 From: Ochi Daiki Date: Fri, 13 Jan 2023 23:42:49 +0900 Subject: [PATCH] exit code (#131) --- cmd/gtree/main.go | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/cmd/gtree/main.go b/cmd/gtree/main.go index 1ed2083..f3f1dfe 100644 --- a/cmd/gtree/main.go +++ b/cmd/gtree/main.go @@ -17,6 +17,13 @@ var ( Revision = "unset" ) +const ( + exitCodeErrOpts = iota + 1 + exitCodeErrOutput + exitCodeErrOpen + exitCodeErrMkdir +) + func main() { commonFlags := []cli.Flag{ &cli.PathFlag{ @@ -123,18 +130,18 @@ func main() { func actionOutput(c *cli.Context) error { oi, err := optionIndentation(c) if err != nil { - return cli.Exit(err, 1) + return cli.Exit(err, exitCodeErrOpts) } oo, err := optionOutput(c) if err != nil { - return cli.Exit(err, 1) + return cli.Exit(err, exitCodeErrOpts) } options := []gtree.Option{oi, oo} markdownPath := c.Path("file") if isInputStdin(markdownPath) { if err := output(os.Stdin, options); err != nil { - return cli.Exit(err, 1) + return cli.Exit(err, exitCodeErrOutput) } return nil } @@ -142,18 +149,18 @@ func actionOutput(c *cli.Context) error { if !c.Bool("watch") { f, err := os.Open(markdownPath) if err != nil { - return cli.Exit(err, 1) + return cli.Exit(err, exitCodeErrOpen) } defer f.Close() if err := output(f, options); err != nil { - return cli.Exit(err, 1) + return cli.Exit(err, exitCodeErrOutput) } return nil } if err := watchMarkdownAndOutput(markdownPath, options); err != nil { - return cli.Exit(err, 1) + return cli.Exit(err, exitCodeErrOutput) } return nil @@ -202,27 +209,27 @@ func actionMkdir(c *cli.Context) error { if !isInputStdin(markdownPath) { in, err = os.Open(markdownPath) if err != nil { - return cli.Exit(err, 1) + return cli.Exit(err, exitCodeErrOpen) } defer in.Close() } oi, err := optionIndentation(c) if err != nil { - return cli.Exit(err, 1) + return cli.Exit(err, exitCodeErrOpts) } oe := gtree.WithFileExtensions(c.StringSlice("extension")) options := []gtree.Option{oi, oe} if c.Bool("dry-run") { if err := outputWithValidation(in, options); err != nil { - return cli.Exit(err, 1) + return cli.Exit(err, exitCodeErrOutput) } return nil } if err := mkdir(in, options); err != nil { - return cli.Exit(err, 1) + return cli.Exit(err, exitCodeErrMkdir) } return nil