Code Monkey home page Code Monkey logo

dbt-completion.bash's Introduction

dbt-completion.bash

What

This script adds autocompletion to the dbt CLI. Once installed, users can tab-complete model, tag, source, and package selectors to node selection flags like --select and --exclude. Need a refresher on resource selection? Check out the docs.

Example usage (using the redshift package):

$ dbt run --model red<TAB>
redshift.*                                  redshift_admin_queries                      redshift_constraints
redshift.base.*                             redshift_admin_table_stats                  redshift_cost
redshift.introspection.*                    redshift_admin_users_schema_privileges      redshift_sort_dist_keys
redshift.views.*                            redshift_admin_users_table_view_privileges  redshift_tables
redshift_admin_dependencies                 redshift_columns

Installation (bash)

This script can be installed by moving it to your home directory (as a dotfile), then sourcing it in your ~/.bash_profile file.

curl https://raw.githubusercontent.com/fishtown-analytics/dbt-completion.bash/master/dbt-completion.bash > ~/.dbt-completion.bash
echo 'source ~/.dbt-completion.bash' >> ~/.bash_profile

Installation (bash script on zsh)

The z shell requires a bit more work, you'll also want to automatically load compinit and bashcompinit if they are not already loaded.

This makes use of zsh's ability to import bash completion scripts and use them, as long as they're script-compatible.

curl https://raw.githubusercontent.com/fishtown-analytics/dbt-completion.bash/master/dbt-completion.bash > ~/.dbt-completion.bash
echo 'autoload -U +X compinit && compinit' >> ~/.zshrc
echo 'autoload -U +X bashcompinit && bashcompinit' >> ~/.zshrc
echo 'source ~/.dbt-completion.bash' >> ~/.zshrc

Installation (zsh-only script, with more features)

Using oh-my-zsh

If using oh-my-zsh, the autocompletion script can be installed by putting the _dbt file in the folder ~/.oh-my-zsh/completions/ (create the completions folder if it doesn't exist).

Using zinit

Add the following to your zshrc file:

zinit ice as"completion"
zinit snippet https://github.com/dbt-labs/dbt-completion.bash/blob/master/_dbt

Manual installation

If installing manually, instructions from zsh-completions mention that you need to include the directory where _dbt is stored in your $fpath by adding the following to ~/.zshrc

fpath=(path/to/_dbt/parent_directory $fpath)

And you may need to rebuild the zsh completion cache by running this in your terminal:

rm -f ~/.zcompdump; compinit

In many Linux distributions, the zsh package includes /usr/local/share/zsh/site-functions in its fpath, so simply copying _dbt in that directory should work.

Bash Completion

In bash, the : character counts as a word separator, which causes this completion script to incorrectly handle selectors like tag:... and source:.... To make the script work with these characters, install the bash-completion program, then re-source the dbt-completion.bash script.

macOS

$ brew install bash-completion

Notes and caveats

This script uses the manifest to quickly provide a list of existing selectors. As such, a dbt resource must be compiled before it will be available for tab completion. In the future, this script should use dbt directly to parse the project directory and generate possible selectors. Until then, brand new models/sources/tags/packages will not be displayed in the tab complete menu.

By default the location of your manifest.json file is assumed to be at target/manifest.json. To change that you can specify the full path to the location of your manifest.json file in the environment variable DBT_MANIFEST_PATH. For example:

export DBT_MANIFEST_PATH=/tmp/dbt/target/manifest.json

This script was tested on macOS using bash 4.4.23. It's very likely that this script will not work as expected on other operating systems or in other shells. If you're interested in helping make this script work on another platform, please open an issue!

On zsh, when you want to use the project.folder.* notation, you may have to prefix with a quote, as otherwise you'll get autocompleted to something like dbt compile -m project.folder.*, and zsh will try to glob, find no matches, and throw an error.

dbt-completion.bash's People

Contributors

andehen avatar b-per avatar beckjake avatar dave-connors-3 avatar drewbanin avatar joellabes avatar jtcohen6 avatar jurgispods avatar lef-f avatar m-ronchi avatar ns-admetrics avatar sean-rose avatar upretip avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

dbt-completion.bash's Issues

Doesn't work with Warp terminal

Hi,

dbt completion doesn't seem to work with the new Warp terminal. This might have to do with the fact that Warp doesn't use <tab> for auto-completion but the right arrow key instead.

dbt completion works with iTerm on Mac. (It's sourced as part of my ~/.bash_profile and so on.) However, nothing happens when I'm trying to use <tab> or the right arrow key to auto-complete a dbt node name.

Complete prefix in zsh

I have a bunch of models with prefixes stg_ or int_
I would like to be able to tab complete the prefix from just the name after the prefix.
i.e.
If I have 3 models: stg_model_a_v1, stg_model_a_v2 and stg_model_a_v3
I would like this to happen:

$ dbt run -s model_a<TAB>

to become:

$ dbt run -s stg_model_a_v
stg_model_a_v1   stg_model_a_v2   stg_model_a_v3

I know this is possible since this is the default functionality for autocompletion for e.g. ls

I am very new to zsh so contributing myself seems like a long shot.

New completion script for zsh

Hi!

I just came across the bash completion script, and while I am using zsh myself, the current script worked to auto-complete the models, with 2 caveats:

  1. I had to update it to make it work with folders including spaces
  2. Prefixing with '+' and '@' didn't work and was throwing errors

So, after spending a bit of time understanding zsh completion mechanism I have created the following script:
Gist link

--

Installation

  • If you are using Oh My Zsh, just save the _dbt file in ~/.oh-my-zsh/completions (create the folder if it doesn't exist)
  • The script has been tested on macOS and with zsh version 5.8

Pros:

  • Completion for models works with strings in the middle of the model names (e.g. typing invoice will match models customer_invoice and vendor_invoice
  • Completion has been set up for some flags and commands (listing most of the flags available when running dbt run, dbt test and dbt docs as well as the global flags)
  • Support for '+' and '@' has been replicated from the bash completion script

Cons:

  • Currently, when running dbt run or dbt test the models will be suggested even though there is no model selection flag defined or if the model selection flag is not the last one in the list

--

Happy to submit a PR and share the script on this repo if people want to use it and improve it.

Support completion of subcommands and their arguments

After installing this completion script, I realized that it doesn't complete subcommands, e.g. I expect dbt d<TAB> to offer debug deps docs, or, dbt run --s<TAB> to offer --select --selector --state as completions.

Digging around, I learned that the dbt CLI is built using Click and that it supports shell completion.

It would be nice to merge that generated script with this one to get the best of both worlds!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.