Skip to content

Commit dc1ca04

Browse files
feat(cmd): add output option
1 parent b1a17a7 commit dc1ca04

5 files changed

Lines changed: 23 additions & 7 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,4 @@ tags
7373
# End of https://www.toptal.com/developers/gitignore/api/go,linux,vim,executable
7474

7575
*.benchmark
76+
out

cmd/dockerfile-parser/dockerfile-parser.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212
func main() {
1313
startTime := time.Now()
1414
recursive := slices.Contains(os.Args, "-r")
15-
count := wrapper.ParsePath(os.Args[len(os.Args)-1], recursive)
15+
output := slices.Contains(os.Args, "-o")
16+
count := wrapper.ParsePath(os.Args[len(os.Args)-1], recursive, output)
1617
diff := time.Now().Sub(startTime)
1718
fmt.Printf("Parsing %d files finished in %v\n", count, diff)
1819
}

internal/pkg/wrapper/wrapper.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@ import (
77
"strings"
88

99
"github.com/coffeemakingtoaster/dockerfile-parser/internal/pkg/display"
10+
"github.com/coffeemakingtoaster/dockerfile-parser/pkg/ast"
1011
"github.com/coffeemakingtoaster/dockerfile-parser/pkg/lexer"
1112
"github.com/coffeemakingtoaster/dockerfile-parser/pkg/parser"
1213
)
1314

14-
func ParsePath(path string, recursive bool) int {
15+
func ParsePath(path string, recursive, output bool) int {
1516
isFile, err := isFile(path)
1617
if err != nil {
1718
panic(err)
1819
}
1920
if isFile {
20-
parseAndDisplayFileList([]string{path})
21+
parseAndDisplayFileList([]string{path}, output)
2122
return 1
2223
} else {
2324
paths := buildDirPathList(path, recursive)
24-
parseAndDisplayFileList(paths)
25+
parseAndDisplayFileList(paths, output)
2526
return len(paths)
2627
}
2728
}
@@ -66,7 +67,7 @@ func isFile(path string) (bool, error) {
6667
}
6768
}
6869

69-
func parseAndDisplayFileList(paths []string) {
70+
func parseAndDisplayFileList(paths []string, output bool) {
7071
for _, path := range paths {
7172
fmt.Printf("---\t%s\t---\n", path)
7273
l, err := lexer.NewFromFile(path)
@@ -85,5 +86,18 @@ func parseAndDisplayFileList(paths []string) {
8586
continue
8687
}
8788
display.DisplayAst(root)
89+
if output {
90+
outputReconstructed(root, filepath.Base(path))
91+
}
92+
}
93+
}
94+
95+
func outputReconstructed(root *ast.StageNode, filename string) {
96+
os.MkdirAll("./out", 0755)
97+
content := root.Reconstruct()
98+
data := strings.Join(content, "\n")
99+
err := os.WriteFile(filepath.Join("./out", filename), []byte(data), 0755)
100+
if err != nil {
101+
fmt.Printf("Something went wrong: %s", err.Error())
88102
}
89103
}

pkg/ast/reconstruct.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (oi *OnbuildInstructionNode) Reconstruct() []string {
129129
func (ri *RunInstructionNode) Reconstruct() []string {
130130
reconstructed := fmt.Sprintf("%s ", ri.Instruction())
131131
if !ri.ShellForm && !ri.IsHeredoc {
132-
return []string{reconstructed + escapeSlice(strings.Split(ri.Cmd[0], " "))}
132+
return []string{reconstructed + escapeSlice(ri.Cmd)}
133133
}
134134
if ri.IsHeredoc {
135135
reconstructed += "<< "

pkg/ast/reconstruct_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func TestReconstructInstruction(t *testing.T) {
218218
Input: ast.StageNode{
219219
Instructions: []ast.InstructionNode{
220220
&ast.RunInstructionNode{
221-
Cmd: []string{"curl google.com"},
221+
Cmd: []string{"curl", "google.com"},
222222
ShellForm: false,
223223
},
224224
},

0 commit comments

Comments
 (0)