Skip to content

Commit

Permalink
[Feature] Greatly improved forms with dbt profile browsing (#8)
Browse files Browse the repository at this point in the history
This PR greatly improves the forms that compose the TUI, based on
feedback from the dbt Labs DX team.

- Now unified into one form with conditional visibility rather than
multiple forms with conditional logic
- This allows shift+tab and tab to back/forward through the entire thing
- Validated input to prevent entering blank values
- Default values where sensible so you can Just Hit Enter
- We fetch all the dbt profiles before the form runs, so you can browse
them rather than trying to enter the name correctly, this will error
early if there are parsing issues with the profiles rather than after
you fill out the form
  • Loading branch information
gwenwindflower authored Apr 20, 2024
2 parents be1b581 + 6972583 commit ccfd7ba
Show file tree
Hide file tree
Showing 14 changed files with 351 additions and 319 deletions.
50 changes: 50 additions & 0 deletions fetch_dbt_profiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"log"
"os"
"path/filepath"

"gopkg.in/yaml.v2"
)

type DbtProfile struct {
Target string `yaml:"target"`
Outputs map[string]struct {
ConnType string `yaml:"type"`
Account string `yaml:"account"`
User string `yaml:"user"`
Role string `yaml:"role"`
Authenticator string `yaml:"authenticator"`
Database string `yaml:"database"`
Schema string `yaml:"schema"`
Project string `yaml:"project"`
Dataset string `yaml:"dataset"`
Path string `yaml:"path"`
Threads int `yaml:"threads"`
} `yaml:"outputs"`
}

type DbtProfiles map[string]DbtProfile

func FetchDbtProfiles() (DbtProfiles, error) {
paths := []string{
filepath.Join(".", "profiles.yml"),
filepath.Join(os.Getenv("HOME"), ".dbt", "profiles.yml"),
}
ps := DbtProfiles{}
for _, path := range paths {
pf := DbtProfiles{}
yf, err := os.ReadFile(path)
if err != nil {
continue
}
if err = yaml.Unmarshal(yf, pf); err != nil {
log.Fatalf("Could not read dbt profile, \nlikely unsupported fields or formatting issues\n please open an issue: %v\n", err)
}
for k, v := range pf {
ps[k] = v
}
}
return ps, nil
}
28 changes: 28 additions & 0 deletions fetch_dbt_profiles_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"os"
"testing"
)

func TestFetchDbtProfiles(t *testing.T) {
CreateTempDbtProfiles(t)
defer os.RemoveAll(os.Getenv("HOME"))
defer os.Unsetenv("HOME")
profiles, err := FetchDbtProfiles()
if err != nil {
t.Fatalf("Error fetching dbt profiles: %v\n", err)
}
if err != nil {
t.Fatalf("Error fetching dbt profiles: %v\n", err)
}
if profiles["elf"].Outputs["dev"].ConnType != "snowflake" {
t.Fatalf("Expected snowflake, got %s\n", profiles["elf"].Outputs["dev"].ConnType)
}
if profiles["human"].Outputs["dev"].ConnType != "bigquery" {
t.Fatalf("Expected bigquery, got %s\n", profiles["human"].Outputs["dev"].ConnType)
}
if profiles["dwarf"].Outputs["dev"].ConnType != "duckdb" {
t.Fatalf("Expected duckdb, got %s\n", profiles["dwarf"].Outputs["dev"].ConnType)
}
}
Loading

0 comments on commit ccfd7ba

Please sign in to comment.