diff --git a/Makefile.base.mk b/Makefile.base.mk index e6fb7b50c..f5920a0a5 100644 --- a/Makefile.base.mk +++ b/Makefile.base.mk @@ -33,35 +33,7 @@ export HOST_UID # As such, default shell of the user has to be get the other way. USER_SHELL=$(shell env | grep '^SHELL=' | cut -d '=' -f 2) -up: - pnpm nx run --output-style=stream core:docker-compose:up -down: - pnpm nx run --output-style=stream core:docker-compose:down - -serve: - pnpm nx run --output-style=stream core:serve - -clean: - # remove created images - @docker-compose -p down --remove-orphans --rmi all 2>/dev/null \ - && echo 'Image(s) removed.' \ - || echo 'Image(s) already removed.' - -prune: - # clean all that is not actively used - docker system prune -af - - -set-env: -ifeq ($(ENV_STAGE), local) - $(error Please set ENV_STAGE to other value than "local") -endif -ifeq (, $(shell which aws-vault)) - $(USER_SHELL) -else - aws-vault exec $(AWS_VAULT_PROFILE) -- $(USER_SHELL) -endif aws-login: aws-vault login $(AWS_VAULT_PROFILE) diff --git a/packages/internal/cli/src/commands/get-env.ts b/packages/internal/cli/src/commands/aws/get-env.ts similarity index 62% rename from packages/internal/cli/src/commands/get-env.ts rename to packages/internal/cli/src/commands/aws/get-env.ts index 5bc420e82..e512f1394 100644 --- a/packages/internal/cli/src/commands/get-env.ts +++ b/packages/internal/cli/src/commands/aws/get-env.ts @@ -1,12 +1,10 @@ import { Command } from '@oclif/core'; - -import { getConfigStorage, getEnvStageKey } from '../config/storage'; -import { initConfig } from '../config/init'; +import { initConfig } from '../../config/init'; export default class GetEnv extends Command { static description = 'Get currently selected ENV stage'; - static examples = [`$ saas get-env`]; + static examples = [`$ saas aws get-env`]; async run(): Promise { const { envStage } = await initConfig(this, {}); diff --git a/packages/internal/cli/src/commands/aws/login.ts b/packages/internal/cli/src/commands/aws/login.ts new file mode 100644 index 000000000..0cef2129f --- /dev/null +++ b/packages/internal/cli/src/commands/aws/login.ts @@ -0,0 +1,18 @@ +import { Command } from '@oclif/core'; + +import { initConfig } from '../../config/init'; +import { runCommand } from '../../lib/runCommand'; +import { assertAwsVaultInstalled } from '../../lib/awsVault'; + +export default class GetEnv extends Command { + static description = 'Get currently selected ENV stage'; + + static examples = [`$ saas aws login`]; + + async run(): Promise { + await initConfig(this, { requireAws: true }); + await assertAwsVaultInstalled(); + + await runCommand('aws-vault', ['login']); + } +} diff --git a/packages/internal/cli/src/commands/set-env.ts b/packages/internal/cli/src/commands/aws/set-env.ts similarity index 82% rename from packages/internal/cli/src/commands/set-env.ts rename to packages/internal/cli/src/commands/aws/set-env.ts index 73838dd2c..9e98cce6b 100644 --- a/packages/internal/cli/src/commands/set-env.ts +++ b/packages/internal/cli/src/commands/aws/set-env.ts @@ -1,12 +1,12 @@ import { Args, Command } from '@oclif/core'; -import { setEnvStage } from '../config/storage'; -import { initConfig } from '../config/init'; +import { setEnvStage } from '../../config/storage'; +import { initConfig } from '../../config/init'; export default class SetEnv extends Command { static description = 'Select ENV stage'; - static examples = [`$ saas set-env qa`]; + static examples = [`$ saas aws set-env qa`]; static args = { envStage: Args.string({ diff --git a/packages/internal/cli/src/commands/backend/down.ts b/packages/internal/cli/src/commands/backend/down.ts new file mode 100644 index 000000000..3d148d633 --- /dev/null +++ b/packages/internal/cli/src/commands/backend/down.ts @@ -0,0 +1,18 @@ +import { Command } from '@oclif/core'; + +import { initConfig } from '../../config/init'; +import { runCommand } from '../../lib/runCommand'; +import { assertDockerIsRunning } from '../../lib/docker'; + +export default class BackendUp extends Command { + static description = 'Stops all backend services'; + + static examples = [`$ saas backend down`]; + + async run(): Promise { + await initConfig(this, { requireLocalEnvStage: true }); + await assertDockerIsRunning(); + + await runCommand('pnpm', ['nx', 'run', 'core:docker-compose:down']); + } +} diff --git a/packages/internal/cli/src/config/aws.ts b/packages/internal/cli/src/config/aws.ts index b34e553cc..e087c61d3 100644 --- a/packages/internal/cli/src/config/aws.ts +++ b/packages/internal/cli/src/config/aws.ts @@ -8,6 +8,8 @@ import { lookpath } from 'lookpath'; import { validateStageEnv } from './env'; import { color } from '@oclif/color'; +import { isAwsVaultInstalled } from '../lib/awsVault'; +import { assertChamberInstalled, isChamberInstalled } from '../lib/chamber'; const exec = promisify(childProcess.exec); @@ -21,12 +23,7 @@ async function loadStageEnv( envStage: string, shouldValidate = true ) { - const chamberExists = await lookpath('chamber'); - if (!chamberExists) { - context.error( - 'chamber executable missing. Make sure it is installed in the system and re-run the command.' - ); - } + await assertChamberInstalled(); let chamberOutput; try { @@ -56,8 +53,7 @@ export const initAWS = async ( context: Command, options: LoadAWSCredentialsOptions ) => { - const awsVaultExists = await lookpath('aws-vault'); - if (awsVaultExists) { + if (await isAwsVaultInstalled()) { const awsVaultProfile = process.env.AWS_VAULT_PROFILE; const { stdout } = await exec(`aws-vault export ${awsVaultProfile}`); diff --git a/packages/internal/cli/src/config/init.ts b/packages/internal/cli/src/config/init.ts index e3f0ac1ea..0e852d588 100644 --- a/packages/internal/cli/src/config/init.ts +++ b/packages/internal/cli/src/config/init.ts @@ -46,7 +46,7 @@ export const initConfig = async ( if (requireLocalEnvStage && envStage !== ENV_STAGE_LOCAL) { context.error( `This command should only be run on a local environment stage. -Please call \`saas set-env local\` first or open a new terminal.` +Please call \`saas aws set-env local\` first or open a new terminal.` ); } @@ -56,7 +56,7 @@ Please call \`saas set-env local\` first or open a new terminal.` if (envStage === ENV_STAGE_LOCAL) { context.error( `Remote environment stage required.\nPlease call \`${color.green( - 'saas set-env [stage-name]' + 'saas aws set-env [stage-name]' )}\` first. Do not use \`local\` value.` ); } diff --git a/packages/internal/cli/src/lib/awsVault.ts b/packages/internal/cli/src/lib/awsVault.ts new file mode 100644 index 000000000..597bfbd38 --- /dev/null +++ b/packages/internal/cli/src/lib/awsVault.ts @@ -0,0 +1,18 @@ +import { CLIError } from '@oclif/errors'; +import * as childProcess from 'child_process'; +import { promisify } from 'util'; +import { lookpath } from 'lookpath'; + +const exec = promisify(childProcess.exec); + +export const isAwsVaultInstalled = async () => { + return await lookpath('aws-vault'); +}; + +export const assertAwsVaultInstalled = async () => { + const isInstalled = await isAwsVaultInstalled(); + if (!isInstalled) { + throw new CLIError(`aws-vault executable not found. Make sure it is installed in the system and re-run the + command. Go to https://github.com/99designs/aws-vault and learn how to install and configure it.`); + } +}; diff --git a/packages/internal/cli/src/lib/chamber.ts b/packages/internal/cli/src/lib/chamber.ts new file mode 100644 index 000000000..fb7cffd46 --- /dev/null +++ b/packages/internal/cli/src/lib/chamber.ts @@ -0,0 +1,18 @@ +import { CLIError } from '@oclif/errors'; +import * as childProcess from 'child_process'; +import { promisify } from 'util'; +import { lookpath } from 'lookpath'; + +const exec = promisify(childProcess.exec); + +export const isChamberInstalled = async () => { + return await lookpath('chamber'); +}; + +export const assertChamberInstalled = async () => { + const isInstalled = await isChamberInstalled(); + if (!isInstalled) { + throw new CLIError(`chamber executable not found. Make sure it is installed in the system and re-run the + command. Go to https://github.com/segmentio/chamber and learn how to install and configure it.`); + } +}; diff --git a/packages/internal/core/project.json b/packages/internal/core/project.json index 06281558e..8bd1615a5 100644 --- a/packages/internal/core/project.json +++ b/packages/internal/core/project.json @@ -56,29 +56,12 @@ "commands": ["docker-compose down"] } }, - "serve": { - "executor": "nx:run-commands", - "options": { - "color": true, - "commands": ["nx run core:docker-compose:up", "nx run webapp:start"], - "parallel": false - } - }, "lint": { "executor": "@nx/linter:eslint", "outputs": ["{options.outputFile}"], "options": { "lintFilePatterns": ["packages/internal/core/**/*.ts"] } - }, - "set-env": { - "executor": "nx:run-commands", - "options": { - "color": true, - "commands": ["node ./scripts/set-env.js {args.env}"], - "cwd": "packages/internal/core", - "parallel": false - } } }, "tags": []