Skip to content

Commit

Permalink
add env-id command
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy Razon committed Apr 3, 2024
1 parent 9cbcaaa commit 447d80f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
37 changes: 37 additions & 0 deletions packages/cli/src/commands/env-id.ts
Original file line number Diff line number Diff line change
@@ -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<typeof EnvId> {
static description = 'Show the Preevy environment ID for the current Compose project'

static flags = {
...envIdFlags,
}

static enableJsonFlag = true

static args = {}

async run(): Promise<unknown> {
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
}
}
12 changes: 7 additions & 5 deletions packages/core/src/env-id.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
}) => {
Expand All @@ -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<ComposeModel>)
Expand All @@ -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 })
}

0 comments on commit 447d80f

Please sign in to comment.