Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: accept - or /dev/stdin instead of -in #28

Merged
merged 1 commit into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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"
}