| 6 | import chalk from "chalk"; |
| 7 | |
| 8 | export function registerConfigCommands(program: Command): void { |
| 9 | // Login command |
| 10 | const loginCmd: Command = program |
| 11 | .command("login") |
| 12 | .description("Authenticate with a OneUptime instance") |
| 13 | .argument("<api-key>", "API key for authentication") |
| 14 | .argument( |
| 15 | "<instance-url>", |
| 16 | "OneUptime instance URL (e.g. https://oneuptime.com)", |
| 17 | ) |
| 18 | .option("--context-name <name>", "Name for this context", "default") |
| 19 | .action( |
| 20 | ( |
| 21 | apiKey: string, |
| 22 | instanceUrl: string, |
| 23 | options: { contextName: string }, |
| 24 | ) => { |
| 25 | try { |
| 26 | const context: CLIContext = { |
| 27 | name: options.contextName, |
| 28 | apiUrl: instanceUrl.replace(/\/+$/, ""), |
| 29 | apiKey: apiKey, |
| 30 | }; |
| 31 | |
| 32 | ConfigManager.addContext(context); |
| 33 | ConfigManager.setCurrentContext(context.name); |
| 34 | |
| 35 | printSuccess( |
| 36 | `Logged in successfully. Context "${context.name}" is now active.`, |
| 37 | ); |
| 38 | } catch (error) { |
| 39 | printError( |
| 40 | `Login failed: ${error instanceof Error ? error.message : String(error)}`, |
| 41 | ); |
| 42 | process.exit(1); |
| 43 | } |
| 44 | }, |
| 45 | ); |
| 46 | |
| 47 | // Suppress unused variable warning - loginCmd is used for registration |
| 48 | void loginCmd; |
| 49 | |
| 50 | // Context commands |
| 51 | const contextCmd: Command = program |
| 52 | .command("context") |
| 53 | .description("Manage CLI contexts (environments/projects)"); |
| 54 | |
| 55 | contextCmd |
| 56 | .command("list") |
| 57 | .description("List all configured contexts") |
| 58 | .action(() => { |
| 59 | const contexts: Array<CLIContext & { isCurrent: boolean }> = |
| 60 | ConfigManager.listContexts(); |
| 61 | |
| 62 | if (contexts.length === 0) { |
| 63 | printInfo( |
| 64 | "No contexts configured. Run `oneuptime login` to create one.", |
| 65 | ); |