@chabokan.net/cli 0.8.8 → 0.8.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,91 @@
1
+ import { Flags } from '@oclif/core';
2
+ import inquirer from 'inquirer';
3
+ import axios from "axios";
4
+ import chalk from "chalk";
5
+ import { isObject } from "../helper.js";
6
+ import Command from "../base.js";
7
+ export default class Login extends Command {
8
+ static description = 'login to hub.chabokan.net account';
9
+ static flags = {
10
+ ...Command.flags,
11
+ username: Flags.string({ char: 'u', description: 'your username' }),
12
+ password: Flags.string({ char: 'p', description: 'your password' }),
13
+ token: Flags.string({ char: 't', description: 'login with api token' }),
14
+ };
15
+ async run() {
16
+ const { args, flags } = await this.parse(Login);
17
+ const cli = this;
18
+ await this.init_run();
19
+ const config_json = await this.read_config();
20
+ const body = { username: flags.username, password: flags.password };
21
+ if (!flags.token) {
22
+ if (!flags.username) {
23
+ let { username } = await inquirer.prompt({
24
+ type: 'input',
25
+ message: 'Enter your username:',
26
+ name: 'username',
27
+ validate(input) {
28
+ if (input.length === 0) {
29
+ return false;
30
+ }
31
+ return true;
32
+ }
33
+ });
34
+ body.username = username;
35
+ }
36
+ if (!flags.password) {
37
+ let { password } = await inquirer.prompt({
38
+ type: 'password',
39
+ name: 'password',
40
+ message: 'Enter your password:',
41
+ validate(input) {
42
+ if (input.length === 0) {
43
+ return false;
44
+ }
45
+ return true;
46
+ }
47
+ });
48
+ body.password = password;
49
+ }
50
+ const { data } = await axios.post("accounts/login/", body, this.axiosConfig);
51
+ if (data.success) {
52
+ if (!isObject(config_json.users)) {
53
+ config_json.users = {};
54
+ }
55
+ // @ts-ignore
56
+ config_json.users[body.username] = {
57
+ "token": data.token
58
+ };
59
+ config_json["default_user"] = body.username;
60
+ this.write_config(config_json);
61
+ cli.log(`${chalk.green('[Success]')} you are logged in successfully.`);
62
+ }
63
+ else {
64
+ cli.log(`${chalk.red('[Error]')} your username or password is wrong!`);
65
+ }
66
+ }
67
+ else {
68
+ try {
69
+ this.axiosConfig.headers.Authorization = `Token ${flags.token}`;
70
+ const { data } = await axios.get("accounts/info/", this.axiosConfig);
71
+ if (!isObject(config_json.users)) {
72
+ config_json.users = {};
73
+ }
74
+ // @ts-ignore
75
+ config_json.users[data.user.email] = {
76
+ "token": flags.token
77
+ };
78
+ config_json["default_user"] = data.user.email;
79
+ await this.write_config(config_json);
80
+ cli.log(`${chalk.green('[Success]')} you are logged in successfully.`);
81
+ }
82
+ catch (e) {
83
+ if (process.env.CHABOK_DEBUG == "true") {
84
+ // @ts-ignore
85
+ cli.log(e);
86
+ }
87
+ cli.log(`${chalk.red('[Error]')} your token is wrong!`);
88
+ }
89
+ }
90
+ }
91
+ }
@@ -0,0 +1,8 @@
1
+ import Command from "../../base.js";
2
+ export default class ServiceList extends Command {
3
+ static description: string;
4
+ static flags: {
5
+ help: import("@oclif/core/lib/interfaces/parser.js").BooleanFlag<void>;
6
+ };
7
+ run(): Promise<void>;
8
+ }
@@ -0,0 +1,32 @@
1
+ import Command from "../../base.js";
2
+ import { ux } from "@oclif/core";
3
+ export default class ServiceList extends Command {
4
+ static description = "show account services list";
5
+ static flags = {
6
+ ...Command.flags,
7
+ };
8
+ async run() {
9
+ const { args, flags } = await this.parse(ServiceList);
10
+ const cli = this;
11
+ await this.init_run();
12
+ const config_json = await this.read_config();
13
+ let all_services = await this.get_services();
14
+ if (all_services.length > 0) {
15
+ const services_data = all_services.map((service) => {
16
+ // const shamshiate = shamsi.gregorianToJalali(
17
+ // new Date(project.created)
18
+ // );
19
+ return {
20
+ Name: service.name,
21
+ };
22
+ });
23
+ ux.table(services_data, {
24
+ Name: {},
25
+ }, flags);
26
+ }
27
+ else {
28
+ cli.log("first you should create service");
29
+ return;
30
+ }
31
+ }
32
+ }
@@ -0,0 +1,10 @@
1
+ import Command from "../../base.js";
2
+ export default class ServiceLogs extends Command {
3
+ static description: string;
4
+ static flags: {
5
+ service: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces/parser.js").CustomOptions>;
6
+ help: import("@oclif/core/lib/interfaces/parser.js").BooleanFlag<void>;
7
+ };
8
+ run(): Promise<void>;
9
+ send_request(cli: any, selected_service: any): Promise<void>;
10
+ }
@@ -0,0 +1,59 @@
1
+ import { Flags } from '@oclif/core';
2
+ import Command from "../../base.js";
3
+ import { isEmptyObject } from "../../helper.js";
4
+ import chalk from "chalk";
5
+ import inquirer from 'inquirer';
6
+ import axios from "axios";
7
+ export default class ServiceLogs extends Command {
8
+ static description = 'read latest logs from service';
9
+ static flags = {
10
+ ...Command.flags,
11
+ service: Flags.string({ char: 's', description: 'service name' }),
12
+ };
13
+ async run() {
14
+ const { args, flags } = await this.parse(ServiceLogs);
15
+ const cli = this;
16
+ await this.init_run();
17
+ const config_json = await this.read_config();
18
+ let selected_service = flags.service;
19
+ if (isEmptyObject(config_json.users)) {
20
+ cli.log(`${chalk.red('[Error]')} first you should login!`);
21
+ return;
22
+ }
23
+ if (!flags.service) {
24
+ let all_services = await this.get_services();
25
+ if (all_services.length > 0) {
26
+ let { service } = await inquirer.prompt({
27
+ type: 'list',
28
+ message: 'Please select a service:',
29
+ name: 'service',
30
+ choices: all_services
31
+ });
32
+ selected_service = service;
33
+ }
34
+ else {
35
+ cli.log("first you should create service");
36
+ return;
37
+ }
38
+ }
39
+ await this.send_request(cli, selected_service);
40
+ }
41
+ async send_request(cli, selected_service) {
42
+ try {
43
+ if (selected_service) {
44
+ const { data } = await axios.get("services/" + selected_service + "/logs/", this.axiosConfig);
45
+ if (data.success) {
46
+ cli.log(data.logs);
47
+ }
48
+ }
49
+ }
50
+ catch (e) {
51
+ if (e.response.status == 404) {
52
+ cli.log(chalk.blue("Selected Service Not Founded."));
53
+ }
54
+ else {
55
+ console.log(e.data);
56
+ }
57
+ }
58
+ }
59
+ }
@@ -0,0 +1,13 @@
1
+ import Command from "../../base.js";
2
+ export default class ServiceResize extends Command {
3
+ static description: string;
4
+ static flags: {
5
+ service: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces/parser.js").CustomOptions>;
6
+ ram: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces/parser.js").CustomOptions>;
7
+ cpu: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces/parser.js").CustomOptions>;
8
+ disk: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces/parser.js").CustomOptions>;
9
+ help: import("@oclif/core/lib/interfaces/parser.js").BooleanFlag<void>;
10
+ };
11
+ run(): Promise<void>;
12
+ send_request(cli: any, selected_service: any, ram: any, cpu: any, disk: any): Promise<void>;
13
+ }
@@ -0,0 +1,130 @@
1
+ import { Flags } from '@oclif/core';
2
+ import Command from "../../base.js";
3
+ import { isEmptyObject } from "../../helper.js";
4
+ import chalk from "chalk";
5
+ import inquirer from 'inquirer';
6
+ import axios from "axios";
7
+ export default class ServiceResize extends Command {
8
+ static description = 'resize a service';
9
+ static flags = {
10
+ ...Command.flags,
11
+ service: Flags.string({ char: 's', description: 'service name' }),
12
+ ram: Flags.string({ char: 'r', description: 'RAM' }),
13
+ cpu: Flags.string({ char: 'c', description: 'CPU' }),
14
+ disk: Flags.string({ char: 'd', description: 'DISK' }),
15
+ };
16
+ async run() {
17
+ const { args, flags } = await this.parse(ServiceResize);
18
+ const cli = this;
19
+ await this.init_run();
20
+ const config_json = await this.read_config();
21
+ let selected_service = flags.service;
22
+ let selected_ram = flags.ram;
23
+ let selected_cpu = flags.cpu;
24
+ let selected_disk = flags.disk;
25
+ if (isEmptyObject(config_json.users)) {
26
+ cli.log(`${chalk.red('[Error]')} first you should login!`);
27
+ return;
28
+ }
29
+ if (!selected_service) {
30
+ let all_services = await this.get_services({ 'not_status': 'pending', 'has_custom_plan': 'true' });
31
+ if (all_services) {
32
+ let { service } = await inquirer.prompt({
33
+ type: 'list',
34
+ message: 'Please select a service:',
35
+ name: 'service',
36
+ choices: all_services
37
+ });
38
+ selected_service = service;
39
+ }
40
+ }
41
+ if (!selected_ram) {
42
+ let { ram } = await inquirer.prompt({
43
+ type: 'list',
44
+ message: 'Please select service ram:',
45
+ name: 'ram',
46
+ choices: [
47
+ { value: "0.5", name: "0.5G" },
48
+ { value: "1", name: "1G" },
49
+ { value: "2", name: "2G" },
50
+ { value: "4", name: "4G" },
51
+ { value: "6", name: "6G" },
52
+ { value: "8", name: "8G" },
53
+ ]
54
+ });
55
+ selected_ram = ram;
56
+ }
57
+ if (!selected_cpu) {
58
+ let { cpu } = await inquirer.prompt({
59
+ type: 'list',
60
+ message: 'Please select service cpu:',
61
+ name: 'cpu',
62
+ choices: [
63
+ { value: "0.5", name: "0.5 Core" },
64
+ { value: "1", name: "1 Core" },
65
+ { value: "2", name: "2 Core" },
66
+ { value: "3", name: "3 Core" },
67
+ { value: "4", name: "4 Core" },
68
+ ]
69
+ });
70
+ selected_cpu = cpu;
71
+ }
72
+ if (!selected_disk) {
73
+ let { disk } = await inquirer.prompt({
74
+ type: 'list',
75
+ message: 'Please select service disk:',
76
+ name: 'disk',
77
+ choices: [
78
+ { value: "5", name: "5G" },
79
+ { value: "10", name: "10G" },
80
+ { value: "15", name: "15G" },
81
+ { value: "30", name: "30G" },
82
+ { value: "40", name: "40G" },
83
+ { value: "50", name: "50G" },
84
+ ]
85
+ });
86
+ selected_disk = disk;
87
+ }
88
+ let wrong_value = false;
89
+ if (selected_ram % 0.5 != 0) {
90
+ cli.log(`${chalk.red('[Error]')} ram amount should coefficient of 0.5`);
91
+ wrong_value = true;
92
+ }
93
+ if (selected_cpu % 0.5 != 0) {
94
+ cli.log(`${chalk.red('[Error]')} cpu amount should coefficient of 0.5`);
95
+ wrong_value = true;
96
+ }
97
+ if (selected_disk % 5 != 0) {
98
+ cli.log(`${chalk.red('[Error]')} disk amount should coefficient of 5`);
99
+ wrong_value = true;
100
+ }
101
+ if (selected_service && selected_ram && selected_cpu && selected_disk && !wrong_value) {
102
+ await this.send_request(cli, selected_service, selected_ram, selected_cpu, selected_disk);
103
+ }
104
+ }
105
+ async send_request(cli, selected_service, ram, cpu, disk) {
106
+ try {
107
+ if (selected_service) {
108
+ const { data } = await axios.post("services/" + selected_service + "/resize/", {
109
+ "ram": ram,
110
+ "cpu": cpu,
111
+ "disk": disk,
112
+ }, this.axiosConfig);
113
+ if (data.success) {
114
+ cli.log(`${chalk.green('[Success]')} service resized successfully.`);
115
+ }
116
+ else {
117
+ cli.log(`${chalk.red('[Error]')} some problem in resize.`);
118
+ }
119
+ }
120
+ }
121
+ catch (e) {
122
+ if (e.response.status == 404) {
123
+ cli.log(chalk.blue("Selected Service Not Founded."));
124
+ }
125
+ else {
126
+ console.log(e.data);
127
+ }
128
+ }
129
+ }
130
+ }
@@ -0,0 +1,10 @@
1
+ import Command from "../../base.js";
2
+ export default class ServiceRestart extends Command {
3
+ static description: string;
4
+ static flags: {
5
+ service: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces/parser.js").CustomOptions>;
6
+ help: import("@oclif/core/lib/interfaces/parser.js").BooleanFlag<void>;
7
+ };
8
+ run(): Promise<void>;
9
+ send_request(cli: any, selected_service: any): Promise<void>;
10
+ }
@@ -0,0 +1,57 @@
1
+ import { Flags } from "@oclif/core";
2
+ import Command from "../../base.js";
3
+ import { isEmptyObject } from "../../helper.js";
4
+ import chalk from "chalk";
5
+ import inquirer from "inquirer";
6
+ import axios from "axios";
7
+ export default class ServiceRestart extends Command {
8
+ static description = "restart a service";
9
+ static flags = {
10
+ ...Command.flags,
11
+ service: Flags.string({ char: "s", description: "service name" }),
12
+ };
13
+ async run() {
14
+ const { args, flags } = await this.parse(ServiceRestart);
15
+ const cli = this;
16
+ await this.init_run();
17
+ const config_json = await this.read_config();
18
+ let selected_service = flags.service;
19
+ if (isEmptyObject(config_json.users)) {
20
+ cli.log(`${chalk.red("[Error]")} first you should login!`);
21
+ return;
22
+ }
23
+ if (!flags.service) {
24
+ let all_services = await this.get_services({
25
+ not_status: "pending",
26
+ });
27
+ if (all_services) {
28
+ let { service } = await inquirer.prompt({
29
+ type: "list",
30
+ message: "Please select a service:",
31
+ name: "service",
32
+ choices: all_services,
33
+ });
34
+ selected_service = service;
35
+ }
36
+ }
37
+ await this.send_request(cli, selected_service);
38
+ }
39
+ async send_request(cli, selected_service) {
40
+ try {
41
+ if (selected_service) {
42
+ const { data } = await axios.get("services/" + selected_service + "/restart/", this.axiosConfig);
43
+ if (data.success) {
44
+ cli.log(`${chalk.green("[Success]")} service restarted successfully.`);
45
+ }
46
+ }
47
+ }
48
+ catch (e) {
49
+ if (e.response.status == 404) {
50
+ cli.log(chalk.blue("Selected Service Not Founded."));
51
+ }
52
+ else {
53
+ console.log(e.data);
54
+ }
55
+ }
56
+ }
57
+ }
@@ -0,0 +1,10 @@
1
+ import Command from "../../base.js";
2
+ export default class ServiceStart extends Command {
3
+ static description: string;
4
+ static flags: {
5
+ service: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces/parser.js").CustomOptions>;
6
+ help: import("@oclif/core/lib/interfaces/parser.js").BooleanFlag<void>;
7
+ };
8
+ run(): Promise<void>;
9
+ send_request(cli: any, selected_service: any): Promise<void>;
10
+ }
@@ -0,0 +1,55 @@
1
+ import { Flags } from '@oclif/core';
2
+ import Command from "../../base.js";
3
+ import { isEmptyObject } from "../../helper.js";
4
+ import chalk from "chalk";
5
+ import inquirer from 'inquirer';
6
+ import axios from "axios";
7
+ export default class ServiceStart extends Command {
8
+ static description = 'start a service';
9
+ static flags = {
10
+ ...Command.flags,
11
+ service: Flags.string({ char: 's', description: 'service name' }),
12
+ };
13
+ async run() {
14
+ const { args, flags } = await this.parse(ServiceStart);
15
+ const cli = this;
16
+ await this.init_run();
17
+ const config_json = await this.read_config();
18
+ let selected_service = flags.service;
19
+ if (isEmptyObject(config_json.users)) {
20
+ cli.log(`${chalk.red('[Error]')} first you should login!`);
21
+ return;
22
+ }
23
+ if (!flags.service) {
24
+ let all_services = await this.get_services({ 'not_status': 'pending' });
25
+ if (all_services) {
26
+ let { service } = await inquirer.prompt({
27
+ type: 'list',
28
+ message: 'Please select a service:',
29
+ name: 'service',
30
+ choices: all_services
31
+ });
32
+ selected_service = service;
33
+ }
34
+ }
35
+ await this.send_request(cli, selected_service);
36
+ }
37
+ async send_request(cli, selected_service) {
38
+ try {
39
+ if (selected_service) {
40
+ const { data } = await axios.get("services/" + selected_service + "/start/", this.axiosConfig);
41
+ if (data.success) {
42
+ cli.log(`${chalk.green('[Success]')} service start successfully.`);
43
+ }
44
+ }
45
+ }
46
+ catch (e) {
47
+ if (e.response.status == 404) {
48
+ cli.log(chalk.blue("Selected Service Not Founded."));
49
+ }
50
+ else {
51
+ console.log(e.data);
52
+ }
53
+ }
54
+ }
55
+ }
@@ -0,0 +1,10 @@
1
+ import Command from "../../base.js";
2
+ export default class ServiceStop extends Command {
3
+ static description: string;
4
+ static flags: {
5
+ service: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces/parser.js").CustomOptions>;
6
+ help: import("@oclif/core/lib/interfaces/parser.js").BooleanFlag<void>;
7
+ };
8
+ run(): Promise<void>;
9
+ send_request(cli: any, selected_service: any): Promise<void>;
10
+ }
@@ -0,0 +1,55 @@
1
+ import { Flags } from '@oclif/core';
2
+ import Command from "../../base.js";
3
+ import { isEmptyObject } from "../../helper.js";
4
+ import chalk from "chalk";
5
+ import inquirer from 'inquirer';
6
+ import axios from "axios";
7
+ export default class ServiceStop extends Command {
8
+ static description = 'stop a service';
9
+ static flags = {
10
+ ...Command.flags,
11
+ service: Flags.string({ char: 's', description: 'service name' }),
12
+ };
13
+ async run() {
14
+ const { args, flags } = await this.parse(ServiceStop);
15
+ const cli = this;
16
+ await this.init_run();
17
+ const config_json = await this.read_config();
18
+ let selected_service = flags.service;
19
+ if (isEmptyObject(config_json.users)) {
20
+ cli.log(`${chalk.red('[Error]')} first you should login!`);
21
+ return;
22
+ }
23
+ if (!flags.service) {
24
+ let all_services = await this.get_services({ 'not_status': 'pending' });
25
+ if (all_services) {
26
+ let { service } = await inquirer.prompt({
27
+ type: 'list',
28
+ message: 'Please select a service:',
29
+ name: 'service',
30
+ choices: all_services
31
+ });
32
+ selected_service = service;
33
+ }
34
+ }
35
+ await this.send_request(cli, selected_service);
36
+ }
37
+ async send_request(cli, selected_service) {
38
+ try {
39
+ if (selected_service) {
40
+ const { data } = await axios.get("services/" + selected_service + "/stop/", this.axiosConfig);
41
+ if (data.success) {
42
+ cli.log(`${chalk.green('[Success]')} service stop successfully.`);
43
+ }
44
+ }
45
+ }
46
+ catch (e) {
47
+ if (e.response.status == 404) {
48
+ cli.log(chalk.blue("Selected Service Not Founded."));
49
+ }
50
+ else {
51
+ console.log(e.data);
52
+ }
53
+ }
54
+ }
55
+ }
@@ -0,0 +1,2 @@
1
+ export declare const GLOBAL_CONF_PATH: string;
2
+ export declare let BASE_API_URL: string;
@@ -0,0 +1,7 @@
1
+ import * as path from 'path';
2
+ import * as os from 'os';
3
+ export const GLOBAL_CONF_PATH = path.join(os.homedir(), '.chabok.json');
4
+ export let BASE_API_URL = 'https://apihub.chabokan.net/fa/api/v1/';
5
+ if (process.env.CHABOK_API_URL) {
6
+ BASE_API_URL = process.env.CHABOK_API_URL;
7
+ }
@@ -0,0 +1,12 @@
1
+ import { Ignore } from 'ignore';
2
+ export declare function isObject(obj: any): boolean;
3
+ export declare function isEmptyObject(obj: any): any;
4
+ export declare function read_config_file(): {
5
+ users: {};
6
+ default_user: string;
7
+ };
8
+ export declare function get_all_services(filter: {} | undefined, axiosConfig: any): Promise<any>;
9
+ export declare function trimLines(lines: string[]): string[];
10
+ export declare const loadIgnoreFile: (ignoreInstance: Ignore, ignoreFilePath: string, projectPath: string) => void;
11
+ export declare function addIgnorePatterns(ignoreInstance: Ignore, projectPath: string, dir: string): void;
12
+ export declare const checkUpdate: (version: any) => Promise<void>;