Skip to content

Commit 56aa8d7

Browse files
committed
feat: make python executable dynamic depending on os
1 parent bce9c34 commit 56aa8d7

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

cmd/format.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"log/slog"
99
"path/filepath"
10+
"runtime"
1011
"sync"
1112

1213
"github.com/fatih/color"
@@ -64,6 +65,13 @@ var formatCmd = &cobra.Command{
6465
slog.Bool("isFile", !fileInfo.IsDir()),
6566
)
6667

68+
// doing this as there seems to be a hassle using python as executable in $PATH in macos
69+
pythonExecutable := "python3"
70+
if runtime.GOOS == "windows" {
71+
pythonExecutable = "python"
72+
} else {
73+
pythonExecutable = "python3"
74+
}
6775

6876
if fileInfo.IsDir() {
6977
fmt.Println("\nDirectory to format: ", green(fileOrDirPath))
@@ -83,7 +91,7 @@ var formatCmd = &cobra.Command{
8391
wg.Add(1)
8492
go func(i int) {
8593
defer wg.Done()
86-
formatSqlxFile((*sqlxFiles)[i], inplace, sqlfluffConfigPath, logger)
94+
formatSqlxFile((*sqlxFiles)[i], inplace, sqlfluffConfigPath, pythonExecutable, logger)
8795
}(i)
8896
}
8997
wg.Wait()
@@ -94,7 +102,7 @@ var formatCmd = &cobra.Command{
94102
fmt.Printf(red("Only .sqlx files are supported for formatting \n"))
95103
return
96104
}
97-
formatSqlxFile(fileOrDirPath, inplace, sqlfluffConfigPath, logger)
105+
formatSqlxFile(fileOrDirPath, inplace, sqlfluffConfigPath, pythonExecutable, logger)
98106
} else {
99107
cmd.Help()
100108
}

cmd/utils.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ func findSqlxFiles(dataformRootDirectory string) *[]string {
4848
return nil
4949
}
5050

51-
func formatSqlCode(sqlxFileMetaData *sqlxFileMetaData, pythonScriptPath string, sqlfluffConfigPath string, logger *slog.Logger) error {
51+
func formatSqlCode(sqlxFileMetaData *sqlxFileMetaData, pythonScriptPath string, sqlfluffConfigPath string, pythonExecutable string, logger *slog.Logger) error {
5252
queryString := *&sqlxFileMetaData.queryString
5353

54-
cmd := exec.Command("python", pythonScriptPath, string(sqlfluffConfigPath), string(queryString))
54+
cmd := exec.Command(pythonExecutable, pythonScriptPath, string(sqlfluffConfigPath), string(queryString))
5555

5656
var stdout bytes.Buffer
5757
var stderr bytes.Buffer
@@ -133,14 +133,14 @@ func writeContentsToFileInPlace(sqlxFileMetaData *sqlxFileMetaData, formattingEr
133133
}
134134
}
135135

136-
func formatSqlxFile(sqlxFilePath string, inplace bool, sqlfluffConfigPath string, logger *slog.Logger) {
136+
func formatSqlxFile(sqlxFilePath string, inplace bool, sqlfluffConfigPath string, pythonExecutable string, logger *slog.Logger) {
137137
sqlxFileMetaData, err := getSqlxFileMetaData(sqlxFilePath)
138138

139139
if err != nil {
140140
fmt.Println("Error finding config blocks:", err)
141141
} else {
142142
pythonScriptPath := filepath.Join(".formatdataform", "sqlfluff_formatter.py")
143-
formattingError := formatSqlCode(&sqlxFileMetaData, pythonScriptPath, sqlfluffConfigPath, logger)
143+
formattingError := formatSqlCode(&sqlxFileMetaData, pythonScriptPath, sqlfluffConfigPath, pythonExecutable, logger)
144144
if inplace {
145145
writeContentsToFileInPlace(&sqlxFileMetaData, formattingError)
146146
} else {

0 commit comments

Comments
 (0)