Skip to content
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
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,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.2.0
rev: v0.3.0
hooks:
- id: helmfmt
```
Expand All @@ -258,7 +258,6 @@ Add these lines to your `settings.json`:
"formatter": {
"external": {
"command": "helmfmt",
"arguments": ["--files", "{buffer_path}", "--stdout"]
}
}
}
Expand All @@ -277,7 +276,7 @@ Add these lines to your settings:
```json
"advancedLocalFormatters.formatters": [
{
"command": ["helmfmt", "--files", "$absoluteFilePath", "--stdout"],
"command": ["helmfmt"],
"languages": ["helm"]
}
],
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.0
0.3.0
38 changes: 36 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
Expand Down Expand Up @@ -85,7 +86,7 @@ func run() int {
RunE: func(cmd *cobra.Command, args []string) error {
// Apply rule overrides from flags
for _, rule := range disableRules {
if _, exists := config.Rules.Indent[rule]; exists { // Updated access pattern
if _, exists := config.Rules.Indent[rule]; exists {
ruleConfig := config.Rules.Indent[rule]
ruleConfig.Disabled = true
config.Rules.Indent[rule] = ruleConfig
Expand All @@ -95,7 +96,7 @@ func run() int {
}

for _, rule := range enableRules {
if _, exists := config.Rules.Indent[rule]; exists { // Updated access pattern
if _, exists := config.Rules.Indent[rule]; exists {
ruleConfig := config.Rules.Indent[rule]
ruleConfig.Disabled = false
config.Rules.Indent[rule] = ruleConfig
Expand All @@ -104,6 +105,18 @@ func run() int {
}
}

// Check if stdin is being piped
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 {
// Files mode
if len(args) == 0 {
Expand Down Expand Up @@ -150,6 +163,27 @@ func run() int {
return 0
}

func processStdin(config *Config) error {
// Read all input from stdin
input, err := io.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("error reading from stdin: %w", err)
}

orig := string(input)

// Validate syntax
if err := validateTemplateSyntax(orig); err != nil {
return fmt.Errorf("invalid syntax: %w", err)
}

// Format and output to stdout
formatted := ensureTrailingNewline(formatIndentation(orig, config, "<stdin>"))
fmt.Print(formatted)

return nil
}

func collectFiles(root string, config *Config) ([]string, error) {
var out []string
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
Expand Down
47 changes: 46 additions & 1 deletion templates_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"bytes"
"io"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -73,7 +75,7 @@ func TestFormatIndentationFromTemplates(t *testing.T) {
t.Fatalf("Failed to read expected file %s: %v", expectedPath, err)
}

// Format using the input file path for exclusion matching
// Test 1: Direct formatting (file mode)
result := formatIndentation(string(inputContent), config, testCase.InputFile)
result = ensureTrailingNewline(result)
expected := string(expectedContent)
Expand All @@ -83,6 +85,49 @@ func TestFormatIndentationFromTemplates(t *testing.T) {
t.Errorf("Test '%s' failed\nInput file: %s\nExpected file: %s\nExpected:\n%s\n\nGot:\n%s",
testCase.Name, testCase.InputFile, testCase.ExpectedFile, expected, result)
}

// Test 2: stdin mode (skip if test uses file exclusion patterns)
hasExcludePatterns := false
if testCase.Config != nil && testCase.Config.Rules.Indent != nil {
for _, ruleConfig := range testCase.Config.Rules.Indent {
if len(ruleConfig.Exclude) > 0 {
hasExcludePatterns = true
break
}
}
}

if !hasExcludePatterns {
t.Run("stdin", func(t *testing.T) {
// Setup stdin/stdout pipes
oldStdin, oldStdout := os.Stdin, os.Stdout
r, w, _ := os.Pipe()
os.Stdin = r

rOut, wOut, _ := os.Pipe()
os.Stdout = wOut

// Write input and process
go func() {
w.Write(inputContent)
w.Close()
}()

processStdin(config)

wOut.Close()
os.Stdin, os.Stdout = oldStdin, oldStdout

// Read output
var buf bytes.Buffer
io.Copy(&buf, rOut)

if buf.String() != expected {
t.Errorf("Test '%s' failed (stdin)\nExpected:\n%s\n\nGot:\n%s",
testCase.Name, expected, buf.String())
}
})
}
})
}
}