Skip to content

Commit

Permalink
add --output-urls-to flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy Razon committed Nov 23, 2023
1 parent 2f1549f commit 00e10e9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/cli-common/src/lib/common-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,8 @@ export const urlFlags = {
default: 'browser',
required: true,
})(),
'output-urls-to': Flags.file({
description: 'Output URLs to file',
required: false,
}),
} as const
4 changes: 3 additions & 1 deletion packages/cli/src/commands/proxy/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { formatPublicKey } from '@preevy/common'
import { spawn } from 'child_process'
import { connectToTunnelServerSsh } from '../../tunnel-server-client'
import ProfileCommand from '../../profile-command'
import { filterUrls, printUrls } from '../urls'
import { filterUrls, printUrls, writeUrlsToFile } from '../urls'

// eslint-disable-next-line no-use-before-define
export default class Connect extends ProfileCommand<typeof Connect> {
Expand Down Expand Up @@ -141,6 +141,8 @@ export default class Connect extends ProfileCommand<typeof Connect> {
filters: this.config.preevyHooks.filterUrls,
})

await writeUrlsToFile({ log: this.logger }, flags, urls)

if (flags.json) {
return urls
}
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/commands/up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { inspect } from 'util'
import { editUrl, tunnelNameResolver } from '@preevy/common'
import MachineCreationDriverCommand from '../machine-creation-driver-command'
import { envIdFlags, urlFlags } from '../common-flags'
import { filterUrls, printUrls } from './urls'
import { filterUrls, printUrls, writeUrlsToFile } from './urls'
import { connectToTunnelServerSsh } from '../tunnel-server-client'

// eslint-disable-next-line no-use-before-define
Expand Down Expand Up @@ -175,6 +175,8 @@ export default class Up extends MachineCreationDriverCommand<typeof Up> {
)),
)

await writeUrlsToFile({ log: this.logger }, flags, urls)

if (flags.json) {
return urls
}
Expand Down
19 changes: 17 additions & 2 deletions packages/cli/src/commands/urls.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import fs from 'fs'
import yaml from 'yaml'
import { Args, ux, Interfaces } from '@oclif/core'
import { FlatTunnel, ProfileStore, TunnelOpts, addBaseComposeTunnelAgentService, commands, findComposeTunnelAgentUrl, findEnvId, getTunnelNamesToServicePorts, profileStore } from '@preevy/core'
import { HooksListeners, PluginContext, tunnelServerFlags } from '@preevy/cli-common'
import { FlatTunnel, Logger, ProfileStore, TunnelOpts, addBaseComposeTunnelAgentService, commands, findComposeTunnelAgentUrl, findEnvId, getTunnelNamesToServicePorts, profileStore } from '@preevy/core'
import { HooksListeners, PluginContext, text, tunnelServerFlags } from '@preevy/cli-common'
import { asyncReduce } from 'iter-tools-es'
import { tunnelNameResolver } from '@preevy/common'
import { connectToTunnelServerSsh } from '../tunnel-server-client'
import ProfileCommand from '../profile-command'
import { envIdFlags, urlFlags } from '../common-flags'

export const writeUrlsToFile = async (
{ log }: { log: Logger },
{ 'output-urls-to': outputUrlsTo }: Interfaces.InferredFlags<Pick<typeof urlFlags, 'output-urls-to'>>,
urls: FlatTunnel[],
) => {
if (!outputUrlsTo) return
const contents = /\.ya?ml$/.test(outputUrlsTo) ? yaml.stringify(urls) : JSON.stringify(urls)
await fs.promises.writeFile(outputUrlsTo, contents, { encoding: 'utf8' })
log.info(`URLs written to file ${text.code(outputUrlsTo)}`)
}

export const printUrls = (
flatTunnels: FlatTunnel[],
flags: Interfaces.InferredFlags<typeof ux.table.Flags> & { 'include-access-credentials': boolean },
Expand Down Expand Up @@ -133,6 +146,8 @@ export default class Urls extends ProfileCommand<typeof Urls> {
filters: this.config.preevyHooks.filterUrls,
})

await writeUrlsToFile({ log: this.logger }, flags, urls)

if (flags.json) {
return urls
}
Expand Down

0 comments on commit 00e10e9

Please sign in to comment.