refactor(credentials): promote login/logout to top-level actions (#292)
## Problem
`:CP credentials login/logout/clear` is verbose and inconsistent with
other
actions that are all top-level (`:CP run`, `:CP submit`, etc.). The
clear-all
subcommand is also unnecessary since re-logging in overwrites existing
credentials.
## Solution
Replace `:CP credentials {login,logout,clear}` with `:CP login
[platform]`
and `:CP logout [platform]`. Remove the clear-all command and the
credentials
subcommand dispatch — login/logout are now regular actions routed
through the
standard action dispatcher.
This commit is contained in:
parent
98ac0aa7a7
commit
49e0ae3885
5 changed files with 28 additions and 73 deletions
|
|
@ -433,30 +433,27 @@ COMMANDS *cp-commands*
|
||||||
Cancel an active race countdown.
|
Cancel an active race countdown.
|
||||||
|
|
||||||
Credential Commands ~
|
Credential Commands ~
|
||||||
:CP credentials login [platform]
|
:CP login [platform]
|
||||||
Set or update stored credentials for a platform.
|
Set or update stored credentials for a platform.
|
||||||
Always prompts for username and password,
|
Prompts for username and password, overwriting
|
||||||
overwriting any previously saved credentials.
|
any previously saved credentials.
|
||||||
If [platform] is omitted, uses the active platform.
|
If [platform] is omitted, uses the active platform.
|
||||||
Examples: >
|
Examples: >
|
||||||
:CP credentials login atcoder
|
:CP login atcoder
|
||||||
:CP credentials login codeforces
|
:CP login codeforces
|
||||||
<
|
<
|
||||||
:CP credentials logout [platform]
|
:CP logout [platform]
|
||||||
Remove stored credentials for a platform.
|
Remove stored credentials for a platform.
|
||||||
If [platform] is omitted, uses the active platform.
|
If [platform] is omitted, uses the active platform.
|
||||||
Examples: >
|
Examples: >
|
||||||
:CP credentials logout atcoder
|
:CP logout atcoder
|
||||||
<
|
|
||||||
:CP credentials clear
|
|
||||||
Remove stored credentials for all platforms.
|
|
||||||
<
|
<
|
||||||
Submit Commands ~
|
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 credentials login). Prompts on first use
|
:CP login). Prompts on first use if no
|
||||||
if no credentials are saved.
|
credentials are saved.
|
||||||
--lang: Submit solution for a specific language.
|
--lang: Submit solution for a specific language.
|
||||||
|
|
||||||
State Restoration ~
|
State Restoration ~
|
||||||
|
|
@ -963,34 +960,30 @@ 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*
|
CREDENTIALS *cp-credentials*
|
||||||
|
|
||||||
Manage stored login credentials for platform submission.
|
Manage stored login credentials for platform submission.
|
||||||
|
|
||||||
Credentials are stored under _credentials in the main cache file
|
Credentials are stored under _credentials in the main cache file
|
||||||
(stdpath('data')/cp-nvim.json). Use :CP cache read to inspect them.
|
(stdpath('data')/cp-nvim.json). Use :CP cache read to inspect them.
|
||||||
|
|
||||||
:CP credentials login [platform]
|
:CP login [platform]
|
||||||
Set or update credentials for a platform. Always prompts for
|
Set or update credentials for a platform. Prompts for username
|
||||||
username and password, overwriting any previously saved values.
|
and password, overwriting any previously saved values.
|
||||||
Omit [platform] to use the currently active platform.
|
Omit [platform] to use the currently active platform.
|
||||||
|
|
||||||
:CP credentials logout [platform]
|
:CP logout [platform]
|
||||||
Remove stored credentials for a platform.
|
Remove stored credentials for a platform.
|
||||||
Omit [platform] to use the currently active platform.
|
Omit [platform] to use the currently active platform.
|
||||||
|
|
||||||
:CP credentials clear
|
|
||||||
Remove stored credentials for 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 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 credentials login). Prompts on first use if no credentials
|
:CP login). Prompts on first use if no credentials are saved.
|
||||||
are saved.
|
|
||||||
--lang: Override the language to submit.
|
--lang: Override the language to submit.
|
||||||
|
|
||||||
Platform support:
|
Platform support:
|
||||||
|
|
|
||||||
|
|
@ -83,28 +83,8 @@ local function parse_command(args)
|
||||||
else
|
else
|
||||||
return { type = 'action', action = 'interact' }
|
return { type = 'action', action = 'interact' }
|
||||||
end
|
end
|
||||||
elseif first == 'credentials' then
|
elseif first == 'login' or first == 'logout' then
|
||||||
local subcommand = args[2]
|
return { type = 'action', action = first, platform = args[2] }
|
||||||
if not subcommand then
|
|
||||||
return {
|
|
||||||
type = 'error',
|
|
||||||
message = 'credentials command requires subcommand (login, logout, clear)',
|
|
||||||
}
|
|
||||||
end
|
|
||||||
if vim.tbl_contains({ 'login', 'logout' }, subcommand) then
|
|
||||||
return {
|
|
||||||
type = 'credentials',
|
|
||||||
subcommand = subcommand,
|
|
||||||
platform = args[3],
|
|
||||||
}
|
|
||||||
elseif subcommand == 'clear' then
|
|
||||||
return {
|
|
||||||
type = 'credentials',
|
|
||||||
subcommand = 'clear',
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return { type = 'error', message = 'unknown credentials subcommand: ' .. subcommand }
|
|
||||||
end
|
|
||||||
elseif first == 'stress' then
|
elseif first == 'stress' then
|
||||||
return {
|
return {
|
||||||
type = 'action',
|
type = 'action',
|
||||||
|
|
@ -345,6 +325,10 @@ function M.handle_command(opts)
|
||||||
require('cp.race').start(cmd.platform, cmd.contest, cmd.language)
|
require('cp.race').start(cmd.platform, cmd.contest, cmd.language)
|
||||||
elseif cmd.action == 'race_stop' then
|
elseif cmd.action == 'race_stop' then
|
||||||
require('cp.race').stop()
|
require('cp.race').stop()
|
||||||
|
elseif cmd.action == 'login' then
|
||||||
|
require('cp.credentials').login(cmd.platform)
|
||||||
|
elseif cmd.action == 'logout' then
|
||||||
|
require('cp.credentials').logout(cmd.platform)
|
||||||
end
|
end
|
||||||
elseif cmd.type == 'problem_jump' then
|
elseif cmd.type == 'problem_jump' then
|
||||||
local platform = state.get_platform()
|
local platform = state.get_platform()
|
||||||
|
|
@ -374,15 +358,6 @@ 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 == 'login' then
|
|
||||||
creds.login(cmd.platform)
|
|
||||||
elseif cmd.subcommand == 'logout' then
|
|
||||||
creds.logout(cmd.platform)
|
|
||||||
elseif cmd.subcommand == 'clear' then
|
|
||||||
creds.clear()
|
|
||||||
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,8 @@ M.ACTIONS = {
|
||||||
'race',
|
'race',
|
||||||
'stress',
|
'stress',
|
||||||
'submit',
|
'submit',
|
||||||
'credentials',
|
'login',
|
||||||
|
'logout',
|
||||||
}
|
}
|
||||||
|
|
||||||
M.PLATFORM_DISPLAY_NAMES = {
|
M.PLATFORM_DISPLAY_NAMES = {
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,7 @@ local state = require('cp.state')
|
||||||
function M.login(platform)
|
function M.login(platform)
|
||||||
platform = platform or state.get_platform()
|
platform = platform or state.get_platform()
|
||||||
if not platform then
|
if not platform then
|
||||||
logger.log(
|
logger.log('No platform specified. Usage: :CP login <platform>', vim.log.levels.ERROR)
|
||||||
'No platform specified. Usage: :CP credentials login <platform>',
|
|
||||||
vim.log.levels.ERROR
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -35,10 +32,7 @@ end
|
||||||
function M.logout(platform)
|
function M.logout(platform)
|
||||||
platform = platform or state.get_platform()
|
platform = platform or state.get_platform()
|
||||||
if not platform then
|
if not platform then
|
||||||
logger.log(
|
logger.log('No platform specified. Usage: :CP logout <platform>', vim.log.levels.ERROR)
|
||||||
'No platform specified. Usage: :CP credentials logout <platform>',
|
|
||||||
vim.log.levels.ERROR
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
cache.load()
|
cache.load()
|
||||||
|
|
@ -46,10 +40,4 @@ function M.logout(platform)
|
||||||
logger.log(platform .. ' credentials cleared', vim.log.levels.INFO, true)
|
logger.log(platform .. ' credentials cleared', vim.log.levels.INFO, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.clear()
|
|
||||||
cache.load()
|
|
||||||
cache.clear_credentials(nil)
|
|
||||||
logger.log('all credentials cleared', vim.log.levels.INFO, true)
|
|
||||||
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] == 'credentials' then
|
elseif args[2] == 'login' or args[2] == 'logout' then
|
||||||
return filter_candidates({ 'login', 'logout', 'clear' })
|
return filter_candidates(platforms)
|
||||||
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,8 +126,6 @@ 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({ 'login', 'logout' }, 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