From 79e66eaec55092ecf4e35c5bfc27049e27a1fb7b Mon Sep 17 00:00:00 2001 From: Braydon Kains <93549768+braydonk@users.noreply.github.com> Date: Wed, 24 Aug 2022 21:35:43 -0400 Subject: [PATCH] feat: accept - or /dev/stdin instead of -in (#28) 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. --- README.md | 2 +- cmd/yamlfmt/main.go | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8d1bef2..c905242 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/cmd/yamlfmt/main.go b/cmd/yamlfmt/main.go index e756951..56197bf 100644 --- a/cmd/yamlfmt/main.go +++ b/cmd/yamlfmt/main.go @@ -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" @@ -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) { @@ -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" +}