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:
parent
3e0b7beabf
commit
a04702d87c
6 changed files with 88 additions and 32 deletions
|
|
@ -432,21 +432,29 @@ COMMANDS *cp-commands*
|
||||||
:CP race stop
|
:CP race stop
|
||||||
Cancel an active race countdown.
|
Cancel an active race countdown.
|
||||||
|
|
||||||
Submit Commands ~
|
Credential Commands ~
|
||||||
:CP login [platform]
|
:CP credentials set [platform]
|
||||||
Set or update stored credentials for a platform.
|
Set or update stored credentials for a platform.
|
||||||
Always prompts for username and password,
|
Always prompts for username and password,
|
||||||
overwriting any previously saved credentials.
|
overwriting any previously saved credentials.
|
||||||
If [platform] is omitted, uses the active platform.
|
If [platform] is omitted, uses the active platform.
|
||||||
Examples: >
|
Examples: >
|
||||||
:CP login atcoder
|
:CP credentials set atcoder
|
||||||
:CP login codeforces
|
:CP credentials set codeforces
|
||||||
<
|
<
|
||||||
|
:CP credentials clear [platform]
|
||||||
|
Remove stored credentials. Without [platform],
|
||||||
|
clears credentials for all platforms.
|
||||||
|
Examples: >
|
||||||
|
:CP credentials clear atcoder
|
||||||
|
:CP credentials clear
|
||||||
|
<
|
||||||
|
Submit Commands ~
|
||||||
:CP submit [--lang {language}]
|
:CP submit [--lang {language}]
|
||||||
Submit the current solution to the online
|
Submit the current solution to the online
|
||||||
judge. Uses stored credentials (set via
|
judge. Uses stored credentials (set via
|
||||||
:CP login). Prompts on first use if no
|
:CP credentials set). Prompts on first use
|
||||||
credentials are saved.
|
if no credentials are saved.
|
||||||
--lang: Submit solution for a specific language.
|
--lang: Submit solution for a specific language.
|
||||||
|
|
||||||
State Restoration ~
|
State Restoration ~
|
||||||
|
|
@ -952,23 +960,31 @@ Count down to a contest's start time and automatically run setup at T=0.
|
||||||
|
|
||||||
Statusline integration: see |cp-race-status|.
|
Statusline integration: see |cp-race-status|.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
CREDENTIALS *cp-credentials*
|
||||||
|
|
||||||
|
Manage stored login credentials for platform submission.
|
||||||
|
|
||||||
|
Credentials are stored under _credentials in the main cache file
|
||||||
|
(stdpath('data')/cp-nvim.json). Use :CP cache read to inspect them.
|
||||||
|
|
||||||
|
:CP credentials set [platform]
|
||||||
|
Set or update credentials for a platform. Always prompts for
|
||||||
|
username and password, overwriting any previously saved values.
|
||||||
|
Omit [platform] to use the currently active platform.
|
||||||
|
|
||||||
|
:CP credentials clear [platform]
|
||||||
|
Remove stored credentials. Without [platform], clears all platforms.
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
SUBMIT *cp-submit*
|
SUBMIT *cp-submit*
|
||||||
|
|
||||||
Submit the current solution to the online judge.
|
Submit the current solution to the online judge.
|
||||||
|
|
||||||
:CP login [platform] *cp-login*
|
|
||||||
Set or update stored credentials for a platform. Always prompts for
|
|
||||||
username and password, overwriting any previously saved credentials.
|
|
||||||
Omit [platform] to use the currently active platform.
|
|
||||||
|
|
||||||
Credentials are stored in the main cache file under _credentials.
|
|
||||||
Use this command before submitting for the first time, or any time
|
|
||||||
you need to update stored credentials.
|
|
||||||
|
|
||||||
:CP submit [--lang {language}]
|
:CP submit [--lang {language}]
|
||||||
Submit the current solution. Uses stored credentials (set via
|
Submit the current solution. Uses stored credentials (set via
|
||||||
:CP login). Prompts on first use if no credentials are saved.
|
:CP credentials set). Prompts on first use if no credentials
|
||||||
|
are saved.
|
||||||
--lang: Override the language to submit.
|
--lang: Override the language to submit.
|
||||||
|
|
||||||
Platform support:
|
Platform support:
|
||||||
|
|
|
||||||
|
|
@ -388,6 +388,18 @@ function M.set_credentials(platform, creds)
|
||||||
M.save()
|
M.save()
|
||||||
end
|
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()
|
function M.clear_all()
|
||||||
local creds = cache_data._credentials
|
local creds = cache_data._credentials
|
||||||
cache_data = {}
|
cache_data = {}
|
||||||
|
|
|
||||||
|
|
@ -83,12 +83,20 @@ local function parse_command(args)
|
||||||
else
|
else
|
||||||
return { type = 'action', action = 'interact' }
|
return { type = 'action', action = 'interact' }
|
||||||
end
|
end
|
||||||
elseif first == 'login' then
|
elseif first == 'credentials' then
|
||||||
return {
|
local subcommand = args[2]
|
||||||
type = 'action',
|
if not subcommand then
|
||||||
action = 'login',
|
return { type = 'error', message = 'credentials command requires subcommand (set, clear)' }
|
||||||
platform = args[2],
|
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
|
elseif first == 'stress' then
|
||||||
return {
|
return {
|
||||||
type = 'action',
|
type = 'action',
|
||||||
|
|
@ -323,8 +331,6 @@ function M.handle_command(opts)
|
||||||
edit.toggle_edit(cmd.test_index)
|
edit.toggle_edit(cmd.test_index)
|
||||||
elseif cmd.action == 'stress' then
|
elseif cmd.action == 'stress' then
|
||||||
require('cp.stress').toggle(cmd.generator_cmd, cmd.brute_cmd)
|
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
|
elseif cmd.action == 'submit' then
|
||||||
require('cp.submit').submit({ language = cmd.language })
|
require('cp.submit').submit({ language = cmd.language })
|
||||||
elseif cmd.action == 'race' then
|
elseif cmd.action == 'race' then
|
||||||
|
|
@ -360,6 +366,13 @@ function M.handle_command(opts)
|
||||||
|
|
||||||
local setup = require('cp.setup')
|
local setup = require('cp.setup')
|
||||||
setup.setup_contest(platform, contest_id, problem_id, cmd.language)
|
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
|
elseif cmd.type == 'cache' then
|
||||||
local cache_commands = require('cp.commands.cache')
|
local cache_commands = require('cp.commands.cache')
|
||||||
cache_commands.handle_cache_command(cmd)
|
cache_commands.handle_cache_command(cmd)
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ M.ACTIONS = {
|
||||||
'race',
|
'race',
|
||||||
'stress',
|
'stress',
|
||||||
'submit',
|
'submit',
|
||||||
'login',
|
'credentials',
|
||||||
}
|
}
|
||||||
|
|
||||||
M.PLATFORM_DISPLAY_NAMES = {
|
M.PLATFORM_DISPLAY_NAMES = {
|
||||||
|
|
|
||||||
|
|
@ -4,29 +4,42 @@ local cache = require('cp.cache')
|
||||||
local logger = require('cp.log')
|
local logger = require('cp.log')
|
||||||
local state = require('cp.state')
|
local state = require('cp.state')
|
||||||
|
|
||||||
function M.login(platform)
|
function M.set(platform)
|
||||||
platform = platform or state.get_platform()
|
platform = platform or state.get_platform()
|
||||||
if not platform then
|
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
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.ui.input({ prompt = platform .. ' username: ' }, function(username)
|
vim.ui.input({ prompt = platform .. ' username: ' }, function(username)
|
||||||
if not username or username == '' then
|
if not username or username == '' then
|
||||||
logger.log('Login cancelled', vim.log.levels.WARN)
|
logger.log('Cancelled', vim.log.levels.WARN)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
vim.fn.inputsave()
|
vim.fn.inputsave()
|
||||||
local password = vim.fn.inputsecret(platform .. ' password: ')
|
local password = vim.fn.inputsecret(platform .. ' password: ')
|
||||||
vim.fn.inputrestore()
|
vim.fn.inputrestore()
|
||||||
if not password or password == '' then
|
if not password or password == '' then
|
||||||
logger.log('Login cancelled', vim.log.levels.WARN)
|
logger.log('Cancelled', vim.log.levels.WARN)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
cache.load()
|
cache.load()
|
||||||
cache.set_credentials(platform, { username = username, password = password })
|
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)
|
||||||
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
|
return M
|
||||||
|
|
@ -103,8 +103,8 @@ end, {
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return filter_candidates(candidates)
|
return filter_candidates(candidates)
|
||||||
elseif args[2] == 'login' then
|
elseif args[2] == 'credentials' then
|
||||||
return filter_candidates(platforms)
|
return filter_candidates({ 'set', 'clear' })
|
||||||
elseif args[2] == 'race' then
|
elseif args[2] == 'race' then
|
||||||
local candidates = { 'stop' }
|
local candidates = { 'stop' }
|
||||||
vim.list_extend(candidates, platforms)
|
vim.list_extend(candidates, platforms)
|
||||||
|
|
@ -126,6 +126,8 @@ end, {
|
||||||
cache.load()
|
cache.load()
|
||||||
local contests = cache.get_cached_contest_ids(args[3])
|
local contests = cache.get_cached_contest_ids(args[3])
|
||||||
return filter_candidates(contests)
|
return filter_candidates(contests)
|
||||||
|
elseif args[2] == 'credentials' and vim.tbl_contains({ 'set', 'clear' }, args[3]) then
|
||||||
|
return filter_candidates(platforms)
|
||||||
elseif args[2] == 'cache' and args[3] == 'clear' then
|
elseif args[2] == 'cache' and args[3] == 'clear' then
|
||||||
local candidates = vim.list_extend({}, platforms)
|
local candidates = vim.list_extend({}, platforms)
|
||||||
table.insert(candidates, '')
|
table.insert(candidates, '')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue