diff --git a/src/api/profile.ts b/src/api/profile.ts index f737370..a8dedef 100644 --- a/src/api/profile.ts +++ b/src/api/profile.ts @@ -1,5 +1,5 @@ import { api, ApiError } from './api'; -import { HttpResponse } from './types'; +import { HttpResponse, SquidResponse } from './types'; export type Profile = { username?: string; @@ -18,7 +18,7 @@ export async function profile({ const { body } = await api>({ method: 'get', auth, - path: `/profile`, + path: `/user`, }); if (!body.payload) { @@ -33,3 +33,15 @@ export async function listOrganizations() { return organizations || []; } + +export async function listSquids({ squidName }: { squidName?: string }) { + const { body } = await api>({ + method: 'get', + path: `/user/squids`, + query: { + squidName, + }, + }); + + return body.payload; +} diff --git a/src/api/types.ts b/src/api/types.ts index 099aedb..228833a 100644 --- a/src/api/types.ts +++ b/src/api/types.ts @@ -93,7 +93,13 @@ export type SquidResponse = { isPublic: boolean; deploy?: DeployResponse; createdAt: Date; - organization?: { id: string; code: string }; + organization?: SquidOrganizationResponse; +}; + +export type SquidOrganizationResponse = { + id: string; + code: string; + name: string; }; export type SquidNameIsAvailableResponse = { diff --git a/src/command.ts b/src/command.ts index e4981d8..6db7e82 100644 --- a/src/command.ts +++ b/src/command.ts @@ -1,8 +1,9 @@ import { Command } from '@oclif/core'; import chalk from 'chalk'; import inquirer from 'inquirer'; +import { isNil } from 'lodash'; -import { ApiError, listOrganizations } from './api'; +import { ApiError, SquidOrganizationResponse, listOrganizations, listSquids } from './api'; import { getTTY } from './tty'; export const RELEASE_DEPRECATE = [ @@ -73,8 +74,8 @@ export abstract class CliCommand extends Command { throw error; } - async promptOrganization(organizationCode: string | null | undefined, using: string): Promise { - if (organizationCode) return organizationCode; + async promptOrganization(orgCode: string | null | undefined, using: string): Promise { + if (orgCode) return orgCode; const organizations = await listOrganizations(); if (organizations.length === 0) { @@ -83,6 +84,24 @@ export abstract class CliCommand extends Command { return organizations[0].code; } + return await this.getOrganizationPromt(organizations, using); + } + + async promptSquidOrganization(orgCode: string | null | undefined, squidName: string, using: string): Promise { + if (orgCode) return orgCode; + + const squids = await listSquids({ squidName }); + const organizations = squids.map((s) => s.organization).filter((o): o is SquidOrganizationResponse => !isNil(o)); + if (organizations.length === 0) { + return this.error(`No organizations has been found`); + } else if (organizations.length === 1) { + return organizations[0].code; + } + + return await this.getOrganizationPromt(organizations, using); + } + + private async getOrganizationPromt(organizations: { code: string; name: string }[], using: string) { const { stdin, stdout } = getTTY(); if (!stdin || !stdout) { this.log(chalk.dim(`You have ${organizations.length} organizations:`)); diff --git a/src/commands/logs.ts b/src/commands/logs.ts index 000fadc..b0106c0 100644 --- a/src/commands/logs.ts +++ b/src/commands/logs.ts @@ -77,13 +77,13 @@ export default class Logs extends CliCommand { args: { name }, } = await this.parse(Logs); - const orgCode = await this.promptOrganization(org, 'using "-o" flag'); + const { squidName, versionName } = parseNameAndVersion(name, this); + + const orgCode = await this.promptSquidOrganization(org, squidName, 'using "-o" flag'); const fromDate = parseDate(since); this.log(`Fetching logs from ${fromDate.toISOString()}...`); - const { squidName, versionName } = parseNameAndVersion(name, this); - if (follow) { await this.fetchLogs(orgCode, squidName, versionName, { limit: 30, diff --git a/src/commands/prod.ts b/src/commands/prod.ts index 03df37a..c00f351 100644 --- a/src/commands/prod.ts +++ b/src/commands/prod.ts @@ -33,7 +33,7 @@ export default class Prod extends DeployCommand { const { squidName, versionName } = parseNameAndVersion(args.nameAndVersion, this); - const orgCode = await this.promptOrganization(org, 'using "-o" flag'); + const orgCode = await this.promptSquidOrganization(org, squidName, 'using "-o" flag'); const foundSquid = await getSquid({ orgCode, squidName, versionName }); if (!foundSquid.versions?.length) { diff --git a/src/commands/restart.ts b/src/commands/restart.ts index c5011fb..9f7696f 100644 --- a/src/commands/restart.ts +++ b/src/commands/restart.ts @@ -47,10 +47,10 @@ export default class Restart extends DeployCommand { flags: { 'no-stream-logs': disableStreamLogs, org }, args: { nameAndVersion }, } = await this.parse(Restart); - const orgCode = await this.promptOrganization(org, 'using "-o" flag'); - const { squidName, versionName } = parseNameAndVersion(nameAndVersion, this); + const orgCode = await this.promptSquidOrganization(org, squidName, 'using "-o" flag'); + const deploy = await restartSquid({ orgCode, squidName, versionName }); await this.pollDeploy({ orgCode, deployId: deploy.id, streamLogs: !disableStreamLogs }); diff --git a/src/commands/rm.ts b/src/commands/rm.ts index a4e135b..436be9c 100644 --- a/src/commands/rm.ts +++ b/src/commands/rm.ts @@ -36,11 +36,11 @@ export default class Rm extends DeployCommand { flags: { force, org }, } = await this.parse(Rm); - const orgCode = await this.promptOrganization(org, 'using "-o" flag'); - if (nameAndVersion.includes('@')) { const { squidName, versionName } = parseNameAndVersion(nameAndVersion, this); + const orgCode = await this.promptSquidOrganization(org, squidName, 'using "-o" flag'); + if (!force) { const { confirm } = await inquirer.prompt([ { @@ -57,6 +57,8 @@ export default class Rm extends DeployCommand { CliUx.ux.action.stop(); return; } else { + const orgCode = await this.promptSquidOrganization(org, nameAndVersion, 'using "-o" flag'); + if (!force) { const { confirm } = await inquirer.prompt([ {