From 09ff0c3822e3d6c1ddf373c8085dba827a79f041 Mon Sep 17 00:00:00 2001 From: gwen windflower Date: Sat, 9 Mar 2024 16:31:45 -0600 Subject: [PATCH] Finish form and clean build dir func --- .gitignore | 2 +- cleanbuilddir.go | 25 +++++++++++++++++++++++++ go.mod | 5 +++-- go.sum | 6 ++++++ main.go | 9 ++++++++- writestagingmodels.go | 10 +++++++++- 6 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 cleanbuilddir.go diff --git a/.gitignore b/.gitignore index 567609b..a007fea 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -build/ +build/* diff --git a/cleanbuilddir.go b/cleanbuilddir.go new file mode 100644 index 0000000..1eaccf1 --- /dev/null +++ b/cleanbuilddir.go @@ -0,0 +1,25 @@ +package main + +import ( + "log" + "os" + "path/filepath" +) + +func CleanBuildDir(buildDirPath string) { + err := filepath.Walk(buildDirPath, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if !info.IsDir() { + err = os.Remove(path) + if err != nil { + return err + } + } + return nil + }) + if err != nil { + log.Fatalf("Error cleaning build directory: %v", err) + } +} diff --git a/go.mod b/go.mod index 3611e1b..78cd4fd 100644 --- a/go.mod +++ b/go.mod @@ -32,9 +32,10 @@ require ( github.com/aws/smithy-go v1.20.1 // indirect github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/catppuccin/go v0.2.0 // indirect - github.com/charmbracelet/bubbles v0.17.2-0.20240108170749-ec883029c8e6 // indirect + github.com/charmbracelet/bubbles v0.18.0 // indirect github.com/charmbracelet/bubbletea v0.25.0 // indirect github.com/charmbracelet/huh v0.3.0 // indirect + github.com/charmbracelet/huh/spinner v0.0.0-20240306161957-71f31c155b08 // indirect github.com/charmbracelet/lipgloss v0.9.1 // indirect github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect github.com/danieljoos/wincred v1.2.1 // indirect @@ -61,7 +62,7 @@ require ( github.com/muesli/termenv v0.15.2 // indirect github.com/pierrec/lz4/v4 v4.1.21 // indirect github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect - github.com/rivo/uniseg v0.4.4 // indirect + github.com/rivo/uniseg v0.4.6 // indirect github.com/sirupsen/logrus v1.9.3 // indirect github.com/zeebo/xxh3 v1.0.2 // indirect golang.org/x/crypto v0.19.0 // indirect diff --git a/go.sum b/go.sum index fe8371d..bfc7ba9 100644 --- a/go.sum +++ b/go.sum @@ -66,10 +66,14 @@ github.com/catppuccin/go v0.2.0 h1:ktBeIrIP42b/8FGiScP9sgrWOss3lw0Z5SktRoithGA= github.com/catppuccin/go v0.2.0/go.mod h1:8IHJuMGaUUjQM82qBrGNBv7LFq6JI3NnQCF6MOlZjpc= github.com/charmbracelet/bubbles v0.17.2-0.20240108170749-ec883029c8e6 h1:6nVCV8pqGaeyxetur3gpX3AAaiyKgzjIoCPV3NXKZBE= github.com/charmbracelet/bubbles v0.17.2-0.20240108170749-ec883029c8e6/go.mod h1:9HxZWlkCqz2PRwsCbYl7a3KXvGzFaDHpYbSYMJ+nE3o= +github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/39KLfy0= +github.com/charmbracelet/bubbles v0.18.0/go.mod h1:08qhZhtIwzgrtBjAcJnij1t1H0ZRjwHyGsy6AL11PSw= github.com/charmbracelet/bubbletea v0.25.0 h1:bAfwk7jRz7FKFl9RzlIULPkStffg5k6pNt5dywy4TcM= github.com/charmbracelet/bubbletea v0.25.0/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45+QIJbLOBxmVNWNNg= github.com/charmbracelet/huh v0.3.0 h1:CxPplWkgW2yUTDDG0Z4S5HH8SJOosWHd4LxCvi0XsKE= github.com/charmbracelet/huh v0.3.0/go.mod h1:fujUdKX8tC45CCSaRQdw789O6uaCRwx8l2NDyKfC4jA= +github.com/charmbracelet/huh/spinner v0.0.0-20240306161957-71f31c155b08 h1:kO5eMMxyCJ6m7gdpGQ7OomrMdfsKVPgC4aB/focl/HE= +github.com/charmbracelet/huh/spinner v0.0.0-20240306161957-71f31c155b08/go.mod h1:nrBG0YEHaxdbqHXW1xvG1hPqkuac9Eg7RTMvogiXuz0= github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg= github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I= github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY= @@ -159,6 +163,8 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rivo/uniseg v0.4.6 h1:Sovz9sDSwbOz9tgUy8JpT+KgCkPYJEN/oYzlJiYTNLg= +github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= diff --git a/main.go b/main.go index c4ddc68..bb8ed70 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "log" + "strings" "github.com/charmbracelet/huh" _ "github.com/snowflakedb/gosnowflake" @@ -85,6 +86,12 @@ For security, we don't currently support password-based authentication.`), } if confirm { databaseType := warehouse + if warehouse == "snowflake" { + dbAccount = strings.ToUpper(dbAccount) + dbUsername = strings.ToUpper(dbUsername) + dbSchema = strings.ToUpper(dbSchema) + dbDatabase = strings.ToUpper(dbDatabase) + } connStr := fmt.Sprintf("%s@%s/%s/%s?authenticator=externalbrowser", dbUsername, dbAccount, dbDatabase, dbSchema) ctx, db, err := ConnectToDB(connStr, databaseType) if err != nil { @@ -95,8 +102,8 @@ For security, we don't currently support password-based authentication.`), log.Fatal(err) } PutColumnsOnTables(db, ctx, tables) + CleanBuildDir("build") WriteYAML(tables) - WriteStagingModels(tables) } } diff --git a/writestagingmodels.go b/writestagingmodels.go index b475e91..93c41ff 100644 --- a/writestagingmodels.go +++ b/writestagingmodels.go @@ -5,13 +5,20 @@ import ( "log" "os" "strings" + "sync" "text/template" ) func WriteStagingModels(tables SourceTables) { + var wg sync.WaitGroup + + fmt.Println("Writing staging models...") for _, table := range tables.SourceTables { + wg.Add(1) go func(table SourceTable) { - tmpl, err := template.ParseFiles("./template.sql") + defer wg.Done() + + tmpl, err := template.ParseFiles("template.sql") if err != nil { log.Fatal(err) } @@ -29,4 +36,5 @@ func WriteStagingModels(tables SourceTables) { } }(table) } + wg.Wait() }