Skip to content

Commit

Permalink
feat: prompt only orgs with certain squid
Browse files Browse the repository at this point in the history
  • Loading branch information
belopash authored and mo4islona committed Mar 7, 2024
1 parent e9d3c78 commit 4c416c6
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 14 deletions.
16 changes: 14 additions & 2 deletions src/api/profile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { api, ApiError } from './api';
import { HttpResponse } from './types';
import { HttpResponse, SquidResponse } from './types';

export type Profile = {
username?: string;
Expand All @@ -18,7 +18,7 @@ export async function profile({
const { body } = await api<HttpResponse<Profile>>({
method: 'get',
auth,
path: `/profile`,
path: `/user`,
});

if (!body.payload) {
Expand All @@ -33,3 +33,15 @@ export async function listOrganizations() {

return organizations || [];
}

export async function listSquids({ squidName }: { squidName?: string }) {
const { body } = await api<HttpResponse<SquidResponse[]>>({
method: 'get',
path: `/user/squids`,
query: {
squidName,
},
});

return body.payload;
}
8 changes: 7 additions & 1 deletion src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
25 changes: 22 additions & 3 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down Expand Up @@ -73,8 +74,8 @@ export abstract class CliCommand extends Command {
throw error;
}

async promptOrganization(organizationCode: string | null | undefined, using: string): Promise<string> {
if (organizationCode) return organizationCode;
async promptOrganization(orgCode: string | null | undefined, using: string): Promise<string> {
if (orgCode) return orgCode;

const organizations = await listOrganizations();
if (organizations.length === 0) {
Expand All @@ -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<string> {
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:`));
Expand Down
6 changes: 3 additions & 3 deletions src/commands/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/restart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
6 changes: 4 additions & 2 deletions src/commands/rm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
{
Expand All @@ -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([
{
Expand Down

0 comments on commit 4c416c6

Please sign in to comment.