Skip to content

Commit

Permalink
change flags, separate commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy Razon committed Nov 23, 2023
1 parent 5956411 commit 9f43e9a
Show file tree
Hide file tree
Showing 40 changed files with 928 additions and 567 deletions.
1 change: 1 addition & 0 deletions packages/cli-common/src/hooks/init/load-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const initHook: OclifHook<'init'> = async function hook({ config, id, arg
async () => await localComposeClient({
composeFiles,
projectName: flags.project,
projectDirectory: process.cwd(),
}).getModelOrError(),
{
text: `Loading compose file${composeFiles.length > 1 ? 's' : ''}: ${composeFiles.join(', ')}`,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export * from './lib/plugins/model'
export * as text from './lib/text'
export { HookName, HookFunc, HooksListeners, Hooks } from './lib/hooks'
export { PluginContext, PluginInitContext } from './lib/plugins/context'
export { composeFlags, envIdFlags, tunnelServerFlags, urlFlags } from './lib/common-flags'
export { composeFlags, envIdFlags, tunnelServerFlags, urlFlags, buildFlags, parseBuildFlags, tableFlags } from './lib/common-flags'
export { formatFlagsToArgs } from './lib/flags'
export { initHook } from './hooks/init/load-plugins'
export { default as BaseCommand } from './commands/base-command'
76 changes: 76 additions & 0 deletions packages/cli-common/src/lib/common-flags/build-flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Flags } from '@oclif/core'
import { InferredFlags } from '@oclif/core/lib/interfaces'
import { BuildSpec, parseRegistry } from '@preevy/core'

const helpGroup = 'BUILD'

export const buildFlags = {
'no-build': Flags.boolean({
description: 'Do not build images',
helpGroup,
allowNo: false,
default: false,
required: false,
}),
registry: Flags.string({
description: 'Image registry. If this flag is specified, the "build-context" flag defaults to "*local"',
helpGroup,
required: false,
}),
'registry-single-name': Flags.string({
description: 'Use single name for image registry, ECR-style. Default: auto-detect from "registry" flag',
helpGroup,
required: false,
dependsOn: ['registry'],
}),
'no-registry-single-name': Flags.boolean({
description: 'Disable auto-detection for ECR-style registry single name',
helpGroup,
allowNo: false,
required: false,
exclusive: ['registry-single-name'],
}),
'no-registry-cache': Flags.boolean({
description: 'Do not add the registry as a cache source and target',
helpGroup,
required: false,
dependsOn: ['registry'],
}),
load: Flags.boolean({
description: 'Load build results to the Docker server at the deployment target',
helpGroup,
required: false,
}),
builder: Flags.string({
description: 'Builder to use',
helpGroup,
required: false,
}),
'no-cache': Flags.boolean({
description: 'Do not use cache when building the images',
helpGroup,
allowNo: false,
required: false,
}),
} as const

export const parseBuildFlags = (flags: Omit<InferredFlags<typeof buildFlags>, 'json'>): BuildSpec | undefined => {
if (flags['no-build']) {
return undefined
}

return {
builder: flags.builder,
noCache: flags['no-cache'],
cacheFromRegistry: !flags['no-registry-cache'],
...flags.registry
? {
registry: parseRegistry({
registry: flags.registry,
singleName: flags['no-registry-single-name'] ? false : flags['registry-single-name'],
}),
load: flags.load,
}
: { load: true },
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { Flags } from '@oclif/core'
import { Flags, ux } from '@oclif/core'
import { mapValues } from 'lodash'
import { EOL } from 'os'

export * from './build-flags'

export const tableFlags = mapValues(ux.table.flags(), f => ({ ...f, helpGroup: 'OUTPUT' }))

const projectFlag = {
project: Flags.string({
char: 'p',
description: 'Project name. Defaults to the Compose project name',
summary: 'Project name. Defaults to the Compose project name',
required: false,
helpGroup: 'GLOBAL',
}),
}
export const composeFlags = {
file: Flags.string({
description: 'Compose configuration file',
summary: 'Compose configuration file',
multiple: true,
delimiter: ',',
singleValue: true,
Expand All @@ -20,7 +26,7 @@ export const composeFlags = {
helpGroup: 'GLOBAL',
}),
'system-compose-file': Flags.string({
description: 'Add extra Compose configuration file without overriding the defaults',
summary: 'Add extra Compose configuration file without overriding the defaults',
multiple: true,
delimiter: ',',
singleValue: true,
Expand All @@ -33,38 +39,40 @@ export const composeFlags = {

export const envIdFlags = {
id: Flags.string({
description: 'Environment id - affects created URLs. If not specified, will try to detect automatically',
summary: 'Environment id',
description: `Affects created URLs${EOL}If not specified, will detect from the current Git context`,
required: false,
}),
...projectFlag,
} as const

export const tunnelServerFlags = {
'tunnel-url': Flags.string({
description: 'Tunnel url, specify ssh://hostname[:port] or ssh+tls://hostname[:port]',
summary: 'Tunnel url, specify ssh://hostname[:port] or ssh+tls://hostname[:port]',
char: 't',
default: 'ssh+tls://livecycle.run' ?? process.env.PREVIEW_TUNNEL_OVERRIDE,
}),
'tls-hostname': Flags.string({
description: 'Override TLS server name when tunneling via HTTPS',
summary: 'Override TLS server name when tunneling via HTTPS',
required: false,
}),
'insecure-skip-verify': Flags.boolean({
description: 'Skip TLS or SSH certificate verification',
summary: 'Skip TLS or SSH certificate verification',
default: false,
}),
} as const

export const urlFlags = {
'include-access-credentials': Flags.boolean({
description: 'Include access credentials for basic auth for each service URL',
summary: 'Include access credentials for basic auth for each service URL',
default: false,
}),
'show-preevy-service-urls': Flags.boolean({
description: 'Show URLs for internal Preevy services',
summary: 'Show URLs for internal Preevy services',
default: false,
}),
'access-credentials-type': Flags.custom<'browser' | 'api'>({
summary: 'Access credentials type',
options: ['api', 'browser'],
dependsOn: ['include-access-credentials'],
default: 'browser',
Expand Down
34 changes: 16 additions & 18 deletions packages/cli/src/commands/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import yaml from 'yaml'
import { Args, Flags, Interfaces } from '@oclif/core'
import {
addBaseComposeTunnelAgentService,
localComposeClient, wrapWithDockerSocket, findEnvId, MachineConnection, ComposeModel, remoteUserModel,
localComposeClient, findEnvId, MachineConnection, ComposeModel, remoteUserModel, dockerEnvContext,
} from '@preevy/core'
import { COMPOSE_TUNNEL_AGENT_SERVICE_NAME } from '@preevy/common'
import DriverCommand from '../driver-command'
Expand Down Expand Up @@ -98,23 +98,21 @@ export default class Logs extends DriverCommand<typeof Logs> {
connection = await this.connect(envId)
}

try {
const compose = localComposeClient({
composeFiles: Buffer.from(yaml.stringify(addBaseComposeTunnelAgentService(userModel))),
projectName: flags.project,
})
const compose = localComposeClient({
composeFiles: Buffer.from(yaml.stringify(addBaseComposeTunnelAgentService(userModel))),
projectName: flags.project,
projectDirectory: process.cwd(),
})

const withDockerSocket = wrapWithDockerSocket({ connection, log })
await withDockerSocket(() => compose.spawnPromise(
[
'logs',
...serializeDockerComposeLogsFlags(flags),
...validateServices(restArgs, userModel),
],
{ stdio: 'inherit' },
))
} finally {
await connection.close()
}
await using dockerContext = await dockerEnvContext({ connection, log })

await compose.spawnPromise(
[
'logs',
...serializeDockerComposeLogsFlags(flags),
...validateServices(restArgs, userModel),
],
{ stdio: 'inherit', env: dockerContext.env },
)
}
}
4 changes: 2 additions & 2 deletions packages/cli/src/commands/proxy/connect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ux, Args, Flags } from '@oclif/core'
import { jwkThumbprint, commands, profileStore, withSpinner, SshConnection, machineId, validateEnvId, normalizeEnvId, EnvId } from '@preevy/core'
import { tunnelServerFlags, urlFlags } from '@preevy/cli-common'
import { text, tunnelServerFlags, urlFlags } from '@preevy/cli-common'
import { inspect } from 'util'
import { formatPublicKey } from '@preevy/common'
import { spawn } from 'child_process'
Expand Down Expand Up @@ -69,7 +69,7 @@ export default class Connect extends ProfileCommand<typeof Connect> {
} else {
const deviceId = (await machineId(this.config.dataDir)).substring(0, 2)
envId = normalizeEnvId(`${composeProject}-dev-${deviceId}`)
this.logger.info(`Using environment ID ${envId}, based on Docker Compose and local device`)
this.logger.info(`Using environment ID ${text.code(envId)}, based on Docker Compose and local device`)
}
let client: SshConnection['client'] | undefined
let hostKey: Buffer
Expand Down
Loading

0 comments on commit 9f43e9a

Please sign in to comment.