@@ -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}
0 commit comments