|
| 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