diff --git a/README.md b/README.md index 4a684e5..ae2045c 100644 --- a/README.md +++ b/README.md @@ -240,7 +240,7 @@ To use `helmfmt` as a pre-commit hook, add the following to your `.pre-commit-co ```yaml repos: - repo: https://github.com/digitalstudium/helmfmt - rev: v0.4.1 + rev: v0.4.2 hooks: - id: helmfmt ``` diff --git a/VERSION b/VERSION index 267577d..2b7c5ae 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.4.1 +0.4.2 diff --git a/main.go b/main.go index a4fbf33..e91679e 100644 --- a/main.go +++ b/main.go @@ -109,19 +109,16 @@ func run() int { stat, _ := os.Stdin.Stat() stdinPiped := (stat.Mode() & os.ModeCharDevice) == 0 - if stdinPiped { - // Process from stdin - if len(args) > 0 { - return fmt.Errorf("cannot specify files when reading from stdin") - } - return processStdin(config) - } - + // If --files flag is used, process the provided files if files { - // Files mode if len(args) == 0 { + // --files with no args means read filenames from stdin (pre-commit style) + if stdinPiped { + return processFilesFromStdin(config, stdout) + } return fmt.Errorf("--files requires at least one file argument") } + // --files with args means process those files exitCode := process(args, stdout, config) if exitCode != 0 { os.Exit(exitCode) @@ -129,6 +126,11 @@ func run() int { return nil } + // If stdin is piped and no --files flag, process stdin as content + if stdinPiped && len(args) == 0 { + return processStdin(config) + } + // Chart mode if len(args) != 1 { return fmt.Errorf("chart mode requires exactly one chart path") @@ -163,6 +165,33 @@ func run() int { return 0 } +func processFilesFromStdin(config *Config, stdout bool) error { + // Read filenames from stdin (one per line) + input, err := io.ReadAll(os.Stdin) + if err != nil { + return fmt.Errorf("error reading from stdin: %w", err) + } + + lines := strings.Split(strings.TrimSpace(string(input)), "\n") + var filenames []string + for _, line := range lines { + line = strings.TrimSpace(line) + if line != "" { + filenames = append(filenames, line) + } + } + + if len(filenames) == 0 { + return fmt.Errorf("no files provided via stdin") + } + + exitCode := process(filenames, stdout, config) + if exitCode != 0 { + os.Exit(exitCode) + } + return nil +} + func processStdin(config *Config) error { // Read all input from stdin input, err := io.ReadAll(os.Stdin)