feat(deepseek api): add printkey subcommand

This commit is contained in:
2026-06-14 11:18:22 +08:00
parent 727e4977f8
commit a9d3e2b329
+27 -7
View File
@@ -2,6 +2,24 @@ const pj_module_dir = (path self | path dirname)
const pj_cache_dir = ($pj_module_dir | path join '..' 'cache')
const key_file = ($pj_cache_dir | path join 'deepseek_apikey.toml')
# Read the stored API key from cache, printing an error and returning null on failure.
def _pj_deepseek_readkey [] {
if not ($key_file | path exists) {
print $"(char --unicode '274c') No DeepSeek API key found. Use `pj deepseek setkey <api-key>` to set it."
return null
}
let config = (open $key_file)
let api_key = ($config.apikey?.key? | default "")
if ($api_key | is-empty) {
print $"(char --unicode '274c') Invalid API key in config file. Use `pj deepseek setkey <api-key>` to set a new one."
return null
}
$api_key
}
# Save your DeepSeek API key to local cache.
export def "pj deepseek setkey" [api_key: string] {
mkdir $pj_cache_dir
@@ -12,6 +30,13 @@ key = \"($api_key)\"
print $"(char --unicode '2714') DeepSeek API key saved to ($key_file)"
}
# Print the stored DeepSeek API key.
export def "pj deepseek printkey" [] {
let api_key = (_pj_deepseek_readkey)
if ($api_key == null) { return }
print $api_key
}
# Query DeepSeek account balance information.
export def "pj deepseek" [] {
if not ($key_file | path exists) {
@@ -19,13 +44,8 @@ export def "pj deepseek" [] {
return
}
let config = (open $key_file)
let api_key = ($config.apikey?.key? | default "")
if ($api_key | is-empty) {
print $"(char --unicode '274c') Invalid API key in config file. Use `pj deepseek setkey <api-key>` to set a new one."
return
}
let api_key = (_pj_deepseek_readkey)
if ($api_key == null) { return }
let response = (curl -L -X GET 'https://api.deepseek.com/user/balance' -H 'Accept: application/json' -H $"Authorization: Bearer ($api_key)" | complete)