Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add env-id command #471

Merged
merged 1 commit into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 })
}
Loading