Skip to content

Commit c1e7d11

Browse files
committed
feat: Add default sqlfluff file to setup process
1 parent 01ebf04 commit c1e7d11

File tree

5 files changed

+222
-89
lines changed

5 files changed

+222
-89
lines changed

cmd/format.go

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,33 @@ var formatCmd = &cobra.Command{
2929
yellow := color.New(color.FgYellow).SprintFunc()
3030
red := color.New(color.FgRed).SprintFunc()
3131

32-
3332
inplace, _ := cmd.Flags().GetBool("inplace")
3433
sqlfluffConfigPath := cmd.Flag("sqlfluff_config_path").Value.String()
3534

36-
// If not file or directory path is supplied
35+
// If not file or directory path is supplied
3736
if len(args) == 0 {
38-
log.Fatalf(yellow(`No file or directory path supplied to command: `, red(` formatdataform format <path>
37+
log.Fatalf(yellow(`No file or directory path supplied to command: `, red(` formatdataform format <path>
3938
^^^^^`)))
40-
return
39+
return
4140
}
4241

43-
// If more than one file or directory path is supplied
44-
// TODO: Add support for multiple files or directories
45-
if len(args) > 1 {
46-
color.Set(color.FgYellow)
47-
fmt.Printf("Only supports one file or directory path at a time, you passed %v \n", len(args))
48-
color.Unset()
49-
return
50-
}
42+
// If more than one file or directory path is supplied
43+
// TODO: Add support for multiple files or directories
44+
if len(args) > 1 {
45+
color.Set(color.FgYellow)
46+
fmt.Printf("Only supports one file or directory path at a time, you passed %v \n", len(args))
47+
color.Unset()
48+
return
49+
}
5150

5251
fileOrDirPath := args[0]
5352

54-
fmt.Println("Sqlfluff config path: ", green(sqlfluffConfigPath))
55-
fmt.Println("Inplace: ", green(inplace))
56-
53+
fmt.Println("Sqlfluff config path: ", green(sqlfluffConfigPath))
54+
fmt.Println("Inplace: ", green(inplace))
55+
fmt.Print("\n")
5756

58-
// make sure the .formatdataform directory exists if not create it
59-
os.Mkdir(".formatdataform", 0755)
57+
// make sure the .formatdataform directory exists if not create it
58+
os.Mkdir(".formatdataform", 0755)
6059
logFile, err := os.OpenFile(".formatdataform/formatdataform_logs.json", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
6160
if err != nil {
6261
log.Fatalf("error opening file: %v", err)
@@ -70,23 +69,22 @@ var formatCmd = &cobra.Command{
7069
slog.Bool("inplace", inplace),
7170
)
7271

73-
74-
75-
if sqlfluffConfigPath == ""{
76-
fmt.Printf(red("sqlfluff config file path is required \n"))
77-
return
78-
}
72+
if sqlfluffConfigPath == "" {
73+
sqlfluffConfigPath = ".formatdataform/.sqlfluff"
74+
fmt.Println("No sqlfluff config passed using default ", sqlfluffConfigPath)
75+
}
7976

8077
if fileExists(sqlfluffConfigPath) == false {
81-
fmt.Printf(red("sqlfluff config file does not exist at: %v \n"), sqlfluffConfigPath)
78+
fmt.Printf(red("sqlfluff config file does not exist at: %v \n"), sqlfluffConfigPath)
79+
fmt.Println("Run ", green("`formatdataform setup` "), "to create a default config")
8280
return
8381
}
8482

85-
if fileExists(".formatdataform/sqlfluff_formatter.py") == false {
86-
fmt.Print(yellow("sqlfluff_formatter.py file does not exist at: ", ".formatdataform/sqlfluff_formatter.py. Run: "))
87-
fmt.Printf("formatdataform setup \n")
88-
return
89-
}
83+
if fileExists(".formatdataform/sqlfluff_formatter.py") == false {
84+
fmt.Print(yellow("sqlfluff_formatter.py file does not exist at: ", ".formatdataform/sqlfluff_formatter.py. Run: "))
85+
fmt.Printf("formatdataform setup \n")
86+
return
87+
}
9088

9189
fileInfo, err := os.Stat(fileOrDirPath)
9290
if err != nil {
@@ -116,10 +114,10 @@ var formatCmd = &cobra.Command{
116114

117115
} else if !fileInfo.IsDir() {
118116
fmt.Println("File to format: ", green(fileOrDirPath))
119-
if filepath.Ext(fileOrDirPath) != ".sqlx" {
120-
fmt.Printf(red("Only .sqlx files are supported for formatting \n"))
121-
return
122-
}
117+
if filepath.Ext(fileOrDirPath) != ".sqlx" {
118+
fmt.Printf(red("Only .sqlx files are supported for formatting \n"))
119+
return
120+
}
123121
formatSqlxFile(fileOrDirPath, inplace, sqlfluffConfigPath, logger)
124122
} else {
125123
cmd.Help()

cmd/python_script.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cmd
2+
3+
4+
var pythonCode = `
5+
import sys
6+
sqlfluff_config_path = sys.argv[1]
7+
my_bad_query = sys.argv[2]
8+
9+
def fix_query(sqlfluff_config_path, my_bad_query):
10+
try:
11+
import sqlfluff
12+
except ImportError:
13+
return "sqlfluff is not installed"
14+
my_good_query = sqlfluff.fix(
15+
my_bad_query,
16+
dialect="bigquery",
17+
config_path=str(sqlfluff_config_path),
18+
)
19+
return my_good_query
20+
21+
print(fix_query(sqlfluff_config_path, my_bad_query)) # so that GoLang can read stdout
22+
`

cmd/setup.go

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package cmd
55

66
import (
77
"fmt"
8+
"log"
89
"os"
910

1011
"github.com/fatih/color"
@@ -15,48 +16,30 @@ import (
1516
var setupCmd = &cobra.Command{
1617
Use: "setup",
1718
Short: "Creates a supporting files need for formatting",
18-
Long: ``,
19+
Long: ``,
1920
Run: func(cmd *cobra.Command, args []string) {
2021

21-
yellow := color.New(color.FgYellow).SprintFunc()
22+
yellow := color.New(color.FgYellow).SprintFunc()
2223

23-
// create a directory .formatdataform
24-
fmt.Println("Creating", yellow(".formatdataform "), "directory at the root of your project")
24+
fmt.Println("Creating `.formatdataform` directory at the root of your project")
2525
os.Mkdir(".formatdataform", 0755)
26-
//add the following lines of python code to the file .formatdataform/sqlfluff_formatter.py
27-
// add the following lines of python code to the file .formatdataform/sqlfluff_formatter.py
28-
f, err := os.Create(".formatdataform/sqlfluff_formatter.py")
2926

30-
code := `
31-
import sys
32-
sqlfluff_config_path = sys.argv[1]
33-
my_bad_query = sys.argv[2]
34-
35-
def fix_query(sqlfluff_config_path, my_bad_query):
36-
try:
37-
import sqlfluff
38-
except ImportError:
39-
return "sqlfluff is not installed"
40-
my_good_query = sqlfluff.fix(
41-
my_bad_query,
42-
dialect="bigquery",
43-
config_path=str(sqlfluff_config_path),
44-
)
45-
return my_good_query
46-
47-
print(fix_query(sqlfluff_config_path, my_bad_query)) # so that GoLang can read stdout
48-
`
27+
err := createFileFromText(pythonCode, ".formatdataform/sqlfluff_formatter.py")
28+
if err != nil {
29+
log.Println("Setup failed!!!")
30+
log.Fatalf(err.Error())
31+
return
32+
}
4933

34+
err = createFileFromText(sqlfluffConfig, ".formatdataform/.sqlfluff")
5035
if err != nil {
51-
fmt.Println(err)
36+
log.Println("Setup failed!!!")
37+
log.Fatalf(err.Error())
5238
return
53-
} else {
54-
f.WriteString(code)
55-
fmt.Println("sqlfluff_formatter.py file created at:", yellow(".formatdataform/sqlfluff_formatter.py"))
56-
f.Close()
57-
fmt.Println("Setup complete")
58-
fmt.Println("Now you can run: ", yellow("formatdataform format <path>"))
5939
}
40+
41+
fmt.Println("Setup complete")
42+
fmt.Println("Now you can run: ", yellow("`formatdataform format <path>`"))
6043
},
6144
}
6245

cmd/sqlfluff_config.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
2+
package cmd
3+
4+
var sqlfluffConfig = `
5+
[sqlfluff]
6+
templater = placeholder
7+
dialect = bigquery
8+
output_line_length = 120
9+
10+
# EXCLUDED RULES
11+
# ==============
12+
# AL07 - Avoid table aliases in from clauses and join conditions.
13+
# ST06 - Select wildcards then simple targets before calculations and aggregates.
14+
# ST07 - Prefer specifying join keys instead of using USING.
15+
# AM03 - Ambiguous ordering directions for columns in order by clause.
16+
# ST04 - Dont mess up nested CASE statement
17+
# LT05 - Line is too long
18+
# AL05 - Tables should not be aliased if that alias is not used.
19+
exclude_rules = AL07, ST06, ST07, AM03, ST04, LT05, AL05
20+
21+
[sqlfluff:rules]
22+
allow_scalar = False
23+
24+
[sqlfluff:indentation]
25+
tab_space_size = 2
26+
indented_joins = False
27+
indented_using_on = True
28+
indented_then = False
29+
indented_on_contents = False
30+
allow_implicit_indents = True
31+
template_blocks_indent = True
32+
33+
[sqlfluff:layout:type:where_clause]
34+
line_position = alone:strict
35+
36+
[sqlfluff:layout:type:binary_operator]
37+
line_position = leading
38+
39+
[sqlfluff:layout:type:comparison_operator]
40+
line_position = trailing
41+
42+
[sqlfluff:layout:type:alias_expression]
43+
# We want non-default spacing _before_ the alias expressions.
44+
spacing_before = align
45+
align_within = select_clause
46+
align_scope = bracketed
47+
48+
[sqlfluff:rules:capitalisation.keywords]
49+
# Keywords must be capitalised
50+
capitalisation_policy = upper
51+
52+
[sqlfluff:rules:capitalisation.literals]
53+
# Null & Boolean Literals eg: NULL, TRUE, FALSE
54+
capitalisation_policy = upper
55+
56+
[sqlfluff:rules:capitalisation.types]
57+
# Data Types eg: INT, STR
58+
extended_capitalisation_policy = upper
59+
60+
[sqlfluff:rules:capitalisation.identifiers]
61+
# Unquoted identifiers
62+
extended_capitalisation_policy = upper
63+
unquoted_identifiers_policy=all
64+
65+
[sqlfluff:rules:capitalisation.functions]
66+
# Function names
67+
capitalisation_policy = upper
68+
extended_capitalisation_policy = upper
69+
70+
[sqlfluff:rules:ambiguous.join]
71+
# Fully qualify JOIN clause
72+
fully_qualify_join_types = inner
73+
74+
[sqlfluff:rules:aliasing.length]
75+
# Minimum string length when creating an alias
76+
min_alias_length = 3
77+
78+
[sqlfluff:rules:aliasing.table]
79+
# Aliasing preference for tables, ie needs an AS
80+
aliasing = explicit
81+
82+
[sqlfluff:rules:aliasing.column]
83+
# Aliasing preference for columns, ie needs an AS
84+
aliasing = explicit
85+
86+
[sqlfluff:rules:layout.commas]
87+
# Leading or trailing commas
88+
line_position = leading
89+
90+
[sqlfluff:layout:type:comma]
91+
line_position = leading
92+
93+
94+
[sqlfluff:rules:convention.select_trailing_comma]
95+
# No trailing comma at end of SELECT, ie before FROM (after last column name)
96+
select_clause_trailing_comma = forbid
97+
98+
[sqlfluff:rules:ambiguous.column_references]
99+
# GROUP BY/ORDER BY column references (i.e. implicit by position or explicit by name)
100+
group_by_and_order_by_style = explicit
101+
102+
[sqlfluff:rules:references.special_chars]
103+
# Special characters in identifiers
104+
unquoted_identifiers_policy = all
105+
quoted_identifiers_policy = all
106+
allow_space_in_identifier = False
107+
additional_allowed_characters = ["", $]
108+
109+
[sqlfluff:rules:references.keywords]
110+
# Keywords should not be used as identifiers.
111+
unquoted_identifiers_policy = all
112+
quoted_identifiers_policy = none
113+
114+
115+
116+
[sqlfluff:templater:placeholder]
117+
param_regex = \${ref\(\"\d*(?P<param_name>[\w]+)"\)\}|\${(?P<param_name>[^\s]+)}
118+
119+
`

0 commit comments

Comments
 (0)