Skip to content

Commit f64bbc3

Browse files
authored
Merge pull request #3 from kidylee/main
feat: add env pull/push commands
2 parents 32c57c8 + 05fadbe commit f64bbc3

File tree

2 files changed

+213
-0
lines changed

2 files changed

+213
-0
lines changed

src/commands/env/pull.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import {Args, Command, Flags} from '@oclif/core'
2+
import {readAuthConfig} from "../../utils/utils.js";
3+
import chalk from "chalk";
4+
import {getProject, getProjects} from "../../utils/shared.js";
5+
import inquirer from "inquirer";
6+
import {Answers} from "../app/create.js";
7+
import fs from 'fs';
8+
9+
export default class EnvPull extends Command {
10+
static override args = {
11+
file: Args.string({description: 'write to file', required: true}),
12+
}
13+
14+
static override description = 'Store remote environment variables in local'
15+
16+
static override examples = [
17+
'<%= config.bin %> <%= command.id %> .env.stage.local',
18+
]
19+
20+
static override flags = {}
21+
22+
public async run(): Promise<void> {
23+
const {args} = await this.parse(EnvPull)
24+
25+
if (fs.existsSync(args.file)) {
26+
const {override} = await inquirer.prompt<any>([
27+
{
28+
message: `Do you want to override ${args.file} file?`,
29+
name: "override",
30+
default: false,
31+
type: "confirm",
32+
},
33+
]);
34+
if (!override) {
35+
return
36+
}
37+
}
38+
const auth = await readAuthConfig(this);
39+
console.log(chalk.blue.bold("\n Listing all Projects \n"));
40+
41+
const projects = await getProjects(auth, this);
42+
const {project} = await inquirer.prompt<Answers>([
43+
{
44+
choices: projects.map((project) => ({
45+
name: project.name,
46+
value: project,
47+
})),
48+
message: "Select the project:",
49+
name: "project",
50+
type: "list",
51+
},
52+
]);
53+
const projectId = project.projectId;
54+
const projectSelected = await getProject(projectId, auth, this);
55+
56+
const choices = [
57+
...projectSelected.applications.map((app: any) => ({
58+
name: `${app.name} (Application)`,
59+
value: app.env,
60+
})),
61+
...projectSelected.compose.map((compose: any) => ({
62+
name: `${compose.name} (Compose)`,
63+
value: compose.env,
64+
})),
65+
]
66+
const {env} = await inquirer.prompt<any>([
67+
{
68+
choices,
69+
message: "Select a service to pull the environment variables:",
70+
name: "env",
71+
type: "list",
72+
},
73+
74+
]);
75+
76+
77+
fs.writeFileSync(args.file, env || "")
78+
this.log(chalk.green("Environment variable write to file successful."));
79+
80+
81+
}
82+
}

src/commands/env/push.ts

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import {Args, Command, Flags} from '@oclif/core'
2+
import fs from "fs";
3+
import chalk from "chalk";
4+
import inquirer from "inquirer";
5+
import {readAuthConfig} from "../../utils/utils.js";
6+
import {getProject, getProjects} from "../../utils/shared.js";
7+
import {Answers} from "../app/create.js";
8+
import axios from "axios";
9+
10+
export default class EnvPush extends Command {
11+
static override args = {
12+
file: Args.string({description: '.env file to push', required: true}),
13+
}
14+
15+
static override description = 'Push dotenv file to remote service'
16+
17+
static override examples = [
18+
'<%= config.bin %> <%= command.id %> .env.stage.local',
19+
]
20+
21+
static override flags = {}
22+
23+
public async run(): Promise<void> {
24+
const {args, flags} = await this.parse(EnvPush)
25+
26+
if (!fs.existsSync(args.file)) {
27+
console.log(chalk.red.bold(`\n File ${args.file} doesn't exists \n`));
28+
return;
29+
}
30+
31+
const {override} = await inquirer.prompt<any>([
32+
{
33+
message: `This command will override entire remote environment variables. Do you want to continue?`,
34+
name: "override",
35+
default: false,
36+
type: "confirm",
37+
},
38+
]);
39+
if (!override) {
40+
return
41+
}
42+
43+
const fileContent = fs.readFileSync(args.file, 'utf-8');
44+
const auth = await readAuthConfig(this);
45+
console.log(chalk.blue.bold("\n Listing all Projects \n"));
46+
47+
const projects = await getProjects(auth, this);
48+
const {project} = await inquirer.prompt<Answers>([
49+
{
50+
choices: projects.map((project) => ({
51+
name: project.name,
52+
value: project,
53+
})),
54+
message: "Select the project:",
55+
name: "project",
56+
type: "list",
57+
},
58+
]);
59+
const projectId = project.projectId;
60+
const projectSelected = await getProject(projectId, auth, this);
61+
62+
const choices = [
63+
...projectSelected.applications.map((app: any) => ({
64+
name: `${app.name} (Application)`,
65+
value: {serviceType: 'app', service: app},
66+
})),
67+
...projectSelected.compose.map((compose: any) => ({
68+
name: `${compose.name} (Compose)`,
69+
value: {serviceType: 'compose', service: compose}
70+
})),
71+
]
72+
const {result: {serviceType, service}} = await inquirer.prompt<any>([
73+
{
74+
choices,
75+
message: "Select a service to pull the environment variables:",
76+
name: "result",
77+
type: "list",
78+
},
79+
80+
]);
81+
82+
if (serviceType === 'app') {
83+
const {applicationId} = service;
84+
const response = await axios.post(
85+
`${auth.url}/api/trpc/application.update`,
86+
{
87+
json: {
88+
applicationId,
89+
env: fileContent
90+
}
91+
}, {
92+
93+
headers: {
94+
Authorization: `Bearer ${auth.token}`,
95+
"Content-Type": "application/json",
96+
},
97+
}
98+
)
99+
if (response.status !== 200) {
100+
this.error(chalk.red("Error stopping application"));
101+
}
102+
this.log(chalk.green("Environment variable push successful."));
103+
104+
}
105+
106+
if (serviceType === 'compose') {
107+
const {composeId} = service;
108+
const response = await axios.post(
109+
`${auth.url}/api/trpc/compose.update`,
110+
{
111+
json: {
112+
composeId,
113+
env: fileContent
114+
}
115+
}, {
116+
headers: {
117+
Authorization: `Bearer ${auth.token}`,
118+
"Content-Type": "application/json",
119+
},
120+
}
121+
)
122+
if (response.status !== 200) {
123+
this.error(chalk.red("Error stopping application"));
124+
}
125+
this.log(chalk.green("Environment variable push successful."));
126+
127+
}
128+
129+
130+
}
131+
}

0 commit comments

Comments
 (0)