Skip to content

Commit

Permalink
modify CLI output option
Browse files Browse the repository at this point in the history
  • Loading branch information
ddddddO committed Aug 8, 2023
1 parent a941116 commit e764353
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 70 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ OPTIONS:
--file value, -f value specify the path to markdown file. (default: stdin)
--massive, -m set this option when there are very many blocks of markdown. (default: false)
--massive-timeout value, --mt value set this option if you want to set a timeout. (default: 0s)
--json, -j set this option when outputting JSON. (default: tree)
--yaml, -y set this option when outputting YAML. (default: tree)
--toml, -t set this option when outputting TOML. (default: tree)
--format value set this option when specifying output format. "json", "yaml", "toml"
--watch, -w follow changes in markdown file. (default: false)
--help, -h show help
```
Expand Down Expand Up @@ -237,7 +235,7 @@ a
#### Output JSON

```console
$ cat testdata/sample5.md | gtree output -j | jq
$ cat testdata/sample5.md | gtree output --format json | jq
{
"value": "a",
"children": [
Expand Down Expand Up @@ -283,7 +281,7 @@ $ cat testdata/sample5.md | gtree output -j | jq
#### Output YAML

```console
$ cat testdata/sample5.md | gtree output -y
$ cat testdata/sample5.md | gtree output --format yaml
value: a
children:
- value: i
Expand All @@ -307,7 +305,7 @@ children:
#### Output TOML

```console
$ cat testdata/sample5.md | gtree output -t
$ cat testdata/sample5.md | gtree output --format toml
value = 'a'
[[children]]
value = 'i'
Expand Down
20 changes: 3 additions & 17 deletions cmd/gtree/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,9 @@ func main() {
return nil
},
},
&cli.BoolFlag{
Name: "json",
Aliases: []string{"j"},
Usage: "set this option when outputting JSON.",
DefaultText: "tree",
},
&cli.BoolFlag{
Name: "yaml",
Aliases: []string{"y"},
Usage: "set this option when outputting YAML.",
DefaultText: "tree",
},
&cli.BoolFlag{
Name: "toml",
Aliases: []string{"t"},
Usage: "set this option when outputting TOML.",
DefaultText: "tree",
&cli.StringFlag{
Name: "format",
Usage: `set this option when specifying output format. "json", "yaml", "toml"`,
},
&cli.BoolFlag{
Name: "watch",
Expand Down
55 changes: 8 additions & 47 deletions cmd/gtree/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,56 +53,17 @@ func outputContinuously(markdownPath string, options []gtree.Option) error {
return nil
}

type encodeType uint

const (
encodeJSON encodeType = 1 << iota
encodeYAML
encodeTOML
)

type stateOutputFormat struct {
encode encodeType
}

func optionOutput(c *cli.Context) (gtree.Option, error) {
s := &stateOutputFormat{}

if c.Bool("json") {
s.encode |= encodeJSON
}
if c.Bool("yaml") {
s.encode |= encodeYAML
}
if c.Bool("toml") {
s.encode |= encodeTOML
}

return s.decideOption()
}

func (s *stateOutputFormat) decideOption() (gtree.Option, error) {
if err := s.validate(); err != nil {
return nil, err
}

switch s.encode {
case encodeJSON:
switch c.String("format") {
case "json":
return gtree.WithEncodeJSON(), nil
case encodeYAML:
case "yaml":
return gtree.WithEncodeYAML(), nil
case encodeTOML:
case "toml":
return gtree.WithEncodeTOML(), nil
case "":
return nil, nil
default:
return nil, errors.New(`specify either "json" or "yaml" or "toml"`)
}
return nil, nil
}

const encodeDefault = encodeType(0)

func (s *stateOutputFormat) validate() error {
switch s.encode {
case encodeDefault, encodeJSON, encodeYAML, encodeTOML:
return nil
}
return errors.New(`choose either "json(j)" or "yaml(y)" or "toml(t)" or blank.`)
}

0 comments on commit e764353

Please sign in to comment.