From a9a018a80c0b67fdc44a3ad5b617747288f89179 Mon Sep 17 00:00:00 2001 From: Roy Razon Date: Wed, 3 Apr 2024 09:34:03 +0300 Subject: [PATCH] add env-id command --- packages/cli/src/commands/env-id.ts | 37 +++++++++++++++++++++++++++++ packages/core/src/env-id.ts | 12 ++++++---- 2 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 packages/cli/src/commands/env-id.ts diff --git a/packages/cli/src/commands/env-id.ts b/packages/cli/src/commands/env-id.ts new file mode 100644 index 00000000..8a135c78 --- /dev/null +++ b/packages/cli/src/commands/env-id.ts @@ -0,0 +1,37 @@ +import { ux } from '@oclif/core' +import { findEnvId } from '@preevy/core' +import ProfileCommand from '../profile-command.js' +import { envIdFlags } from '../common-flags.js' + +// eslint-disable-next-line no-use-before-define +export default class EnvId extends ProfileCommand { + static description = 'Show the Preevy environment ID for the current Compose project' + + static flags = { + ...envIdFlags, + } + + static enableJsonFlag = true + + static args = {} + + async run(): Promise { + const log = this.logger + const { flags, args } = this + + const envId = await findEnvId({ + userSpecifiedEnvId: flags.id, + userSpecifiedProjectName: flags.project, + userModel: () => this.ensureUserModel(), + log, + explanationLogLevel: 'debug', + }) + + if (this.jsonEnabled()) { + return envId + } + + ux.log(envId) + return undefined + } +} diff --git a/packages/core/src/env-id.ts b/packages/core/src/env-id.ts index a1f84caf..553df564 100644 --- a/packages/core/src/env-id.ts +++ b/packages/core/src/env-id.ts @@ -1,7 +1,7 @@ import { detectCiProvider } from './ci-providers/index.js' import { gitContext } from './git.js' import { ComposeModel } from './compose/model.js' -import { Logger } from './log.js' +import { LogLevel, Logger } from './log.js' export type EnvId = string & { __tag: 'EnvId' @@ -74,8 +74,9 @@ export const findProjectName = async ({ userSpecifiedProjectName, userModel }: { } } -export const findEnvIdByProjectName = async ({ log, projectName, projectNameBasedOn }: { +export const findEnvIdByProjectName = async ({ log, explanationLogLevel = 'info', projectName, projectNameBasedOn }: { log: Logger + explanationLogLevel?: LogLevel projectName: string projectNameBasedOn?: string }) => { @@ -86,12 +87,13 @@ export const findEnvIdByProjectName = async ({ log, projectName, projectNameBase basedOn, ].join(' and ') - log.info(`Using environment ID ${envId}, based on ${envIdBaseOn}`) + log[explanationLogLevel](`Using environment ID ${envId}, based on ${envIdBaseOn}`) return envId as EnvId } -export async function findEnvId({ log, userSpecifiedEnvId, userSpecifiedProjectName, userModel }: { +export async function findEnvId({ log, explanationLogLevel = 'info', userSpecifiedEnvId, userSpecifiedProjectName, userModel }: { log: Logger + explanationLogLevel?: LogLevel userSpecifiedEnvId: string | undefined userSpecifiedProjectName: string | undefined userModel: ComposeModel | (() => Promise) @@ -105,5 +107,5 @@ export async function findEnvId({ log, userSpecifiedEnvId, userSpecifiedProjectN ? { projectName: userSpecifiedProjectName, projectNameBasedOn: undefined } : await findProjectName({ userSpecifiedProjectName, userModel }) - return await findEnvIdByProjectName({ log, projectName, projectNameBasedOn }) + return await findEnvIdByProjectName({ log, explanationLogLevel, projectName, projectNameBasedOn }) }