Skip to content

Commit

Permalink
feat(cli)!: Refactor to use Cobra
Browse files Browse the repository at this point in the history
This PR doesn't functionally change anything about the internal workings of the app, but greatly alters the structure of the app. It now uses Cobra for a more proper CLI-like experience (version, help, etc work). This is laying the ground work for saving configs for easier reuse of previous runs.
  • Loading branch information
gwenwindflower committed Apr 23, 2024
1 parent 9112c63 commit c009a81
Show file tree
Hide file tree
Showing 37 changed files with 191 additions and 128 deletions.
124 changes: 124 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package cmd

import (
"context"
"fmt"
"log"
"os"
"time"

"github.com/fatih/color"
"github.com/gwenwindflower/tbd/internal"
"github.com/gwenwindflower/tbd/sourcerer"
"github.com/spf13/cobra"
)

type Elapsed struct {
DbStart time.Time
ProcessingStart time.Time
DbElapsed float64
ProcessingElapsed float64
}

var (
greenBold = color.New(color.FgMagenta).Add(color.Bold).SprintFunc()
rootCmd = &cobra.Command{
Use: "tbd",
Short: "🏁 A sweet and speedy code generator for dbt projects. 🏎️✨",
Long: fmt.Sprintf(`🏁 %s 🏎️✨
tbd uses your database schema to generate YAML configs and
SQL staging models, including tests and docs via LLM.
It's the easy button for starting a dbt project.`, greenBold("A sweet and speedy code generator for dbt projects.")),
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

ps, err := internal.FetchDbtProfiles()
if err != nil {
log.Fatalf("Error fetching dbt profiles: %v\n", err)
}
fr, err := internal.Forms(ps)
if err != nil {
log.Fatalf("Error running form: %v\n", err)
}
if !fr.Confirm {
log.Fatal("β›” User cancelled.")
}
cd := internal.SetConnectionDetails(fr, ps)

e := Elapsed{}
e.DbStart = time.Now()

bd := fr.BuildDir
err = internal.PrepBuildDir(bd)
if err != nil {
log.Fatalf("Error preparing build directory: %v\n", err)
}
dbc, err := sourcerer.GetConn(cd)
if err != nil {
log.Fatalf("Error getting database connection: %v\n", err)
}
err = dbc.ConnectToDb(ctx)
if err != nil {
log.Fatalf("Error connecting to database: %v\n", err)
}
fmt.Println("Connected to database")
ts, err := dbc.GetSourceTables(ctx)
if err != nil {
log.Fatalf("Error getting sources: %v\n", err)
}
fmt.Println("Got source tables")
fmt.Println("Putting columns on tables...")
err = sourcerer.PutColumnsOnTables(ctx, ts, dbc)
if err != nil {
log.Fatalf("Error putting columns on tables: %v\n", err)
}

e.DbElapsed = time.Since(e.DbStart).Seconds()
// End of database interaction, start of processing
e.ProcessingStart = time.Now()

if fr.GenerateDescriptions {
llm, err := internal.GetLlm(fr)
if err != nil {
// Using Printf instead of log.Fatalf since the program doesn't
// need to totally fail if the API provider can't be fetched
fmt.Printf("Error getting API provider: %v\n", err)
}
fmt.Println("Generating descriptions and tests...")
internal.InferColumnFields(llm, ts)
if err != nil {
// Using Printf instead of log.Fatalf since the program
// doesn't need to totally fail if there's an error in the column field inference
fmt.Printf("Error inferring column fields: %v\n", err)
}
}
fmt.Println("Writing files...")
if fr.CreateProfile {
internal.WriteProfile(cd, bd)
}
if fr.ScaffoldProject {
s, err := internal.WriteScaffoldProject(cd, bd, fr.ProjectName)
if err != nil {
log.Fatalf("Error scaffolding project: %v\n", err)
}
bd = s
}
err = internal.WriteFiles(ts, bd, fr.Prefix)
if err != nil {
log.Fatalf("Error writing files: %v\n", err)
}
e.ProcessingElapsed = time.Since(e.ProcessingStart).Seconds()
pinkUnderline := color.New(color.FgMagenta).Add(color.Bold, color.Underline).SprintFunc()
fmt.Printf("\n🏁 Done in %.1fs fetching data and %.1fs writing files!\nYour YAML and SQL files are in the %s directory.", e.DbElapsed, e.ProcessingElapsed, pinkUnderline(fr.BuildDir))
},
}
)

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
21 changes: 21 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"fmt"

"github.com/gwenwindflower/tbd/internal"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(versionCmd)
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of tbd",
Long: `Wanna know what version of tbd you're running? Well I've got some great news for you.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("tbd v%s\n", internal.VERSION)
},
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/marcboeker/go-duckdb v1.6.3
github.com/schollz/progressbar/v3 v3.14.2
github.com/snowflakedb/gosnowflake v1.9.0
github.com/spf13/cobra v1.8.0
google.golang.org/api v0.170.0
gopkg.in/yaml.v2 v2.4.0
)
Expand Down Expand Up @@ -80,6 +81,7 @@ require (
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hashicorp/go-retryablehttp v0.7.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/klauspost/asmfmt v1.3.2 // indirect
github.com/klauspost/compress v1.17.7 // indirect
Expand All @@ -105,6 +107,7 @@ require (
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/rs/zerolog v1.28.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:Yyn
github.com/coreos/go-oidc/v3 v3.5.0 h1:VxKtbccHZxs8juq7RdJntSqtXFtde9YpNpGn0yqgEHw=
github.com/coreos/go-oidc/v3 v3.5.0/go.mod h1:ecXRtV4romGPeO6ieExAsUK9cb/3fp9hXNz1tlv8PIM=
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/danieljoos/wincred v1.1.2 h1:QLdCxFs1/Yl4zduvBdcHB8goaYk9RARS2SgLLRuAyr0=
github.com/danieljoos/wincred v1.1.2/go.mod h1:GijpziifJoIBfYh+S7BbkdUTU4LfM+QnGqR5Vl2tAx0=
github.com/databricks/databricks-sql-go v1.5.4 h1:N6dFuWsouupQdN3W5tHQj0vvWfAfA0YBiblMq4h/qqU=
Expand Down Expand Up @@ -205,6 +206,8 @@ github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxC
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-retryablehttp v0.7.1 h1:sUiuQAnLlbvmExtFQs72iFW/HXeUn8Z1aJLQ4LJJbTQ=
github.com/hashicorp/go-retryablehttp v0.7.1/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
Expand Down Expand Up @@ -287,13 +290,18 @@ github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUz
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY=
github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/schollz/progressbar/v3 v3.14.2 h1:EducH6uNLIWsr560zSV1KrTeUb/wZGAHqyMFIEa99ks=
github.com/schollz/progressbar/v3 v3.14.2/go.mod h1:aQAZQnhF4JGFtRJiw/eobaXpsqpVQAftEQ+hLGXaRc4=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/snowflakedb/gosnowflake v1.9.0 h1:s2ZdwFxFfpqwa5CqlhnzRESnLmwU3fED6zyNOJHFBQA=
github.com/snowflakedb/gosnowflake v1.9.0/go.mod h1:4ZgHxVf2OKwecx07WjfyAMr0gn8Qj4yvwAo68Og8wsU=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"log"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"os"
Expand Down
4 changes: 2 additions & 2 deletions forms.go β†’ internal/forms.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"errors"
Expand Down Expand Up @@ -84,7 +84,7 @@ To prepare, make sure you have the following:
_See_ %s _for warehouse-specific requirements_:
https://github.com/gwenwindflower/tbd
`, greenBold(Version), pinkUnderline("existing dbt profile"), pinkUnderline("connection details"), greenBold("README"))),
`, greenBold(VERSION), pinkUnderline("existing dbt profile"), pinkUnderline("connection details"), greenBold("README"))),
),

huh.NewGroup(
Expand Down
2 changes: 1 addition & 1 deletion get_dbt_profile.go β†’ internal/get_dbt_profile.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"os"
Expand Down
2 changes: 1 addition & 1 deletion llm_get_llm.go β†’ internal/llm_get_llm.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import "time"

Expand Down
2 changes: 1 addition & 1 deletion llm_get_response.go β†’ internal/llm_get_response.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion llm_prompts.go β†’ internal/llm_prompts.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

const (
DESC_PROMPT = `Generate a description for a column in a specific table in a data warehouse,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion llm_set_tests.go β†’ internal/llm_set_tests.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion prep_build_dir.go β†’ internal/prep_build_dir.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"os"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"log"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"os"
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion test_helpers.go β†’ internal/test_helpers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"os"
Expand Down
3 changes: 3 additions & 0 deletions internal/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package internal

const VERSION = "0.0.25"
2 changes: 1 addition & 1 deletion write_files.go β†’ internal/write_files.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion write_files_test.go β†’ internal/write_files_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"strings"
Expand Down
2 changes: 1 addition & 1 deletion write_profile.go β†’ internal/write_profile.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"log"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"os"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"log"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"os"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"embed"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"os"
Expand Down
2 changes: 1 addition & 1 deletion write_yaml.go β†’ internal/write_yaml.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"log"
Expand Down
2 changes: 1 addition & 1 deletion write_yaml_test.go β†’ internal/write_yaml_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"os"
Expand Down
Loading

0 comments on commit c009a81

Please sign in to comment.