diff --git a/ocm-cli/README.md b/ocm-cli/README.md index e02b1894..748af819 100644 --- a/ocm-cli/README.md +++ b/ocm-cli/README.md @@ -55,9 +55,13 @@ directory entries and skips file entries. ## Login ```bash -ocm login [token] +ocm login [token] [--name ] ``` +Pass `--name` to label the manager; the attached TUI shows that name instead of +the manager host. A name is dropped when you log in to a different manager URL +without `--name`. + The token is stored in a platform-specific token store: | Platform | Store | @@ -86,7 +90,7 @@ If `[token]` is omitted, `ocm login` reads it from hidden TTY input (requires ```bash ocm -ocm login [token] +ocm login [token] [--name ] ocm status ocm list ocm use diff --git a/ocm-cli/bin/ocm.ts b/ocm-cli/bin/ocm.ts index cdcc9029..49cb7f4e 100644 --- a/ocm-cli/bin/ocm.ts +++ b/ocm-cli/bin/ocm.ts @@ -24,7 +24,8 @@ Usage: project id; inside a git repo with no match, launch local opencode; outside a git repo, use the last selected repo, else launch local opencode - ocm login [token] Save manager URL + token (token via stdin if omitted) + ocm login [token] [--name ] + Save manager URL + token (token via stdin if omitted) ocm logout Forget saved token and state ocm status Show current manager URL, repo, and whether token is set ocm list List ready repos from the manager @@ -135,11 +136,14 @@ function findRepo(repos: ManagerRepo[], needle: string | number): ManagerRepo | } export async function cmdLogin(args: string[]): Promise { - const url = args[0] - if (!url) die('usage: ocm login [token]') + const nameFlag = args.indexOf('--name') + const managerName = nameFlag >= 0 ? args[nameFlag + 1] : undefined + const positional = nameFlag >= 0 ? [...args.slice(0, nameFlag), ...args.slice(nameFlag + 2)] : args + const url = positional[0] + if (!url || (nameFlag >= 0 && !managerName)) die('usage: ocm login [token] [--name ]') const normalisedUrl = normalizeManagerUrl(url) - let token = args[1] + let token = positional[1] if (!token) { if (process.stdin.isTTY) { process.stderr.write('Paste token (input hidden): ') @@ -167,9 +171,11 @@ export async function cmdLogin(args: string[]): Promise { } const existing = readState() + const sameManager = existing ? normalizeManagerUrl(existing.managerUrl) === normalisedUrl : false writeState({ ...existing, managerUrl: normalisedUrl, + managerName: managerName ?? (sameManager ? existing?.managerName : undefined), }) const store = describeTokenWriteTarget() @@ -229,6 +235,7 @@ export async function cmdStatus(): Promise { } info(`version: ${VERSION}`) info(`manager url: ${state.managerUrl}`) + if (state.managerName) info(`manager name: ${state.managerName}`) info(`token: ${await describeTokenPresence(state.managerUrl)}`) info(`token store: ${store.kind} (${store.location})`) if (state.lastRepoId !== undefined) { diff --git a/ocm-cli/src/remote-context.ts b/ocm-cli/src/remote-context.ts index 97c61fa6..9f40ed91 100644 --- a/ocm-cli/src/remote-context.ts +++ b/ocm-cli/src/remote-context.ts @@ -1,3 +1,6 @@ +import type { OcmState } from './state.js' +import { normalizeManagerUrl } from './manager-auth.js' + export const REMOTE_MANAGER_URL_ENV = 'OCM_REMOTE_MANAGER_URL' export const REMOTE_REPO_NAME_ENV = 'OCM_REMOTE_REPO_NAME' export const REMOTE_REPO_ID_ENV = 'OCM_REMOTE_REPO_ID' @@ -37,3 +40,9 @@ export function readRemoteContext(env: NodeJS.ProcessEnv): RemoteContext | undef return { managerUrl, managerHost, repoName, repoId } } + +/** Returns the saved manager name when it belongs to the attached manager, else the manager host. */ +export function remoteDisplayName(remote: RemoteContext, state: OcmState | null): string { + if (state?.managerName && normalizeManagerUrl(state.managerUrl) === normalizeManagerUrl(remote.managerUrl)) return state.managerName + return remote.managerHost +} diff --git a/ocm-cli/src/state.ts b/ocm-cli/src/state.ts index f47f7f0f..bc8f050c 100644 --- a/ocm-cli/src/state.ts +++ b/ocm-cli/src/state.ts @@ -5,6 +5,7 @@ import { readJsonFile, writeJsonFileAtomic } from './json-store.js' export interface OcmState { managerUrl: string + managerName?: string lastRepoId?: number lastRepoName?: string lastRepoDir?: string diff --git a/ocm-cli/src/tui.tsx b/ocm-cli/src/tui.tsx index 420566e4..a6567176 100644 --- a/ocm-cli/src/tui.tsx +++ b/ocm-cli/src/tui.tsx @@ -6,7 +6,8 @@ import { showGoalDialog } from './tui-goal-dialog.js' import { showMultiRunLaunchDialog, showMultiRunsDialog } from './tui-multi-run-dialogs.js' import { formatMoveProgress } from './move-progress.js' import type { MoveProgress } from './move-progress.js' -import { readRemoteContext } from './remote-context.js' +import { readRemoteContext, remoteDisplayName } from './remote-context.js' +import { readState } from './state.js' import { createGoalStore } from './goal-store.js' import type { GoalStore } from './goal-store.js' import { formatGoalStatus, goalOutcomeToast } from './tui-goal.js' @@ -40,9 +41,10 @@ export default Plugin.define({ const remote = readRemoteContext(process.env) if (remote) { + const managerLabel = remoteDisplayName(remote, readState()) const indicator = () => ( - {remote.managerHost} + {managerLabel} {remote.repoName && ยท {remote.repoName}} ) diff --git a/ocm-cli/test/remote-context.test.ts b/ocm-cli/test/remote-context.test.ts index 8e259691..c41973b1 100644 --- a/ocm-cli/test/remote-context.test.ts +++ b/ocm-cli/test/remote-context.test.ts @@ -4,7 +4,10 @@ import { REMOTE_REPO_ID_ENV, buildRemoteAttachEnv, readRemoteContext, + remoteDisplayName, } from '../src/remote-context.js' +import type { RemoteContext } from '../src/remote-context.js' +import type { OcmState } from '../src/state.js' describe('buildRemoteAttachEnv', () => { it('round-trips through readRemoteContext', () => { @@ -77,4 +80,36 @@ describe('readRemoteContext', () => { expect(result?.repoId).toBe(42) }) +}) + +describe('remoteDisplayName', () => { + const remote: RemoteContext = { managerUrl: 'https://mgr.example.com', managerHost: 'mgr.example.com' } + + it('uses the saved manager name when the state matches the attached manager', () => { + const state: OcmState = { managerUrl: 'https://mgr.example.com', managerName: 'work' } + + expect(remoteDisplayName(remote, state)).toBe('work') + }) + + it('ignores trailing slashes when matching the manager url', () => { + const state: OcmState = { managerUrl: 'https://mgr.example.com/', managerName: 'work' } + + expect(remoteDisplayName(remote, state)).toBe('work') + }) + + it('falls back to the host when the state is for another manager', () => { + const state: OcmState = { managerUrl: 'https://other.example.com', managerName: 'work' } + + expect(remoteDisplayName(remote, state)).toBe('mgr.example.com') + }) + + it('falls back to the host when no name is saved', () => { + const state: OcmState = { managerUrl: 'https://mgr.example.com' } + + expect(remoteDisplayName(remote, state)).toBe('mgr.example.com') + }) + + it('falls back to the host without state', () => { + expect(remoteDisplayName(remote, null)).toBe('mgr.example.com') + }) }) \ No newline at end of file