Skip to content

Commit 9be49cc

Browse files
committed
Add --space-redirects flag
1 parent b2df033 commit 9be49cc

File tree

9 files changed

+42
-27
lines changed

9 files changed

+42
-27
lines changed

.pre-commit-hooks.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- --write
88
- --newline
99
- --indent
10+
- --space-redirects
1011
description: Format Dockerfile files
1112
entry: dockerfmt
1213
files: ^.*Dockerfile.*$

README.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ Available Commands:
2121
version Print the version number of dockerfmt
2222
2323
Flags:
24-
-c, --check Check if the file(s) are formatted
25-
-h, --help help for dockerfmt
26-
-i, --indent uint Number of spaces to use for indentation (default 4)
27-
-n, --newline End the file with a trailing newline
28-
-w, --write Write the formatted output back to the file(s)
24+
-c, --check Check if the file(s) are formatted
25+
-h, --help help for dockerfmt
26+
-i, --indent uint Number of spaces to use for indentation (default 4)
27+
-n, --newline End the file with a trailing newline
28+
-s, --space-redirects Redirect operators will be followed by a space
29+
-w, --write Write the formatted output back to the file(s)
2930
3031
Use "dockerfmt [command] --help" for more information about a command.
3132
```

cmd/root.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import (
1111
)
1212

1313
var (
14-
writeFlag bool
15-
checkFlag bool
16-
newlineFlag bool
17-
indentSize uint
14+
writeFlag bool
15+
checkFlag bool
16+
newlineFlag bool
17+
indentSize uint
18+
spaceRedirects bool
1819
)
1920

2021
var rootCmd = &cobra.Command{
@@ -31,7 +32,12 @@ func Run(cmd *cobra.Command, args []string) {
3132
if err != nil {
3233
log.Fatalf("Failed to read file %s: %v", fileName, err)
3334
}
34-
formattedLines := lib.FormatFileLines(originalLines, indentSize, newlineFlag)
35+
c := &lib.Config{
36+
IndentSize: indentSize,
37+
TrailingNewline: newlineFlag,
38+
SpaceRedirects: spaceRedirects,
39+
}
40+
formattedLines := lib.FormatFileLines(originalLines, c)
3541

3642
if checkFlag {
3743
// Check if the file is already formatted
@@ -58,6 +64,7 @@ func init() {
5864
rootCmd.Flags().BoolVarP(&checkFlag, "check", "c", false, "Check if the file(s) are formatted")
5965
rootCmd.Flags().BoolVarP(&newlineFlag, "newline", "n", false, "End the file with a trailing newline")
6066
rootCmd.Flags().UintVarP(&indentSize, "indent", "i", 4, "Number of spaces to use for indentation")
67+
rootCmd.Flags().BoolVarP(&spaceRedirects, "space-redirects", "s", false, "Redirect operators will be followed by a space")
6168
}
6269

6370
func Execute() {

cmd/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ var versionCmd = &cobra.Command{
1414
Use: "version",
1515
Short: "Print the version number of dockerfmt",
1616
Run: func(cmd *cobra.Command, args []string) {
17-
fmt.Println("dockerfmt 0.2.8")
17+
fmt.Println("dockerfmt 0.3.0")
1818
},
1919
}

js/format.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ import (
1010
)
1111

1212
//export formatBytes
13-
func formatBytes(contents []byte, indentSize uint, newlineFlag bool) *byte {
13+
func formatBytes(contents []byte, indentSize uint, newlineFlag bool, spaceRedirects bool) *byte {
1414
originalLines := strings.SplitAfter(string(contents), "\n")
15-
result := lib.FormatFileLines(originalLines, indentSize, newlineFlag)
15+
c := &lib.Config{
16+
IndentSize: indentSize,
17+
TrailingNewline: newlineFlag,
18+
SpaceRedirects: spaceRedirects,
19+
}
20+
result := lib.FormatFileLines(originalLines, c)
1621
bytes := []byte(result)
1722
return &bytes[0]
1823
}

js/format.ts

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import './wasm_exec.js'
33
export interface FormatOptions {
44
indent: number
55
trailingNewline: boolean
6+
spaceRedirects: boolean
67
}
78

89
export const formatDockerfileContents = async (
@@ -34,6 +35,7 @@ export const formatDockerfileContents = async (
3435
length: number,
3536
indent: number,
3637
trailingNewline: boolean,
38+
spaceRedirects: boolean,
3739
) => number
3840
}
3941

@@ -48,6 +50,7 @@ export const formatDockerfileContents = async (
4850
fileBufferBytes.byteLength,
4951
options.indent,
5052
options.trailingNewline,
53+
options.spaceRedirects,
5154
)
5255

5356
// Decode the result

js/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@reteps/dockerfmt",
3-
"version": "0.2.8",
3+
"version": "0.3.0",
44
"type": "module",
55
"description": "",
66
"repository": "git+https://github.com/reteps/dockerfmt/tree/main/js",

lib/format.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type ParseState struct {
3434
type Config struct {
3535
IndentSize uint
3636
TrailingNewline bool
37+
SpaceRedirects bool
3738
}
3839

3940
func FormatNode(ast *ExtendedNode, c *Config) (string, bool) {
@@ -120,10 +121,7 @@ func FormatOnBuild(n *ExtendedNode, c *Config) string {
120121
return n.OriginalMultiline
121122
}
122123

123-
func FormatFileLines(fileLines []string, indentSize uint, trailingNewline bool) string {
124-
// Top level library function that formats file contents given the lines and the config.
125-
// Since we want this to work exposed to WASM, each config flag should be passed in separately
126-
// and not as a struct.
124+
func FormatFileLines(fileLines []string, c *Config) string {
127125
result, err := parser.Parse(strings.NewReader(strings.Join(fileLines, "")))
128126
if err != nil {
129127
log.Printf("%s\n", strings.Join(fileLines, ""))
@@ -136,10 +134,7 @@ func FormatFileLines(fileLines []string, indentSize uint, trailingNewline bool)
136134
AllOriginalLines: fileLines,
137135
}
138136
rootNode := BuildExtendedNode(result.AST, fileLines)
139-
parseState.Config = &Config{
140-
IndentSize: indentSize,
141-
TrailingNewline: trailingNewline,
142-
}
137+
parseState.Config = c
143138
parseState.processNode(rootNode)
144139

145140
// After all directives are processed, we need to check if we have any trailing comments to add.
@@ -150,7 +145,7 @@ func FormatFileLines(fileLines []string, indentSize uint, trailingNewline bool)
150145

151146
parseState.Output = strings.TrimRight(parseState.Output, "\n")
152147
// Ensure the output ends with a newline if requested
153-
if trailingNewline {
148+
if c.TrailingNewline {
154149
parseState.Output += "\n"
155150
}
156151
return parseState.Output
@@ -243,7 +238,7 @@ func formatShell(content string, hereDoc bool, c *Config) string {
243238

244239
// Now that we have a valid bash-style command, we can format it with shfmt
245240
// log.Printf("Content1: %s\n", content)
246-
content = formatBash(content, c.IndentSize)
241+
content = formatBash(content, c)
247242
// log.Printf("Content2: %s\n", content)
248243

249244
if !hereDoc {
@@ -468,7 +463,7 @@ func IndentFollowingLines(lines string, indentSize uint) string {
468463
return result
469464
}
470465

471-
func formatBash(s string, indentSize uint) string {
466+
func formatBash(s string, c *Config) string {
472467
r := strings.NewReader(s)
473468
f, err := syntax.NewParser(syntax.KeepComments(true)).Parse(r, "")
474469
if err != nil {
@@ -479,7 +474,8 @@ func formatBash(s string, indentSize uint) string {
479474
syntax.NewPrinter(
480475
syntax.Minify(false),
481476
syntax.SingleLine(false),
482-
syntax.Indent(indentSize),
477+
syntax.SpaceRedirects(c.SpaceRedirects),
478+
syntax.Indent(c.IndentSize),
483479
syntax.BinaryNextLine(true),
484480
).Print(buf, f)
485481
return buf.String()

tests/run.dockerfile

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
RUN ["ls" , "-la"]
1+
RUN ["ls" , "-la"]
2+
3+
RUN echo foo >> bar

0 commit comments

Comments
 (0)