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
45
lua/cp/credentials.lua
Normal file
45
lua/cp/credentials.lua
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
local M = {}
|
||||
|
||||
local cache = require('cp.cache')
|
||||
local logger = require('cp.log')
|
||||
local state = require('cp.state')
|
||||
|
||||
function M.set(platform)
|
||||
platform = platform or state.get_platform()
|
||||
if not platform then
|
||||
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('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('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, 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue