feat(config): merge platform config model and add disabled-platform guard (#336)
## Problem Supplying any `platforms` table silently dropped all unlisted platforms, making it easy to accidentally disable platforms. Invoking a disabled platform produced no user-facing error. ## Solution Switch to a merge model: all six platforms are enabled by default and user entries are deep-merged on top. Set a platform key to `false` to disable it explicitly. Add a `check_platform_enabled` guard in `handle_command` for contest fetch, login, logout, and race actions.
This commit is contained in:
parent
82640709d6
commit
e89c57558d
3 changed files with 74 additions and 29 deletions
|
|
@ -283,7 +283,7 @@ local function parse_command(args)
|
|||
message = 'Too few arguments - specify a contest.',
|
||||
}
|
||||
elseif #args == 2 then
|
||||
if args[2] == 'login' or args[2] == 'logout' then
|
||||
if args[2] == 'login' or args[2] == 'logout' or args[2] == 'signup' then
|
||||
return { type = 'action', action = args[2], requires_context = false, platform = first }
|
||||
end
|
||||
local contest = args[2]
|
||||
|
|
@ -330,6 +330,22 @@ local function parse_command(args)
|
|||
return { type = 'error', message = 'Unknown command or no contest context.' }
|
||||
end
|
||||
|
||||
---@param platform string
|
||||
---@return boolean
|
||||
local function check_platform_enabled(platform)
|
||||
local cfg = require('cp.config').get_config()
|
||||
if not cfg.platforms[platform] then
|
||||
logger.log(
|
||||
("Platform '%s' is not enabled. Add it to vim.g.cp.platforms to enable it."):format(
|
||||
constants.PLATFORM_DISPLAY_NAMES[platform] or platform
|
||||
),
|
||||
{ level = vim.log.levels.ERROR }
|
||||
)
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
--- Core logic for handling `:CP ...` commands
|
||||
---@return nil
|
||||
function M.handle_command(opts)
|
||||
|
|
@ -378,6 +394,9 @@ function M.handle_command(opts)
|
|||
elseif cmd.action == 'submit' then
|
||||
require('cp.submit').submit({ language = cmd.language })
|
||||
elseif cmd.action == 'race' then
|
||||
if not check_platform_enabled(cmd.platform) then
|
||||
return
|
||||
end
|
||||
require('cp.race').start(cmd.platform, cmd.contest, cmd.language)
|
||||
elseif cmd.action == 'race_stop' then
|
||||
require('cp.race').stop()
|
||||
|
|
@ -396,9 +415,25 @@ function M.handle_command(opts)
|
|||
end
|
||||
vim.ui.open(url)
|
||||
elseif cmd.action == 'login' then
|
||||
if not check_platform_enabled(cmd.platform) then
|
||||
return
|
||||
end
|
||||
require('cp.credentials').login(cmd.platform)
|
||||
elseif cmd.action == 'logout' then
|
||||
if not check_platform_enabled(cmd.platform) then
|
||||
return
|
||||
end
|
||||
require('cp.credentials').logout(cmd.platform)
|
||||
elseif cmd.action == 'signup' then
|
||||
local url = constants.SIGNUP_URLS[cmd.platform]
|
||||
if not url then
|
||||
logger.log(
|
||||
("No signup URL available for '%s'"):format(cmd.platform),
|
||||
{ level = vim.log.levels.WARN }
|
||||
)
|
||||
return
|
||||
end
|
||||
vim.ui.open(url)
|
||||
end
|
||||
elseif cmd.type == 'problem_jump' then
|
||||
local platform = state.get_platform()
|
||||
|
|
@ -432,6 +467,9 @@ function M.handle_command(opts)
|
|||
local cache_commands = require('cp.commands.cache')
|
||||
cache_commands.handle_cache_command(cmd)
|
||||
elseif cmd.type == 'contest_setup' then
|
||||
if not check_platform_enabled(cmd.platform) then
|
||||
return
|
||||
end
|
||||
local setup = require('cp.setup')
|
||||
setup.setup_contest(cmd.platform, cmd.contest, nil, cmd.language)
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue