From eace66ca4fa5237e755ac89251c1c9912cbee77e Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Tue, 3 Mar 2026 16:21:21 -0500 Subject: [PATCH] refactor(submit): consolidate credentials into main cache file Problem: credentials were stored in a separate file, cp-nvim-credentials.json, alongside the main cp-nvim.json cache. Two files for one plugin's persistent state was unnecessary. Solution: add get_credentials/set_credentials to cache.lua, storing credentials under _credentials[platform] in the shared cache. Update clear_all() to preserve _credentials across cache wipes. Remove the separate file, load_credentials, and save_credentials from submit.lua. --- lua/cp/cache.lua | 21 +++++++++++++++++++++ lua/cp/submit.lua | 38 +++----------------------------------- 2 files changed, 24 insertions(+), 35 deletions(-) diff --git a/lua/cp/cache.lua b/lua/cp/cache.lua index 61c6a89..60c91de 100644 --- a/lua/cp/cache.lua +++ b/lua/cp/cache.lua @@ -371,8 +371,29 @@ function M.get_contest_start_time(platform, contest_id) return cache_data[platform][contest_id].start_time end +---@param platform string +---@return table? +function M.get_credentials(platform) + if not cache_data._credentials then + return nil + end + return cache_data._credentials[platform] +end + +---@param platform string +---@param creds table +function M.set_credentials(platform, creds) + cache_data._credentials = cache_data._credentials or {} + cache_data._credentials[platform] = creds + M.save() +end + function M.clear_all() + local creds = cache_data._credentials cache_data = {} + if creds then + cache_data._credentials = creds + end M.save() end diff --git a/lua/cp/submit.lua b/lua/cp/submit.lua index 616dbbb..d7f91fa 100644 --- a/lua/cp/submit.lua +++ b/lua/cp/submit.lua @@ -1,43 +1,11 @@ local M = {} +local cache = require('cp.cache') local logger = require('cp.log') local state = require('cp.state') -local credentials_file = vim.fn.stdpath('data') .. '/cp-nvim-credentials.json' - -local function load_credentials(platform) - if vim.fn.filereadable(credentials_file) ~= 1 then - return nil - end - local content = vim.fn.readfile(credentials_file) - if #content == 0 then - return nil - end - local ok, data = pcall(vim.json.decode, table.concat(content, '\n')) - if not ok then - return nil - end - return data[platform] -end - -local function save_credentials(platform, creds) - local data = {} - if vim.fn.filereadable(credentials_file) == 1 then - local content = vim.fn.readfile(credentials_file) - if #content > 0 then - local ok, decoded = pcall(vim.json.decode, table.concat(content, '\n')) - if ok then - data = decoded - end - end - end - data[platform] = creds - vim.fn.mkdir(vim.fn.fnamemodify(credentials_file, ':h'), 'p') - vim.fn.writefile({ vim.json.encode(data) }, credentials_file) -end - local function prompt_credentials(platform, callback) - local saved = load_credentials(platform) + local saved = cache.get_credentials(platform) if saved and saved.username and saved.password then callback(saved) return @@ -55,7 +23,7 @@ local function prompt_credentials(platform, callback) return end local creds = { username = username, password = password } - save_credentials(platform, creds) + cache.set_credentials(platform, creds) callback(creds) end) end