mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
fix: address review feedback on execute status, file links, and login flags
This commit is contained in:
1 parent
421387793b
commit
801a4cd112
5 files changed
+81
-13
No files matched your search
+25
-6
@@ -135,15 +135,34 @@ function findRepo(repos: ManagerRepo[], needle: string | number): ManagerRepo |
|
||||
return repos.find((r) => r.name === needle) ?? repos.find((r) => r.name.toLowerCase() === needle.toLowerCase())
|
||||
}
|
||||
|
||||
export async function cmdLogin(args: string[]): Promise<void> {
|
||||
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 LOGIN_USAGE = 'usage: ocm login <url> [token] [--name <name>]'
|
||||
|
||||
function parseLoginArgs(args: string[]): { url: string; token?: string; managerName?: string } {
|
||||
const positional: string[] = []
|
||||
const managerNames: string[] = []
|
||||
for (let index = 0; index < args.length; index++) {
|
||||
const arg = args[index]
|
||||
if (arg === '--name') {
|
||||
const value = args[index + 1]
|
||||
if (!value || value.startsWith('-')) die(LOGIN_USAGE)
|
||||
managerNames.push(value)
|
||||
index++
|
||||
continue
|
||||
}
|
||||
if (arg.startsWith('-')) die(LOGIN_USAGE)
|
||||
positional.push(arg)
|
||||
}
|
||||
if (managerNames.length > 1) die(LOGIN_USAGE)
|
||||
const url = positional[0]
|
||||
if (!url || (nameFlag >= 0 && !managerName)) die('usage: ocm login <url> [token] [--name <name>]')
|
||||
if (!url) die(LOGIN_USAGE)
|
||||
return { url, token: positional[1], managerName: managerNames[0] }
|
||||
}
|
||||
|
||||
export async function cmdLogin(args: string[]): Promise<void> {
|
||||
const { url, token: providedToken, managerName } = parseLoginArgs(args)
|
||||
const normalisedUrl = normalizeManagerUrl(url)
|
||||
|
||||
let token = positional[1]
|
||||
let token = providedToken
|
||||
if (!token) {
|
||||
if (process.stdin.isTTY) {
|
||||
process.stderr.write('Paste token (input hidden): ')
|
||||
|
||||
@@ -160,6 +160,46 @@ describe('cli auth commands', () => {
|
||||
restore()
|
||||
})
|
||||
|
||||
const expectLoginUsageError = async (args: string[]) => {
|
||||
const { cli, stderr, setToken, restore } = await loadCli()
|
||||
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
|
||||
throw new Error('exit')
|
||||
})
|
||||
await expect(cli.cmdLogin(args)).rejects.toThrow('exit')
|
||||
expect(stderr.join('')).toContain('usage: ocm login <url> [token] [--name <name>]')
|
||||
expect(exitSpy).toHaveBeenCalledWith(1)
|
||||
expect(setToken).not.toHaveBeenCalled()
|
||||
restore()
|
||||
}
|
||||
|
||||
it('ocm login rejects a repeated --name flag without saving a token', async () => {
|
||||
await expectLoginUsageError(['https://manager.example.com', '--name', 'prod', '--name', 'staging'])
|
||||
})
|
||||
|
||||
it('ocm login rejects a --name flag without a value', async () => {
|
||||
await expectLoginUsageError(['https://manager.example.com', 'tok_xyz', '--name'])
|
||||
})
|
||||
|
||||
it('ocm login rejects a --name value that looks like a flag', async () => {
|
||||
await expectLoginUsageError(['https://manager.example.com', '--name', '--force'])
|
||||
})
|
||||
|
||||
it('ocm login rejects an unknown flag', async () => {
|
||||
await expectLoginUsageError(['https://manager.example.com', 'tok_xyz', '--force'])
|
||||
})
|
||||
|
||||
it('ocm login accepts the token before or after --name', async () => {
|
||||
const afterName = await loadCli()
|
||||
await afterName.cli.cmdLogin(['https://manager.example.com', 'tok_xyz', '--name', 'prod'])
|
||||
expect(afterName.setToken).toHaveBeenCalledWith('https://manager.example.com', 'tok_xyz')
|
||||
afterName.restore()
|
||||
|
||||
const beforeName = await loadCli()
|
||||
await beforeName.cli.cmdLogin(['https://manager.example.com', '--name', 'prod', 'tok_xyz'])
|
||||
expect(beforeName.setToken).toHaveBeenCalledWith('https://manager.example.com', 'tok_xyz')
|
||||
beforeName.restore()
|
||||
})
|
||||
|
||||
it('ocm logout reports removal', async () => {
|
||||
const { cli, written, restore } = await loadCli({ deleteTokenImpl: () => Promise.resolve(true) })
|
||||
await cli.cmdLogout()
|
||||
|
||||
Reference in new issue
Block a user