feat: add interactive CLI token configuration with settings persistence

Implements comprehensive token management with intelligent priority resolution.
Token sources (highest to lowest priority): environment variable, settings file,
interactive prompt. Changes include:

- Updated home directory from ~/.happy to ~/.config/hapi (env var: HAPI_HOME_DIR)
- Added cliApiToken field to Settings interface for persistent storage
- Made cliApiToken mutable in Configuration class (private field with getter/setter)
- Created ui/tokenInit.ts module for token initialization with fallback prompt
- Enhanced auth command: login (interactive input), logout (clear token), status (show source)
- Updated doctor command to correctly identify token source and mask saved token in output
- Token initialization called early in startup flow to ensure availability before API use
This commit is contained in:
weishu
2025-12-16 21:54:55 +08:00
parent 0485065b87
commit c2b886896b
6 changed files with 151 additions and 29 deletions
+65
View File
@@ -0,0 +1,65 @@
/**
* Token initialization module
*
* Handles CLI_API_TOKEN initialization with priority:
* 1. Environment variable (highest - allows temporary override)
* 2. Settings file (~/.config/hapi/settings.json)
* 3. Interactive prompt (only when both above are missing)
*/
import * as readline from 'node:readline/promises'
import { stdin as input, stdout as output } from 'node:process'
import chalk from 'chalk'
import { configuration } from '@/configuration'
import { readSettings, updateSettings } from '@/persistence'
/**
* Initialize CLI API token
* Must be called before any API operations
*/
export async function initializeToken(): Promise<void> {
// 1. Environment variable has highest priority (allows temporary override)
if (configuration.cliApiToken) {
return
}
// 2. Read from settings file
const settings = await readSettings()
if (settings.cliApiToken) {
configuration._setCliApiToken(settings.cliApiToken)
return
}
// 3. Non-TTY environment cannot prompt, fail with clear error
if (!process.stdin.isTTY) {
throw new Error('CLI_API_TOKEN is required. Set it via environment variable or run `hapi auth login`.')
}
// 4. Interactive prompt
const token = await promptForToken()
// 5. Save and update configuration
await updateSettings(current => ({
...current,
cliApiToken: token
}))
configuration._setCliApiToken(token)
}
async function promptForToken(): Promise<string> {
const rl = readline.createInterface({ input, output })
console.log(chalk.yellow('\nNo CLI_API_TOKEN found.'))
console.log(chalk.gray('You can set it via environment variable or enter it now.\n'))
try {
const token = await rl.question(chalk.cyan('Enter CLI_API_TOKEN: '))
if (!token.trim()) {
throw new Error('Token cannot be empty')
}
console.log(chalk.green('\nToken saved to ~/.config/hapi/settings.json'))
return token.trim()
} finally {
rl.close()
}
}