Skip to content
This repository has been archived by the owner on May 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #8 from monzo/quality-of-life-updates
Browse files Browse the repository at this point in the history
A couple of quality of life updates
  • Loading branch information
DomBlack authored Nov 3, 2020
2 parents 8a3daf0 + 36d3e7f commit d21caa5
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 14 deletions.
31 changes: 28 additions & 3 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import (
"github.com/spf13/cobra"
)

var file string

func init() {
completionCmd.Flags().StringVar(&file, "file", "", "file to which output has to be written")
_ = completionCmd.MarkFlagFilename("file")

rootCmd.AddCommand(completionCmd)
}

Expand Down Expand Up @@ -38,14 +43,34 @@ $ ddbt completion zsh > "${fpath[1]}/_ddbt"
# You will need to start a new shell for this setup to take effect.`,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh"},
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.ExactValidArgs(1),
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
cmd.Root().GenBashCompletion(os.Stdout)
if file != "" {
cmd.Root().GenBashCompletionFile(file)
} else {
cmd.Root().GenBashCompletion(os.Stdout)
}
case "zsh":
cmd.Root().GenZshCompletion(os.Stdout)
if file != "" {
cmd.Root().GenZshCompletionFile(file)
} else {
cmd.Root().GenZshCompletion(os.Stdout)
}
case "fish":
if file != "" {
cmd.Root().GenFishCompletionFile(file, true)
} else {
cmd.Root().GenFishCompletion(os.Stdout, true)
}
case "powershell":
if file != "" {
cmd.Root().GenPowerShellCompletionFile(file)
} else {
cmd.Root().GenPowerShellCompletion(os.Stdout)
}
}
},
}
37 changes: 32 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@ package cmd
import (
"fmt"
"os"
"path"
"path/filepath"

"github.com/spf13/cobra"

"ddbt/bigquery"
"ddbt/compiler"
"ddbt/config"
"ddbt/utils"
)

var rootCmd = &cobra.Command{
Use: "ddbt",
Short: "Dom's Data Build tool is very fast version of DBT",
Long: "DDBT is an experimental drop in replacement for DBT which aims to be much faster at building the DAG for projects with large numbers of models",
Use: "ddbt",
Short: "Dom's Data Build tool is very fast version of DBT",
Long: "DDBT is an experimental drop in replacement for DBT which aims to be much faster at building the DAG for projects with large numbers of models",
Version: utils.DdbtVersion,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Do not run init if we're running info commands which aren't actually going to execute operate on a project
if cmd != versionCmd && cmd != completionCmd && cmd.Name() != "help" {
initDDBT()
}
},
}

var (
Expand All @@ -24,8 +34,6 @@ var (
)

func init() {
cobra.OnInitialize(initDDBT)

rootCmd.PersistentFlags().StringVarP(&targetProfile, "target", "t", "", "Which target profile to use")
rootCmd.PersistentFlags().StringVarP(&upstreamProfile, "upstream", "u", "", "Which target profile to use when reading data outside the current DAG")
rootCmd.PersistentFlags().IntVar(&threads, "threads", 0, "How many threads to execute with")
Expand All @@ -39,6 +47,9 @@ func Execute() {
}

func initDDBT() {
// If you happen to be one folder up from the DBT project, we'll cd in there for you to be nice :)
cdIntoDBTFolder()

// Read the project config
cfg, err := config.Read(targetProfile, upstreamProfile, threads, compiler.CompileStringWithCache)
if err != nil {
Expand All @@ -52,3 +63,19 @@ func initDDBT() {
os.Exit(1)
}
}

func cdIntoDBTFolder() {
wd, err := os.Getwd()
if err != nil {
panic(err)
}

if path.Base(wd) != "dbt" {
if stat, err := os.Stat(filepath.Join(wd, "dbt")); !os.IsNotExist(err) && stat.IsDir() {
err = os.Chdir(filepath.Join(wd, "dbt"))
if err != nil {
panic(err)
}
}
}
}
11 changes: 9 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ var ModelFilter string

func init() {
rootCmd.AddCommand(runCmd)

runCmd.Flags().StringVarP(&ModelFilter, "models", "m", "", "Select which model(s) to run")
addModelsFlag(runCmd)
}

var runCmd = &cobra.Command{
Expand All @@ -41,6 +40,14 @@ var runCmd = &cobra.Command{
},
}

func addModelsFlag(cmd *cobra.Command) {
cmd.Flags().StringVarP(&ModelFilter, "models", "m", "", "Select which model(s) to run")
err := cmd.RegisterFlagCompletionFunc("models", completeModelFn)
if err != nil {
panic(err)
}
}

func compileAllModels() (*fs.FileSystem, *compiler.GlobalContext) {
fmt.Printf("ℹ️ Building for %s profile\n", config.GlobalCfg.Target.Name)

Expand Down
2 changes: 1 addition & 1 deletion cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

func init() {
rootCmd.AddCommand(testCmd)
testCmd.Flags().StringVarP(&ModelFilter, "models", "m", "", "Select which model(s) to test")
addModelsFlag(testCmd)
}

var testCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ var versionCmd = &cobra.Command{
Use: "version",
Short: "Prints the version of DDBT",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(utils.DdbtVersion)
fmt.Println("ddbt version", utils.DdbtVersion)
},
}
3 changes: 2 additions & 1 deletion cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ var skipInitialBuild = false

func init() {
rootCmd.AddCommand(watchCmd)
watchCmd.Flags().StringVarP(&ModelFilter, "models", "m", "", "Select which model(s) to watch")
addModelsFlag(watchCmd)

watchCmd.Flags().BoolVarP(&skipInitialBuild, "skip-run", "s", false, "Skip the initial execution of the DAG and go straight into watch mode")
}

Expand Down
2 changes: 1 addition & 1 deletion utils/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package utils

const DdbtVersion = "ddbt v0.1.0 [experimental]"
const DdbtVersion = "0.1.0"

0 comments on commit d21caa5

Please sign in to comment.