dbt-labs / dbt-completion.bash

Adds autocompletion to the dbt CLI
https://www.getdbt.com/
Apache License 2.0
116 stars 29 forks source link

Add subcommand and parameter completion #21

Open efung opened 9 months ago

efung commented 9 months ago

What

Integrate the output of the Click shell completion script into this one. This gives this script completion for commands and parameters, while preserving the previous functionality.

Resolves #20

How

I ran _DBT_COMPLETE=bash_source dbt and then added it to an else block in the check that looks for the selector flag. In addition, since this introduces a dependency on dbt on this script, I added a check for dbt to bail out early if it's not actually installed or available.

Sample shell transcript

$ dbt <tab>
build          debug          init           run            show           test
clean          deps           list           run-operation  snapshot
compile        docs           parse          seed           source

$ dbt --no-<tab>
--no-cache-selected-only         --no-populate-cache              --no-use-colors
--no-debug                       --no-print                       --no-use-colors-file
--no-fail-fast                   --no-quiet                       --no-use-experimental-parser
--no-log-cache-events            --no-send-anonymous-usage-stats  --no-version-check
--no-partial-parse               --no-static-parser               --no-write-json

$ dbt run --no<tab>
--no-defer          --no-fail-fast      --no-favor-state    --no-version-check

$ dbt run --no-defer -s m<tab>
my_first_dbt_model

Testing

I'm using dbt 1.5.2 on macOS 14.3. The dbt version shouldn't matter, as the completion uses the current dbt.

b-per commented 8 months ago

Hi @efung ! Thanks for this.

I am off for a bit but will have a look in the next couple of weeks

b-per commented 8 months ago

I have done a couple of tests but will need to do more and most likely modify slightly this PR. It might take a few days.

The first issue I see is that dbt can now also mean the dbt Cloud CLI, which is not built on Click, so, in that case, the completion fails. We'll need to have a mechanism to know if the current dbt is dbt-core or the dbt Cloud CLI

I also want to test the performance a bit. The first tests I did on my machine were quite slow (~1 sec to return completion results), whereas the (incomplete) current version returns results immediately.

b-per commented 8 months ago

I just raised an issue in dbt-core about the performance hit of this approach and whether it can be fixed on the Core side