refactor: replace :CP login with :CP credentials subcommand

Problem: :CP login was a poor API — no way to clear credentials without
raw Lua, and the single command didn't scale to multiple operations.

Solution: replace login with a :CP credentials subcommand following the
same pattern as :CP cache. :CP credentials set [platform] prompts and
saves; :CP credentials clear [platform] removes one or all platforms.
Add cache.clear_credentials(), rename login.lua to credentials.lua,
update parse/dispatch/tab-complete, and rewrite vimdoc accordingly.
This commit is contained in:
Barrett Ruth 2026-03-03 16:42:35 -05:00 committed by Barrett Ruth
parent 3e0b7beabf
commit a04702d87c
6 changed files with 88 additions and 32 deletions

View file

@ -388,6 +388,18 @@ function M.set_credentials(platform, creds)
M.save()
end
---@param platform string?
function M.clear_credentials(platform)
if platform then
if cache_data._credentials then
cache_data._credentials[platform] = nil
end
else
cache_data._credentials = nil
end
M.save()
end
function M.clear_all()
local creds = cache_data._credentials
cache_data = {}

View file

@ -83,12 +83,20 @@ local function parse_command(args)
else
return { type = 'action', action = 'interact' }
end
elseif first == 'login' then
return {
type = 'action',
action = 'login',
platform = args[2],
}
elseif first == 'credentials' then
local subcommand = args[2]
if not subcommand then
return { type = 'error', message = 'credentials command requires subcommand (set, clear)' }
end
if vim.tbl_contains({ 'set', 'clear' }, subcommand) then
return {
type = 'credentials',
subcommand = subcommand,
platform = args[3],
}
else
return { type = 'error', message = 'unknown credentials subcommand: ' .. subcommand }
end
elseif first == 'stress' then
return {
type = 'action',
@ -323,8 +331,6 @@ function M.handle_command(opts)
edit.toggle_edit(cmd.test_index)
elseif cmd.action == 'stress' then
require('cp.stress').toggle(cmd.generator_cmd, cmd.brute_cmd)
elseif cmd.action == 'login' then
require('cp.login').login(cmd.platform)
elseif cmd.action == 'submit' then
require('cp.submit').submit({ language = cmd.language })
elseif cmd.action == 'race' then
@ -360,6 +366,13 @@ function M.handle_command(opts)
local setup = require('cp.setup')
setup.setup_contest(platform, contest_id, problem_id, cmd.language)
elseif cmd.type == 'credentials' then
local creds = require('cp.credentials')
if cmd.subcommand == 'set' then
creds.set(cmd.platform)
elseif cmd.subcommand == 'clear' then
creds.clear(cmd.platform)
end
elseif cmd.type == 'cache' then
local cache_commands = require('cp.commands.cache')
cache_commands.handle_cache_command(cmd)

View file

@ -13,7 +13,7 @@ M.ACTIONS = {
'race',
'stress',
'submit',
'login',
'credentials',
}
M.PLATFORM_DISPLAY_NAMES = {

View file

@ -4,29 +4,42 @@ local cache = require('cp.cache')
local logger = require('cp.log')
local state = require('cp.state')
function M.login(platform)
function M.set(platform)
platform = platform or state.get_platform()
if not platform then
logger.log('No platform specified. Usage: :CP login <platform>', vim.log.levels.ERROR)
logger.log(
'No platform specified. Usage: :CP credentials set <platform>',
vim.log.levels.ERROR
)
return
end
vim.ui.input({ prompt = platform .. ' username: ' }, function(username)
if not username or username == '' then
logger.log('Login cancelled', vim.log.levels.WARN)
logger.log('Cancelled', vim.log.levels.WARN)
return
end
vim.fn.inputsave()
local password = vim.fn.inputsecret(platform .. ' password: ')
vim.fn.inputrestore()
if not password or password == '' then
logger.log('Login cancelled', vim.log.levels.WARN)
logger.log('Cancelled', vim.log.levels.WARN)
return
end
cache.load()
cache.set_credentials(platform, { username = username, password = password })
logger.log(platform .. ' credentials saved', vim.log.levels.INFO)
logger.log(platform .. ' credentials saved', vim.log.levels.INFO, true)
end)
end
function M.clear(platform)
cache.load()
cache.clear_credentials(platform)
if platform then
logger.log(platform .. ' credentials cleared', vim.log.levels.INFO, true)
else
logger.log('all credentials cleared', vim.log.levels.INFO, true)
end
end
return M