Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove default keymaps and make cmd additions optional #36

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,22 @@ use {
-- Path to dbt profiles directory
path_to_dbt_profiles_dir = vim.fn.expand("~/.dbt"),

-- flags to include in dbt command
include_profiles_dir = true, -- --profiles-dir
include_project_dir = true, -- --project-dir
include_log_level = true, -- --log-level=INFO

-- Search for ref/source files in macros and models folders
extended_path_search = true,

-- Prevent modifying sql files in target/(compiled|run) folders
protect_compiled_files = true,

-- additional flags to include at the beginning of the rendered dbt command
pre_cmd_args = {},

-- additional flags to include at the end of the rendered dbt command
post_cmd_args = {},
})

-- Setup key mappings
Expand Down Expand Up @@ -100,8 +111,13 @@ use {
path_to_dbt = "dbt",
path_to_dbt_project = "",
path_to_dbt_profiles_dir = vim.fn.expand("~/.dbt"),
include_profiles_dir = true,
include_project_dir = true,
include_log_level = true,
extended_path_search = true,
protect_compiled_files = true,
pre_cmd_args = {},
post_cmd_args = {},
})
require("telescope").load_extension("dbtpal")
end,
Expand Down Expand Up @@ -199,13 +215,19 @@ require("dbtpal").setup({ ... })

The following options are available:

| Option | Description | Default |
| ------ | ----------- | ------- |
| path_to_dbt | Path to the dbt executable | `dbt` (i.e. dbt in the local path) |
| path_to_dbt_project | Path to the dbt project | `""` (auto-detect) |
| path_to_dbt_profiles_dir | Path to dbt profiles directory | `"~/.dbt"` |
| extended_path_search | Search for ref/source files in macros and models folders | `true` |
| protect_compiled_files | Prevent modifying sql files in target/(compiled\|run) folders | `true` |
| Option | Description | Default |
| ------ | ----------- | ------- |
| path_to_dbt | Path to the dbt executable | `dbt` (i.e. dbt in the local path) |
| path_to_dbt_project | Path to the dbt project | `""` (auto-detect) |
| path_to_dbt_profiles_dir | Path to dbt profiles directory | `"~/.dbt"` |
| extended_path_search | Search for ref/source files in macros and models folders | `true` |
| protect_compiled_files | Prevent modifying sql files in target/(compiled\|run) folders | `true` |
| include_profiles_dir | Include `--profiles-dir` flag in dbt command | `true` |
| include_project_dir | Include `--project-dir` flag in dbt command | `true` |
| include_log_level | Include `--log-level=INFO` flag in dbt command (only for dbt >= 1.5) | `true` |
| pre_cmd_args | Additional flags to include at the beginning of the rendered dbt command | `{}` |
| porst_cmd_args | Additional flags to include at the end of the rendered dbt command | `{}` |



### Misc
Expand Down
23 changes: 14 additions & 9 deletions lua/dbtpal/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,30 @@ M.build_path_args = function(cmd, args)
local dbt_path = config.options.path_to_dbt
local dbt_project = config.options.path_to_dbt_project
local dbt_profile = config.options.path_to_dbt_profiles_dir
local include_profiles_dir = config.options.include_profiles_dir
local include_project_dir = config.options.include_project_dir
local include_log_level = config.options.include_log_level

local cmd_args = {}

-- TODO: make this configurable
local pre_cmd_args = config.options.pre_cmd_args or {}
local post_cmd_args = config.options.post_cmd_args or {}

if get_dbt_version() ~= nil and get_dbt_version() >= 1.5 then
log.debug "dbt version >= 1.5, using --log-level=INFO"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change will break for dbt <1.5

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i thought all conditions of the statement would need to be true for it to be included?

if include_log_level and get_dbt_version() ~= nil and get_dbt_version() >= 1.5 then

if dbt_version < 1.5, wouldn't this not pass and log level not be applied even if include_log_level=true?

vim.list_extend(pre_cmd_args, { "--log-level=INFO" })
end

local post_cmd_args = {}
if dbt_profile ~= "v:null" then
if include_profiles_dir and dbt_profile ~= "v:null" then
table.insert(post_cmd_args, "--profiles-dir")
table.insert(post_cmd_args, dbt_profile)
end

table.insert(post_cmd_args, "--project-dir")
table.insert(post_cmd_args, dbt_project)
if include_project_dir and dbt_project ~= "v:null" then
table.insert(post_cmd_args, "--project-dir")
table.insert(post_cmd_args, dbt_project)
end

if include_log_level and get_dbt_version() ~= nil and get_dbt_version() >= 1.5 then
log.debug "dbt version >= 1.5, using --log-level=INFO"
table.insert(post_cmd_args, "--log-level=INFO")
end

vim.list_extend(cmd_args, pre_cmd_args)
vim.list_extend(cmd_args, { cmd })
Expand Down
9 changes: 6 additions & 3 deletions lua/dbtpal/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ M.defaults = {
path_to_dbt_project = "",
path_to_dbt_profiles_dir = vim.fn.expand "~/.dbt",

include_profiles_dir = true,
include_project_dir = true,
include_log_level = true,

custom_dbt_syntax_enabled = true,
extended_path_search = true,
protect_compiled_files = true,

pre_cmd_args = {
"--printer-width=10",
},
pre_cmd_args = {},
post_cmd_args = {},

env = {
["DBT_LOG_LEVEL"] = "NONE",
Expand Down
4 changes: 0 additions & 4 deletions lua/dbtpal/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ vim.api.nvim_create_user_command("DbtCompile", function() main.compile() end, {

vim.api.nvim_create_user_command("DbtBuild", function() main.build() end, { nargs = 0 })

-- Debug keybindings to run dbt
vim.api.nvim_set_keymap("n", "<leader>dr", ":lua require('dbtpal').run()<CR>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<leader>dt", ":lua require('dbtpal').test()<CR>", { noremap = true, silent = true })

local ok, _ = pcall(require, "telescope")
if ok then M.dbt_picker = require("dbtpal.telescope").dbt_picker end
return M
9 changes: 8 additions & 1 deletion lua/dbtpal/projects.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ M.detect_dbt_project_dir = function(bpath)
end

M._get_package_name = function()
local name = vim.fn.system [[grep -E 'name: \S+' ~/projects/clients/causal/internal_dbt/dbt_project.yml]]
local dbt_project_dir = function()
if config.options.path_to_dbt_project ~= "" then
return config.options.path_to_dbt_project
else
return _find_project_dir()
end
end
local name = vim.fn.system [[grep -E 'name: \S+' ]] + dbt_project_dir()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, found an old bug thats been here for a while

return string.match(name, [[name:%s+['"]([%w_]+)]])
end

Expand Down