Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make general improvements #199

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,19 @@ export interface IGetMailboxesResponse {
};
}

export interface IMailPlan {
available: boolean;
maxAccount: number;
maxForwarder: number;
maxInbound: string;
maxInboundMailRetention: number;
maxOutboundMailRetention: number;
maxOutboundPerDay: number;
maxOutboundPerMonth: number;
name: string;
price: number;
}

export interface IGetMailsAccounts {
name: string;
createdAt: string;
Expand Down Expand Up @@ -195,10 +208,10 @@ export default abstract class extends Command {
'api-token': Flags.string({
description: 'your api token to use for authentication',
}),
region: Flags.string({
description: 'the region you want to deploy your app to',
options: ['iran', 'germany'],
}),
// region: Flags.string({
// description: 'the region you want to deploy your app to',
// options: ['iran', 'germany'],
// }),
account: Flags.string({
description: 'temporarily switch to a different account',
}),
Expand Down Expand Up @@ -246,12 +259,12 @@ Please check your network connection.`);
}

if (!config['api-token'] || !config.region) {
const { api_token, region } = config.account
const { api_token } = config.account
? await this.getAccount(config.account)
: await this.getCurrentAccount();

config['api-token'] = config['api-token'] || api_token;
config.region = config.region || region;
// config.region = config.region || region;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget that all accounts have a region field in the global configuration file liara-auth.json

I believe we shouldn't modify this global config file. To eliminate the prompt asking for the region, we can simply set a fallback region to "IRAN" and never prompt the user for a region.

Additionally, we should only remove the --region flag for simplicity.

For backward compatibility, in my opinion, we should automatically set the region to "IRAN". This ensures nothing unexpected happens in the CLI and keeps the region field hidden from the user.

This approach will streamline the process and improve the user experience.

PING @mhemrg


// @ts-ignore
Expand Down
5 changes: 0 additions & 5 deletions src/commands/app/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,8 @@ export default class AppCreate extends Command {

const name = flags.app || (await this.promptAppName());

const account = await this.getCurrentAccount();

await this.setGotConfig(flags);

((account && account.region === 'germany') || flags.region === 'germany') &&
this.error('We do not support germany any more.');

const platform = flags.platform || (await this.promptPlatform());

if (!AVAILABLE_PLATFORMS.includes(platform)) {
Expand Down
23 changes: 5 additions & 18 deletions src/commands/bucket/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class BucketCreate extends Command {

async setGotConfig(
config: IConfig,
isObjMode: boolean = true
isObjMode: boolean = true,
): Promise<void> {
if (isObjMode) {
await super.setGotConfig(config);
Expand All @@ -60,8 +60,7 @@ export default class BucketCreate extends Command {

await this.setGotConfig(flags);

((account && account.region === 'germany') || flags.region === 'germany') &&
this.error('We do not support germany any more.');
// (account && account.region === 'germany') || (flags.region === 'germany') && this.error('We do not support germany any more.');

const plan = flags.plan || (await this.promptPlan());

Expand Down Expand Up @@ -123,8 +122,8 @@ export default class BucketCreate extends Command {
await this.setGotConfig(flags, false);

try {
// TODO: Use proper type for plans
const { plans } = await this.got('v1/me').json<{ plans: any }>();

this.spinner.stop();

const { plan } = (await inquirer.prompt({
Expand All @@ -133,27 +132,15 @@ export default class BucketCreate extends Command {
message: 'Please select a plan:',
choices: [
...Object.keys(plans.objectStorage)
.filter((plan) => {
if (plans.objectStorage[plan].available) {
return true;
}
})
.filter((plan) => plans.objectStorage[plan].available)
.map((plan) => {
const availablePlan = plans.objectStorage[plan];
const price = availablePlan.price * 720;
const space = availablePlan.space;
const storageClass = availablePlan.storageClass;
return {
value: plan,
name: `Space: ${space}${spacing(
5,
space
)}GB, Storage Class: ${storageClass}${spacing(
5,
storageClass
)}, Price: ${price.toLocaleString()}${
price ? spacing(7, price) + 'Tomans/Month' : ''
}`,
name: `Plan: ${plan}, Space: ${space}GB, Storage class: ${storageClass}, Price: ${price.toLocaleString()} Tomans/Month`,
};
}),
],
Expand Down
20 changes: 9 additions & 11 deletions src/commands/db/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ export default class Create extends Command {
const debug = createDebugLogger(flags.debug);

await this.setGotConfig(flags);
const account = await this.getCurrentAccount();

((account && account.region === 'germany') || flags.region === 'germany') &&
this.error('We do not support germany any more.');

const hostname = flags.name || (await this.promptHostname());
const type = flags.type || (await this.promptType());
Expand Down Expand Up @@ -200,13 +196,15 @@ export default class Create extends Command {
})
.map((bundlePlan) => {
const planDetails = plans.databaseBundlePlans[bundlePlan];
return Object.keys(planDetails).map((key) => {
const { displayPrice } = planDetails[key];
return {
name: `Plan type: ${key}, Price: ${displayPrice.toLocaleString()} Tomans/Month`,
value: key,
};
});
return Object.keys(planDetails)
.filter((key) => planDetails[key].available)
.map((key) => {
const { displayPrice } = planDetails[key];
return {
name: `Plan: ${key}, Price: ${displayPrice.toLocaleString()} Tomans/Month`,
value: key,
};
});
})
.flat(),
],
Expand Down
38 changes: 16 additions & 22 deletions src/commands/db/resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import spacing from '../../utils/spacing.js';
export default class Resize extends Command {
static description = 'resize a database';

static PATH = 'v1/databases/{database-id}/resize';

static flags = {
...Command.flags,
name: Flags.string({
Expand All @@ -33,32 +31,32 @@ export default class Resize extends Command {
const debug = createDebugLogger(flags.debug);

await this.setGotConfig(flags);
const account = await this.getCurrentAccount();

((account && account.region === 'germany') || flags.region === 'germany') &&
this.error('We do not support germany any more.');

const hostname = flags.name || (await this.promptHostname());
const disk =
flags.disk === 'y'
? true
: flags.disk === 'n'
? false
: (await this.promptDisk()) === 'y'
? true
: false;
? false
: (await this.promptDisk()) === 'y'
? true
: false;

try {
const database = await this.getDatabaseByHostname(hostname);
if (database === undefined) {
this.log(`Database ${hostname} not found`);
return;
}

const planID = flags.plan || (await this.promptPlan(database.type));

const databaseID = database._id;
await this.got.post(Resize.PATH.replace('{database-id}', databaseID), {

const result = await this.got.post(`v1/databases/${databaseID}/resize`, {
json: { planID: planID, disk: disk },
});

this.log(`Database ${hostname} changed to plan ${planID}.`);
} catch (error) {
debug(error.message);
Expand All @@ -82,23 +80,23 @@ export default class Resize extends Command {
}

async getDatabaseByHostname(hostname: string) {
const { databases } = await this.got(
'v1/databases'
).json<IGetDatabasesResponse>();
const { databases } =
await this.got('v1/databases').json<IGetDatabasesResponse>();

if (!databases.length) {
this.error(`Not found any database.
Please open up https://console.liara.ir/databases and create the database, first.`);
}

const database = databases.find(
(database) => database.hostname === hostname
(database) => database.hostname === hostname,
);
return database;
}

async promptPlan(databaseType: string) {
this.spinner.start('Loading...');

try {
const { plans } = await this.got('v1/me').json<{ plans: any }>();
this.spinner.stop();
Expand All @@ -111,6 +109,7 @@ Please open up https://console.liara.ir/databases and create the database, first
...Object.keys(plans.databases)
.filter((plan) => {
if (
(plan === 'free' || plan.includes('g2')) &&
plans.databases[plan].available &&
plans.databases[plan].supports.includes(databaseType)
) {
Expand All @@ -126,14 +125,9 @@ Please open up https://console.liara.ir/databases and create the database, first
const storageClass = availablePlan.storageClass;
return {
value: plan,
name: `RAM: ${ram}${spacing(5, ram)}GB, CPU: ${cpu}${spacing(
5,
cpu
)}Core, Disk: ${disk}${spacing(3, disk) + 'GB'}${
name: `RAM: ${ram}GB, CPU: ${cpu} Core, Disk: ${disk}GB, ${
storageClass || 'SSD'
}, Price: ${price.toLocaleString()}${
price ? spacing(7, price) + 'Tomans/Month' : ''
}`,
}, Price: ${price.toLocaleString()} Tomans/Month `,
};
}),
],
Expand Down
42 changes: 27 additions & 15 deletions src/commands/mail/create.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import ora, { Ora } from 'ora';
import inquirer from 'inquirer';
import Command, { IConfig } from '../../base.js';
import Command, { IConfig, IMailPlan } from '../../base.js';
import { Flags } from '@oclif/core';
import {
MAIL_SERVICE_MODES,
MAIL_SERVICE_PLANS,
MAIL_SERVICE_URL,
MAIL_SERVICE_PLANS,
DEV_MODE,
MAIL_SERVICE_URL_DEV,
} from '../../constants.js';
import { createDebugLogger } from '../../utils/output.js';
import { getMailPlanName } from '../../utils/get-mail-plan-names.js';

export default class MailCreate extends Command {
static description = 'create a mail server';
Expand All @@ -33,9 +34,6 @@ export default class MailCreate extends Command {

async setGotConfig(config: IConfig): Promise<void> {
await super.setGotConfig(config);
this.got = this.got.extend({
prefixUrl: DEV_MODE ? MAIL_SERVICE_URL_DEV : MAIL_SERVICE_URL,
});
}

async run() {
Expand All @@ -47,13 +45,8 @@ export default class MailCreate extends Command {

const domain = flags.domain || (await this.promptDomain());

const account = await this.getCurrentAccount();

await this.setGotConfig(flags);

((account && account.region === 'germany') || flags.region === 'germany') &&
this.error('We do not support germany any more.');

const plan = flags.platform || (await this.promptPlan());

if (!MAIL_SERVICE_PLANS.includes(plan)) {
Expand All @@ -67,7 +60,10 @@ export default class MailCreate extends Command {
}

try {
await this.got.post('api/v1/mails', { json: { domain, plan, mode } });
await this.got.post('api/v1/mails', {
json: { domain, plan, mode },
prefixUrl: MAIL_SERVICE_URL,
});
this.log(`Mail server ${domain} created.`);
} catch (error) {
debug(error.message);
Expand All @@ -82,7 +78,7 @@ export default class MailCreate extends Command {

if (error.response && error.response.statusCode === 409) {
this.error(
`The mail server already exists. Please use a unique name for your mail server.`
`The mail server already exists. Please use a unique name for your mail server.`,
);
}

Expand All @@ -92,7 +88,7 @@ export default class MailCreate extends Command {
error.response.body
) {
this.error(
`You are allowed to create only one Mail Server on the free plan.`
`You are allowed to create only one Mail Server on the free plan.`,
);
}

Expand All @@ -115,13 +111,29 @@ export default class MailCreate extends Command {
this.spinner.start('Loading...');

try {
const { plans } = await this.got('v1/me').json<{ plans: any }>();

this.spinner.stop();

const { plan } = (await inquirer.prompt({
name: 'plan',
type: 'list',
message: 'Please select the plan you want:',
choices: [...MAIL_SERVICE_PLANS.map((plan) => plan)],
message: 'Please select a plan:',
choices: [
...Object.keys(plans.mail)
.filter((plan) => plans.mail[plan].available)
.map((plan) => {
const availablePlan = plans.mail[plan];
const maxAccount = availablePlan.maxAccount;
const maxOutboundPerDay = availablePlan.maxOutboundPerDay;
const maxOutboundPerMonth = availablePlan.maxOutboundPerMonth;
const price = availablePlan.price;
return {
value: plan,
name: `Plan: ${getMailPlanName(plan)}, Max outbound per day: ${maxOutboundPerDay}, Max outbound per month: ${maxOutboundPerMonth}, Max account: ${maxAccount}, Price: ${price}`,
};
}),
],
})) as { plan: string };

return plan;
Expand Down
6 changes: 1 addition & 5 deletions src/commands/zone/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ export default class Check extends Command {
const debug = createDebugLogger(flags.debug);

await this.setGotConfig(flags);
const account = await this.getCurrentAccount();

((account && account.region === 'germany') || flags.region === 'germany') &&
this.error('We do not support germany any more.');

const zone = flags.zone || (await this.promptZone());

Expand All @@ -54,7 +50,7 @@ export default class Check extends Command {
}

if (error.response && error.response.statusCode === 404) {
this.error(`The zone does not exist.`);
this.error(`Zone does not exists or its status is not pending.`);
}

if (error.response && error.response.statusCode === 406) {
Expand Down
Loading