Skip to content

Commit

Permalink
split error
Browse files Browse the repository at this point in the history
  • Loading branch information
ddddddO committed Jun 10, 2023
1 parent c2bbc4b commit f3acd0d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
26 changes: 26 additions & 0 deletions cmd/gtree/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import "github.com/urfave/cli/v2"

const (
exitCodeErrOpts = iota + 1
exitCodeErrOutput
exitCodeErrOpen
exitCodeErrMkdir
)

func exitErrOpts(err error) cli.ExitCoder {
return cli.Exit(err, exitCodeErrOpts)
}

func exitErrOutput(err error) cli.ExitCoder {
return cli.Exit(err, exitCodeErrOutput)
}

func exitErrOpen(err error) cli.ExitCoder {
return cli.Exit(err, exitCodeErrOpen)
}

func exitErrMkdir(err error) cli.ExitCoder {
return cli.Exit(err, exitCodeErrMkdir)
}
38 changes: 16 additions & 22 deletions cmd/gtree/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ var (
Revision = "unset"
)

const (
exitCodeErrOpts = iota + 1
exitCodeErrOutput
exitCodeErrOpen
exitCodeErrMkdir
)

func main() {
commonFlags := []cli.Flag{
&cli.PathFlag{
Expand Down Expand Up @@ -149,69 +142,70 @@ func notExistArgs(c *cli.Context) error {
func actionOutput(c *cli.Context) error {
oi, err := optionIndentation(c)
if err != nil {
return cli.Exit(err, exitCodeErrOpts)
return exitErrOpts(err)
}
oo, err := optionOutput(c)
if err != nil {
return cli.Exit(err, exitCodeErrOpts)
return exitErrOpts(err)
}
options := []gtree.Option{oi, oo}

markdownPath := c.Path("file")
if isInputStdin(markdownPath) {
if err := output(os.Stdin, options); err != nil {
return cli.Exit(err, exitCodeErrOutput)
return exitErrOutput(err)
}
return nil
}

if c.Bool("watch") {
if err := outputContinuously(markdownPath, options); err != nil {
return cli.Exit(err, exitCodeErrOutput)
return exitErrOutput(err)
}
} else {
f, err := os.Open(markdownPath)
if err != nil {
return cli.Exit(err, exitCodeErrOpen)
return exitErrOpen(err)
}
defer f.Close()

if err := output(f, options); err != nil {
return cli.Exit(err, exitCodeErrOutput)
return exitErrOutput(err)
}
}

return nil
}

func actionMkdir(c *cli.Context) error {
markdownPath := c.Path("file")
in := os.Stdin
var err error
if !isInputStdin(markdownPath) {
in, err = os.Open(markdownPath)
var (
in = os.Stdin
err error
)
if !isInputStdin(c.Path("file")) {
in, err = os.Open(c.Path("file"))
if err != nil {
return cli.Exit(err, exitCodeErrOpen)
return exitErrOpen(err)
}
defer in.Close()
}

oi, err := optionIndentation(c)
if err != nil {
return cli.Exit(err, exitCodeErrOpts)
return exitErrOpts(err)
}
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, exitCodeErrOutput)
return exitErrOutput(err)
}
return nil
}

if err := mkdir(in, options); err != nil {
return cli.Exit(err, exitCodeErrMkdir)
return exitErrMkdir(err)
}

return nil
Expand Down

0 comments on commit f3acd0d

Please sign in to comment.