mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
feat(ocm-cli): label the attached manager with an optional login name
This commit is contained in:
1 parent
612b0e6621
commit
8b428837c3
6 files changed
+66
-8
No files matched your search
+6
-2
@@ -55,9 +55,13 @@ directory entries and skips file entries.
|
||||
## Login
|
||||
|
||||
```bash
|
||||
ocm login <manager-url> [token]
|
||||
ocm login <manager-url> [token] [--name <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 <manager-url> [token]
|
||||
ocm login <manager-url> [token] [--name <name>]
|
||||
ocm status
|
||||
ocm list
|
||||
ocm use <repoId|name>
|
||||
|
||||
+11
-4
@@ -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 <url> [token] Save manager URL + token (token via stdin if omitted)
|
||||
ocm login <url> [token] [--name <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<void> {
|
||||
const url = args[0]
|
||||
if (!url) die('usage: ocm login <url> [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 <url> [token] [--name <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<void> {
|
||||
}
|
||||
|
||||
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<void> {
|
||||
}
|
||||
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) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import { readJsonFile, writeJsonFileAtomic } from './json-store.js'
|
||||
|
||||
export interface OcmState {
|
||||
managerUrl: string
|
||||
managerName?: string
|
||||
lastRepoId?: number
|
||||
lastRepoName?: string
|
||||
lastRepoDir?: string
|
||||
|
||||
+4
-2
@@ -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 = () => (
|
||||
<box flexDirection="row" flexShrink={0} gap={1}>
|
||||
<text fg={context.theme.hue.accent[200]}>{remote.managerHost}</text>
|
||||
<text fg={context.theme.hue.orange?.[400] ?? context.theme.text.feedback.warning.base}>{managerLabel}</text>
|
||||
{remote.repoName && <text fg={context.theme.text.muted}> · {remote.repoName}</text>}
|
||||
</box>
|
||||
)
|
||||
|
||||
@@ -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')
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user