@@ -61,70 +61,71 @@ func formatSqlCode(sqlxFileMetaData *sqlxParserMeta, pythonScriptPath string, sq
61
61
err := cmd .Run ()
62
62
if err != nil {
63
63
logger .Error (stderr .String (), slog .String ("file" , sqlxFileMetaData .filepath ), "error" , err .Error ())
64
- sqlxFileMetaData .sqlBlocksMeta .formattedSqlBlockContent = string (queryString )
64
+ sqlxFileMetaData .sqlBlocksMeta .formattedSqlBlockContent = string (queryString )
65
65
return ErrorFormattingSqlxFile
66
66
}
67
67
output := stdout .String ()
68
68
sql_fluff_not_installed := (strings .TrimSpace (output ) == "sqlfluff is not installed" )
69
69
if sql_fluff_not_installed {
70
70
log .Fatal (color .RedString ("sqlfluff not installed. Please install sqlfluff using 'pip install sqlfluff'" ))
71
71
}
72
- sqlxFileMetaData .sqlBlocksMeta .formattedSqlBlockContent = output
72
+ sqlxFileMetaData .sqlBlocksMeta .formattedSqlBlockContent = output
73
73
return nil
74
74
}
75
75
76
76
func finalFormmatedSqlxFileContents (sqlxFileMetaData * sqlxParserMeta ) string {
77
- spaceBetweenBlocks := "\n \n "
78
- spaceBetweenSameOps := "\n "
79
-
80
- formattedQuery := ""
81
- prePostOpBlock := ""
82
-
83
- preOpsBlocks := sqlxFileMetaData .preOpsBlocksMeta
84
- postOpsBlocks := sqlxFileMetaData .postOpsBlocksMeta
85
-
86
- preOpsBlockContent := ""
87
- if len (preOpsBlocks ) > 0 {
88
- for _ , preOpsBlock := range preOpsBlocks {
89
- preOpsBlockContent += preOpsBlock .preOpsBlockContent + spaceBetweenSameOps
90
- }
91
- }
92
-
93
- postOpsBlockContent := ""
94
- if len (postOpsBlocks ) > 0 {
95
- for _ , postOpsBlock := range postOpsBlocks {
96
- postOpsBlockContent += postOpsBlock .postOpsBlockContent + spaceBetweenSameOps
97
- }
98
- }
99
-
100
- if preOpsBlockContent == "" && postOpsBlockContent == "" {
101
- prePostOpBlock = ""
102
- } else {
103
- prePostOpBlock = spaceBetweenBlocks + preOpsBlockContent + spaceBetweenBlocks + postOpsBlockContent
104
- }
105
-
106
- formattedQuery = sqlxFileMetaData .configBlockMeta .configBlockContent +
107
- prePostOpBlock +
108
- spaceBetweenBlocks +
109
- sqlxFileMetaData .sqlBlocksMeta .formattedSqlBlockContent
110
- return formattedQuery
77
+ spaceBetweenBlocks := "\n \n "
78
+ // NOTE: Dataform at the time of writing this does not really have multiple preOpsBlocks or postOpsBlocks in its compiled json although it does not throw a compilation error if you put one
79
+ spaceBetweenSameOps := "\n "
80
+
81
+ formattedQuery := ""
82
+ prePostOpBlock := ""
83
+
84
+ preOpsBlocks := sqlxFileMetaData .preOpsBlocksMeta
85
+ postOpsBlocks := sqlxFileMetaData .postOpsBlocksMeta
86
+
87
+ preOpsBlockContent := ""
88
+ if len (preOpsBlocks ) > 0 {
89
+ for _ , preOpsBlock := range preOpsBlocks {
90
+ preOpsBlockContent += preOpsBlock .preOpsBlockContent + spaceBetweenSameOps
91
+ }
92
+ }
93
+
94
+ postOpsBlockContent := ""
95
+ if len (postOpsBlocks ) > 0 {
96
+ for _ , postOpsBlock := range postOpsBlocks {
97
+ postOpsBlockContent += postOpsBlock .postOpsBlockContent + spaceBetweenSameOps
98
+ }
99
+ }
100
+
101
+ if preOpsBlockContent == "" && postOpsBlockContent == "" {
102
+ prePostOpBlock = ""
103
+ } else {
104
+ prePostOpBlock = spaceBetweenBlocks + preOpsBlockContent + spaceBetweenBlocks + postOpsBlockContent
105
+ }
106
+
107
+ formattedQuery = sqlxFileMetaData .configBlockMeta .configBlockContent +
108
+ prePostOpBlock +
109
+ spaceBetweenBlocks +
110
+ sqlxFileMetaData .sqlBlocksMeta .formattedSqlBlockContent
111
+ return formattedQuery
111
112
}
112
113
113
114
func writeContentsToFile (sqlxFileMetaData * sqlxParserMeta , formattingError error ) {
114
115
115
116
yellow := color .New (color .FgYellow ).SprintFunc ()
116
117
red := color .New (color .FgRed ).SprintFunc ()
117
118
118
- filPathSeparator := string (os .PathSeparator )
119
- _definitions := "definitions" + filPathSeparator
119
+ filPathSeparator := string (os .PathSeparator )
120
+ _definitions := "definitions" + filPathSeparator
120
121
baseFilepath := strings .Split (sqlxFileMetaData .filepath , _definitions )
121
- formattedFilePath := filepath .Join ("formatted" , "definitions" , baseFilepath [1 ])
122
+ formattedFilePath := filepath .Join ("formatted" , "definitions" , baseFilepath [1 ])
122
123
123
124
dirToCreate := formattedFilePath [:strings .LastIndex (formattedFilePath , filPathSeparator )]
124
125
125
126
os .MkdirAll (dirToCreate , 0755 ) // TODO: make this configurable
126
127
127
- formattedQuery := finalFormmatedSqlxFileContents (sqlxFileMetaData )
128
+ formattedQuery := finalFormmatedSqlxFileContents (sqlxFileMetaData )
128
129
129
130
err := os .WriteFile (formattedFilePath , []byte (formattedQuery ), 0664 )
130
131
if err != nil {
@@ -145,7 +146,7 @@ func writeContentsToFileInPlace(sqlxFileMetaData *sqlxParserMeta, formattingErro
145
146
yellow := color .New (color .FgYellow ).SprintFunc ()
146
147
red := color .New (color .FgRed ).SprintFunc ()
147
148
148
- formattedQuery := finalFormmatedSqlxFileContents (sqlxFileMetaData )
149
+ formattedQuery := finalFormmatedSqlxFileContents (sqlxFileMetaData )
149
150
150
151
err := os .WriteFile (sqlxFileMetaData .filepath , []byte (formattedQuery ), 0664 )
151
152
if err != nil {
@@ -162,13 +163,13 @@ func writeContentsToFileInPlace(sqlxFileMetaData *sqlxParserMeta, formattingErro
162
163
}
163
164
164
165
func formatSqlxFile (sqlxFilePath string , inplace bool , sqlfluffConfigPath string , pythonExecutable string , logger * slog.Logger ) {
165
- sqlxFileMetaData , err := sqlxParser (sqlxFilePath )
166
- // fmt.Printf("%+v\n", sqlxFileMetaData)
166
+ sqlxFileMetaData , err := sqlxParser (sqlxFilePath )
167
+ // fmt.Printf("%+v\n", sqlxFileMetaData)
167
168
168
169
if err != nil {
169
170
fmt .Println ("Error finding config blocks:" , err )
170
171
} else {
171
- pythonScriptPath := filepath .Join (".formatdataform" , "sqlfluff_formatter.py" )
172
+ pythonScriptPath := filepath .Join (".formatdataform" , "sqlfluff_formatter.py" )
172
173
formattingError := formatSqlCode (& sqlxFileMetaData , pythonScriptPath , sqlfluffConfigPath , pythonExecutable , logger )
173
174
if inplace {
174
175
writeContentsToFileInPlace (& sqlxFileMetaData , formattingError )
@@ -197,7 +198,7 @@ func createFileFromText(text string, filepath string) error {
197
198
return err
198
199
} else {
199
200
f .WriteString (text )
200
- fmt .Printf ("file created at: `%s` \n " , filepath )
201
+ fmt .Printf ("file created at: `%s` \n " , filepath )
201
202
f .Close ()
202
203
}
203
204
return nil
0 commit comments