Skip to content

Commit

Permalink
preevy login at init (#293)
Browse files Browse the repository at this point in the history
* add optional login to init command
* workaround for local profile deletion
* unified text formatting
  • Loading branch information
Roy Razon authored Oct 22, 2023
1 parent 13442fb commit 4ab73c6
Show file tree
Hide file tree
Showing 24 changed files with 561 additions and 321 deletions.
2 changes: 1 addition & 1 deletion packages/cli-common/src/commands/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from '@preevy/core'
import { asyncReduce } from 'iter-tools-es'
import { commandLogger } from '../lib/log'
import { composeFlags } from '../lib/flags'
import { composeFlags } from '../lib/common-flags'

// eslint-disable-next-line no-use-before-define
export type Flags<T extends typeof Command> = Interfaces.InferredFlags<typeof BaseCommand['baseFlags'] & T['flags']>
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-common/src/hooks/init/load-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Hook as OclifHook, Command, Flags } from '@oclif/core'
import { Parser } from '@oclif/core/lib/parser/parse'
import { Config, Topic } from '@oclif/core/lib/interfaces'
import { localComposeClient, ComposeModel, resolveComposeFiles, withSpinner, NoComposeFilesError } from '@preevy/core'
import { composeFlags } from '../../lib/flags'
import { composeFlags } from '../../lib/common-flags'
import { addPluginFlags, loadPlugins, hooksFromPlugins, addPluginCommands } from '../../lib/plugins'

type InternalConfig = Config & {
Expand Down
5 changes: 3 additions & 2 deletions packages/cli-common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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/flags'
export { composeFlags, envIdFlags, tunnelServerFlags, urlFlags } from './lib/common-flags'
export { formatFlagsToArgs } from './lib/flags'
export { initHook } from './hooks/init/load-plugins'
export { default as BaseCommand } from './commands/base-command'
export * as utils from './utils'
69 changes: 69 additions & 0 deletions packages/cli-common/src/lib/common-flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Flags } from '@oclif/core'

const projectFlag = {
project: Flags.string({
char: 'p',
description: 'Project name. Defaults to the Compose project name',
required: false,
helpGroup: 'GLOBAL',
}),
}
export const composeFlags = {
file: Flags.string({
description: 'Compose configuration file',
multiple: true,
required: false,
char: 'f',
default: [],
helpGroup: 'GLOBAL',
}),
'system-compose-file': Flags.string({
description: 'Add extra Compose configuration file without overriding the defaults',
multiple: true,
required: false,
default: [],
helpGroup: 'GLOBAL',
}),
...projectFlag,
} as const

export const envIdFlags = {
id: Flags.string({
description: 'Environment id - affects created URLs. If not specified, will try to detect automatically',
required: false,
}),
...projectFlag,
} as const

export const tunnelServerFlags = {
'tunnel-url': Flags.string({
description: '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',
required: false,
}),
'insecure-skip-verify': Flags.boolean({
description: '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',
default: false,
}),
'show-preevy-service-urls': Flags.boolean({
description: 'Show URLs for internal Preevy services',
default: false,
}),
'access-credentials-type': Flags.custom<'browser' | 'api'>({
options: ['api', 'browser'],
dependsOn: ['include-access-credentials'],
default: 'browser',
required: true,
})(),
} as const
File renamed without changes.
90 changes: 23 additions & 67 deletions packages/cli-common/src/lib/flags.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,25 @@
import { Flags } from '@oclif/core'
import { Flag } from '@oclif/core/lib/interfaces'

const projectFlag = {
project: Flags.string({
char: 'p',
description: 'Project name. Defaults to the Compose project name',
required: false,
helpGroup: 'GLOBAL',
}),
}
export const composeFlags = {
file: Flags.string({
description: 'Compose configuration file',
multiple: true,
required: false,
char: 'f',
default: [],
helpGroup: 'GLOBAL',
}),
'system-compose-file': Flags.string({
description: 'Add extra Compose configuration file without overriding the defaults',
multiple: true,
required: false,
default: [],
helpGroup: 'GLOBAL',
}),
...projectFlag,
} as const

export const envIdFlags = {
id: Flags.string({
description: 'Environment id - affects created URLs. If not specified, will try to detect automatically',
required: false,
}),
...projectFlag,
} as const
type FlagSpec<T> =Pick<Flag<T>, 'type' | 'default'>

export const tunnelServerFlags = {
'tunnel-url': Flags.string({
description: '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',
required: false,
}),
'insecure-skip-verify': Flags.boolean({
description: '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',
default: false,
}),
'show-preevy-service-urls': Flags.boolean({
description: 'Show URLs for internal Preevy services',
default: false,
}),
'access-credentials-type': Flags.custom<'browser' | 'api'>({
options: ['api', 'browser'],
dependsOn: ['include-access-credentials'],
default: 'browser',
required: true,
})(),
} as const
export function formatFlagsToArgs(flags: Record<string, unknown>, spec: Record<string, FlagSpec<unknown>> = {}, prefix = '') {
return Object.entries(flags).flatMap(function format(this: void, [key, value]):string[] {
if (spec[key]?.default === value) {
return []
}
if (Array.isArray(value)) {
return value.flatMap(v => format([key, v]))
}
if (typeof value === 'boolean') {
const defaultIsOn = spec[key]?.type === 'boolean' && spec[key].default === true
if (!value && defaultIsOn) {
return [`--no-${key}`]
}
return [`--${key}`]
}
if (typeof value === 'object') {
return Object.entries(value ?? {}).flatMap(([k, v]) => format([`${key}-${k}`, v]))
}
return [`--${prefix}${key}`, `${value}`]
})
}
14 changes: 14 additions & 0 deletions packages/cli-common/src/lib/text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Config } from '@oclif/core'
import chalk from 'chalk'

export const code = (c: string) => chalk.bold(c)

export const codeList = (c: string[]) => c.map(code).join(', ')

export const command = ({ bin }: Pick<Config, 'bin'>, ...args: string[]) => code(`${bin} ${args.join(' ')}`)

export const highlight = (s: string) => chalk.greenBright(s)

export const success = (s: string) => chalk.greenBright(s)

export const recommendation = (s: string) => chalk.cyan(s)
26 changes: 0 additions & 26 deletions packages/cli-common/src/utils/flags.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/cli-common/src/utils/index.ts

This file was deleted.

32 changes: 32 additions & 0 deletions packages/cli/docs/login.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
`preevy login`
==============

Login to the Livecycle SaaS

* [`preevy login`](#preevy-login)

## `preevy login`

Login to the Livecycle SaaS

```
USAGE
$ preevy login [-D] [-f <value>] [--system-compose-file <value>] [-p <value>] [--lc-auth-url <value>]
[--lc-api-url <value>] [--lc-client-id <value>]
FLAGS
--lc-api-url=<value> [default: https://app.livecycle.run] The Livecycle API URL'
--lc-auth-url=<value> [default: https://auth.livecycle.dev] The login URL
--lc-client-id=<value> [default: BHXcVtapfKPEpZtYO3AJ2Livmz6j7xK0] The client ID for the OAuth app
GLOBAL FLAGS
-D, --debug Enable debug logging
-f, --file=<value>... [default: ] Compose configuration file
-p, --project=<value> Project name. Defaults to the Compose project name
--system-compose-file=<value>... [default: ] Add extra Compose configuration file without overriding the defaults
DESCRIPTION
Login to the Livecycle SaaS
```

_See code: [dist/commands/login.ts](https://github.com/livecycle/preevy/blob/v0.0.55/packages/cli/src/commands/login.ts)_
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"/oclif.manifest.json"
],
"dependencies": {
"@inquirer/confirm": "^2.0.14",
"@oclif/core": "^2",
"@oclif/plugin-help": "^5",
"@preevy/cli-common": "0.0.55",
Expand All @@ -29,7 +30,6 @@
"@preevy/driver-kube-pod": "0.0.55",
"@preevy/driver-lightsail": "0.0.55",
"@preevy/plugin-github-pr-link": "0.0.55",
"chalk": "^4.1.2",
"inquirer": "^8.0.0",
"iter-tools-es": "^7.5.3",
"lodash": "^4.17.21",
Expand Down
Loading

0 comments on commit 4ab73c6

Please sign in to comment.