mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
section 2: Phase 2: Attach env carries the Manager URL and repo id
This commit is contained in:
1 parent
e8548b1e07
commit
b316cb960c
4 files changed
+53
-9
No files matched your search
@@ -1,15 +1,19 @@
|
||||
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'
|
||||
|
||||
export type RemoteContext = {
|
||||
managerUrl: string
|
||||
managerHost: string
|
||||
repoName?: string
|
||||
repoId?: number
|
||||
}
|
||||
|
||||
export function buildRemoteAttachEnv(managerUrl: string, repoName: string): Record<string, string> {
|
||||
export function buildRemoteAttachEnv(managerUrl: string, repoName: string, repoId: number): Record<string, string> {
|
||||
return {
|
||||
[REMOTE_MANAGER_URL_ENV]: managerUrl,
|
||||
[REMOTE_REPO_NAME_ENV]: repoName,
|
||||
[REMOTE_REPO_ID_ENV]: String(repoId),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,14 +21,19 @@ export function readRemoteContext(env: NodeJS.ProcessEnv): RemoteContext | undef
|
||||
const urlValue = env[REMOTE_MANAGER_URL_ENV]
|
||||
if (!urlValue) return undefined
|
||||
|
||||
const managerUrl = urlValue.trim()
|
||||
|
||||
let managerHost: string
|
||||
try {
|
||||
managerHost = new URL(urlValue).host
|
||||
managerHost = new URL(managerUrl).host
|
||||
} catch {
|
||||
managerHost = urlValue.trim()
|
||||
managerHost = managerUrl
|
||||
}
|
||||
|
||||
const repoName = env[REMOTE_REPO_NAME_ENV] || undefined
|
||||
|
||||
return { managerHost, repoName }
|
||||
}
|
||||
const repoIdValue = Number(env[REMOTE_REPO_ID_ENV])
|
||||
const repoId = Number.isInteger(repoIdValue) && repoIdValue > 0 ? repoIdValue : undefined
|
||||
|
||||
return { managerUrl, managerHost, repoName, repoId }
|
||||
}
|
||||
+1
-1
@@ -42,7 +42,7 @@ export function buildAttachInvocation(target: AttachTarget): AttachInvocation {
|
||||
if (target.sessionID) args.push('--session', target.sessionID)
|
||||
return {
|
||||
args,
|
||||
env: { ...process.env, OPENCODE_PASSWORD: target.token, ...buildRemoteAttachEnv(target.managerUrl, target.repoName) },
|
||||
env: { ...process.env, OPENCODE_PASSWORD: target.token, ...buildRemoteAttachEnv(target.managerUrl, target.repoName, target.repoId) },
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +1,22 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import {
|
||||
REMOTE_MANAGER_URL_ENV,
|
||||
REMOTE_REPO_ID_ENV,
|
||||
buildRemoteAttachEnv,
|
||||
readRemoteContext,
|
||||
} from '../src/remote-context.js'
|
||||
|
||||
describe('buildRemoteAttachEnv', () => {
|
||||
it('round-trips through readRemoteContext', () => {
|
||||
const env = buildRemoteAttachEnv('https://mgr.example.com', 'oc-manager')
|
||||
const env = buildRemoteAttachEnv('https://mgr.example.com', 'oc-manager', 7)
|
||||
const result = readRemoteContext(env)
|
||||
|
||||
expect(result).toEqual({ managerHost: 'mgr.example.com', repoName: 'oc-manager' })
|
||||
expect(result).toEqual({
|
||||
managerUrl: 'https://mgr.example.com',
|
||||
managerHost: 'mgr.example.com',
|
||||
repoName: 'oc-manager',
|
||||
repoId: 7,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -43,4 +49,32 @@ describe('readRemoteContext', () => {
|
||||
|
||||
expect(result?.repoName).toBeUndefined()
|
||||
})
|
||||
|
||||
it('returns the trimmed raw manager URL', () => {
|
||||
const env = { [REMOTE_MANAGER_URL_ENV]: ' https://mgr.example.com ' }
|
||||
const result = readRemoteContext(env)
|
||||
|
||||
expect(result?.managerUrl).toBe('https://mgr.example.com')
|
||||
})
|
||||
|
||||
it('returns undefined repoId when the repo id var is missing', () => {
|
||||
const env = { [REMOTE_MANAGER_URL_ENV]: 'https://mgr.example.com' }
|
||||
const result = readRemoteContext(env)
|
||||
|
||||
expect(result?.repoId).toBeUndefined()
|
||||
})
|
||||
|
||||
it.each(['', 'abc', '0', '-1', '3.5'])('returns undefined repoId for %j', (value) => {
|
||||
const env = { [REMOTE_MANAGER_URL_ENV]: 'https://mgr.example.com', [REMOTE_REPO_ID_ENV]: value }
|
||||
const result = readRemoteContext(env)
|
||||
|
||||
expect(result?.repoId).toBeUndefined()
|
||||
})
|
||||
|
||||
it('returns the numeric repoId when the repo id var is a positive integer', () => {
|
||||
const env = { [REMOTE_MANAGER_URL_ENV]: 'https://mgr.example.com', [REMOTE_REPO_ID_ENV]: '42' }
|
||||
const result = readRemoteContext(env)
|
||||
|
||||
expect(result?.repoId).toBe(42)
|
||||
})
|
||||
})
|
||||
@@ -1,7 +1,7 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import type { WarpTarget } from '../src/warp.js'
|
||||
import { setPendingWarp, takePendingWarp, buildAttachInvocation, runPendingWarp } from '../src/warp.js'
|
||||
import { REMOTE_MANAGER_URL_ENV, REMOTE_REPO_NAME_ENV } from '../src/remote-context.js'
|
||||
import { REMOTE_MANAGER_URL_ENV, REMOTE_REPO_ID_ENV, REMOTE_REPO_NAME_ENV } from '../src/remote-context.js'
|
||||
|
||||
const sampleTarget: WarpTarget = {
|
||||
managerUrl: 'https://manager.example.com',
|
||||
@@ -24,6 +24,7 @@ describe('buildAttachInvocation', () => {
|
||||
OPENCODE_PASSWORD: 'tok_abc123',
|
||||
[REMOTE_MANAGER_URL_ENV]: 'https://manager.example.com',
|
||||
[REMOTE_REPO_NAME_ENV]: 'my-repo',
|
||||
[REMOTE_REPO_ID_ENV]: '42',
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in new issue
Block a user