From 9f722fb8601b1f5e6d4b034d30eb328e1bb9c526 Mon Sep 17 00:00:00 2001 From: Otto Ahoniemi Date: Mon, 28 Nov 2022 03:04:33 +0200 Subject: Try to get sourcery auth token from config file (#2240) * feat: try to get sourcery token from config file * fix: support windows * fix: only check for token if it's not in options --- lua/lspconfig/server_configurations/sourcery.lua | 28 ++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'lua') diff --git a/lua/lspconfig/server_configurations/sourcery.lua b/lua/lspconfig/server_configurations/sourcery.lua index 608a816f..bde18a15 100644 --- a/lua/lspconfig/server_configurations/sourcery.lua +++ b/lua/lspconfig/server_configurations/sourcery.lua @@ -9,6 +9,25 @@ local root_files = { 'pyrightconfig.json', } +local token_in_auth_file = function() + local is_windows = vim.fn.has 'win32' == 1 + local path_sep = is_windows and '\\' or '/' + + local config_home = is_windows and vim.fn.getenv 'APPDATA' or vim.fn.expand '~/.config' + local auth_file_path = config_home .. path_sep .. 'sourcery' .. path_sep .. 'auth.yaml' + + if vim.fn.filereadable(auth_file_path) == 1 then + local content = vim.fn.readfile(auth_file_path) + for _, line in ipairs(content) do + if line:match 'sourcery_token: (.+)' then + return true + end + end + end + + return false +end + return { default_config = { cmd = { 'sourcery', 'lsp' }, @@ -24,9 +43,12 @@ return { single_file_support = true, }, on_new_config = function(new_config, _) - if not new_config.init_options.token then + if not new_config.init_options.token and not token_in_auth_file() then local notify = vim.notify_once or vim.notify - notify('[lspconfig] The authentication token must be provided in config.init_options', vim.log.levels.ERROR) + notify( + '[lspconfig] The authentication token must be provided in config.init_options or configured via "sourcery login"', + vim.log.levels.ERROR + ) end end, docs = { @@ -54,6 +76,8 @@ require'lspconfig'.sourcery.setup { }, } ``` + +Alternatively, you can login to sourcery by running `sourcery login` with sourcery-cli. ]], }, } -- cgit v1.2.3-70-g09d2