Skip to content

Commit

Permalink
feat: accept - or /dev/stdin instead of -in (#28)
Browse files Browse the repository at this point in the history
Most command line utilities handle stdin with the `-` or `/dev/stdin`
argument. This PR changes the stdin operation detection from `-in` to
`-` or `/dev/stdin`. This detection is done manually to remain working
on all platforms.
  • Loading branch information
braydonk committed Aug 25, 2022
1 parent 5ad1599 commit 79e66ea
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -65,7 +65,7 @@ The CLI supports 3 operation modes:
- Format the matched files and output the diff to `stdout`
* Lint (`-lint` flag)
- Format the matched files and output the diff to `stdout`, exits with status 1 if there are any differences
* Stdin (`-in` flag)
* Stdin (just `-` or `/dev/stdin` argument)
- Format the yaml data from `stdin` and output the result to `stdout`

(NOTE: If providing paths as command line arguments, the flags must be specified before any paths)
11 changes: 8 additions & 3 deletions cmd/yamlfmt/main.go
Expand Up @@ -32,7 +32,6 @@ var (
source yaml and formatted yaml.`)
dry *bool = flag.Bool("dry", false, `Perform a dry run; show the output of a formatting
operation without performing it.`)
in *bool = flag.Bool("in", false, "Format yaml read from stdin and output to stdout")
)

const defaultConfigName = ".yamlfmt"
Expand All @@ -51,12 +50,14 @@ func run() error {
op = command.OperationLint
} else if *dry {
op = command.OperationDry
} else if *in {
op = command.OperationStdin
} else {
op = command.OperationFormat
}

if len(flag.Args()) == 1 && isStdin(flag.Args()[0]) {
op = command.OperationStdin
}

configData, err := readDefaultConfigFile()
if err != nil {
if errors.Is(err, os.ErrNotExist) {
Expand Down Expand Up @@ -101,3 +102,7 @@ func readConfig(path string) (map[string]interface{}, error) {
func getFullRegistry() *yamlfmt.Registry {
return yamlfmt.NewFormatterRegistry(&basic.BasicFormatterFactory{})
}

func isStdin(arg string) bool {
return arg == "-" || arg == "/dev/stdin"
}

0 comments on commit 79e66ea

Please sign in to comment.