/
get.ts
61 lines (56 loc) · 2 KB
/
get.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { Flags } from '@oclif/core'
import inquirer from '../../ui/autocomplete'
import {
fetchEnvironments,
fetchEnvironmentByKey,
} from '../../api/environments'
import { EnvironmentPromptResult, environmentPrompt } from '../../ui/prompts'
import Base from '../base'
import { batchRequests } from '../../utils/batchRequests'
export default class DetailedEnvironments extends Base {
static hidden = false
static description = 'Retrieve Environments from the management API'
static examples = [
'<%= config.bin %> <%= command.id %>',
'<%= config.bin %> <%= command.id %> --keys=environment-one,environment-two',
]
static flags = {
...Base.flags,
keys: Flags.string({
description:
'Comma-separated list of environment keys to fetch details for',
}),
}
authRequired = true
public async run(): Promise<void> {
const { flags } = await this.parse(DetailedEnvironments)
const keys = flags['keys']?.split(',')
const { headless, project } = flags
await this.requireProject(project, headless)
if (keys) {
const environments = await batchRequests(keys, (key) =>
fetchEnvironmentByKey(this.authToken, this.projectKey, key),
)
this.writer.showResults(environments)
return
}
// show all environments if no keys flag provided in headless mode
if (flags.headless) {
const environments = await fetchEnvironments(
this.authToken,
this.projectKey,
)
this.writer.showResults(environments)
return
}
// prompt for key in interactive mode
const responses = await inquirer.prompt<EnvironmentPromptResult>(
[environmentPrompt],
{
token: this.authToken,
projectKey: this.projectKey,
},
)
this.writer.showResults(responses.environment)
}
}