@@ -2,6 +2,7 @@ package cmd
22
33import (
44 "fmt"
5+ "io"
56 "log"
67 "os"
78 "strings"
@@ -19,44 +20,79 @@ var (
1920)
2021
2122var rootCmd = & cobra.Command {
22- Use : "dockerfmt [Dockerfile]" ,
23+ Use : "dockerfmt [Dockerfile... ]" ,
2324 Short : "dockerfmt is a Dockerfile and RUN step formatter." ,
2425 Long : `A updated version of the dockfmt. Uses the dockerfile parser from moby/buildkit and the shell formatter from mvdan/sh.` ,
2526 Run : Run ,
26- Args : cobra .MinimumNArgs ( 1 ) ,
27+ Args : cobra .ArbitraryArgs ,
2728}
2829
2930func Run (cmd * cobra.Command , args []string ) {
30- for _ , fileName := range args {
31- originalLines , err := lib .GetFileLines (fileName )
31+ config := & lib.Config {
32+ IndentSize : indentSize ,
33+ TrailingNewline : newlineFlag ,
34+ SpaceRedirects : spaceRedirects ,
35+ }
36+
37+ allFormatted := true
38+
39+ if len (args ) == 0 {
40+ if writeFlag {
41+ log .Fatal ("Error: Cannot use -w/--write flag when reading from stdin" )
42+ }
43+
44+ inputBytes , err := io .ReadAll (os .Stdin )
3245 if err != nil {
33- log .Fatalf ("Failed to read file %s : %v" , fileName , err )
46+ log .Fatalf ("Failed to read from stdin : %v" , err )
3447 }
35- c := & lib.Config {
36- IndentSize : indentSize ,
37- TrailingNewline : newlineFlag ,
38- SpaceRedirects : spaceRedirects ,
48+
49+ if ! processInput ("stdin" , inputBytes , config ) {
50+ allFormatted = false // Mark as not formatted if check fails
3951 }
40- formattedLines := lib .FormatFileLines (originalLines , c )
41-
42- if checkFlag {
43- // Check if the file is already formatted
44- originalContent := strings .Join (originalLines , "" )
45- if originalContent != formattedLines {
46- fmt .Printf ("File %s is not formatted\n " , fileName )
47- os .Exit (1 )
48- }
49- } else if writeFlag {
50- // Write the formatted output back to the file
51- err := os .WriteFile (fileName , []byte (formattedLines ), 0644 )
52+
53+ } else {
54+ for _ , fileName := range args {
55+ inputBytes , err := os .ReadFile (fileName )
5256 if err != nil {
53- log .Fatalf ("Failed to write to file %s: %v" , fileName , err )
57+ log .Fatalf ("Failed to read file %s: %v" , fileName , err )
58+ }
59+
60+ if ! processInput (fileName , inputBytes , config ) {
61+ allFormatted = false
5462 }
55- } else {
56- // Print the formatted output to stdout
57- fmt .Printf ("%s" , formattedLines )
5863 }
5964 }
65+
66+ // If check mode was enabled and any input was not formatted, exit with status 1
67+ if checkFlag && ! allFormatted {
68+ os .Exit (1 )
69+ }
70+ }
71+
72+ func processInput (inputName string , inputBytes []byte , config * lib.Config ) (formatted bool ) {
73+ originalContent := string (inputBytes )
74+ lines := strings .SplitAfter (strings .TrimSuffix (originalContent , "\n " ), "\n " )
75+
76+ formattedContent := lib .FormatFileLines (lines , config )
77+
78+ if checkFlag {
79+ if originalContent != formattedContent {
80+ fmt .Printf ("%s is not formatted\n " , inputName )
81+ return false
82+ }
83+ return true
84+ } else if writeFlag {
85+ err := os .WriteFile (inputName , []byte (formattedContent ), 0644 )
86+ if err != nil {
87+ log .Fatalf ("Failed to write to file %s: %v" , inputName , err )
88+ }
89+ } else {
90+ _ , err := os .Stdout .Write ([]byte (formattedContent ))
91+ if err != nil {
92+ log .Fatalf ("Failed to write to stdout: %v" , err )
93+ }
94+ }
95+ return true
6096}
6197
6298func init () {
0 commit comments