From 9aa43f4e945aafdff22debcee9a1028acdb7c7c7 Mon Sep 17 00:00:00 2001 From: Braydon Kains <93549768+braydonk@users.noreply.github.com> Date: Thu, 25 Aug 2022 09:57:26 -0400 Subject: [PATCH] Restore in flag (#33) * fix: restore functionality of -in flag Originally I had changed the read from stdin to be specified by a single - argument or /dev/stdin by suggestion from #26. In that PR I had removed the -in flag. After sleeping on it I realized I might as well keep that flag, since it might intuitively make sense to more people, makes more sense on Windows, and maintains backwards compatibility (even though that is currently not a promise of the project, might as well do it if it's easy). * docs: add -in flag back to README --- README.md | 2 +- cmd/yamlfmt/flags.go | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 70b54f4..d8feed2 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ The CLI supports the following flags/arguments: - 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 (just `-` or `/dev/stdin` argument) +* Stdin (just `-` or `/dev/stdin` argument, or `-in` flag) - Format the yaml data from `stdin` and output the result to `stdout` * Custom config path (`-conf` flag) - If you would like to use a config not stored at `.yamlfmt` in the working directory, you can pass a relative or absolute path to a separate configuration file diff --git a/cmd/yamlfmt/flags.go b/cmd/yamlfmt/flags.go index d9b7a57..9fd6dbc 100644 --- a/cmd/yamlfmt/flags.go +++ b/cmd/yamlfmt/flags.go @@ -28,11 +28,12 @@ var ( source yaml and formatted yaml.`) flagDry *bool = flag.Bool("dry", false, `Perform a dry run; show the output of a formatting operation without performing it.`) + flagIn *bool = flag.Bool("in", false, "Format yaml read from stdin and output to stdout") flagConf *string = flag.String("conf", "", "Read yamlfmt config from this path") ) func getOperation() command.Operation { - if len(flag.Args()) == 1 && isStdin(flag.Args()[0]) { + if *flagIn || isStdinArg() { return command.OperationStdin } if *flagLint { @@ -44,7 +45,11 @@ func getOperation() command.Operation { return command.OperationFormat } -func isStdin(arg string) bool { +func isStdinArg() bool { + if len(flag.Args()) != 1 { + return false + } + arg := flag.Args()[0] return arg == "-" || arg == "/dev/stdin" }