-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
77 lines (66 loc) · 2.21 KB
/
index.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const { ux, sdk } = require('@cto.ai/sdk')
const { LOGO } = require('./constants')
const { whichJobPrompt, shouldContinuePrompt } = require('./prompts')
const {
createIncident,
updateIncident,
searchIncidents,
whoOnCall,
} = require('./integrations/pagerDuty')
const { listIssues } = require('./integrations/gitlab')
const { retrieveAuth, getFlags, getInitialJob } = require('./utils/helpers')
// Map our job names to their functions
const jobs = {
'Create an Incident': createIncident,
'Update an Incident': updateIncident,
'List Issues': listIssues,
'Search for Incidents': searchIncidents,
'List On-Call': whoOnCall,
}
async function main() {
const greeting = `\n👋 Welcome to Incident.sh 👋\n`
// Attempt to parse the user's passed flags
let initialJob
try {
initialJob = getInitialJob()
} catch (err) {
await ux.print(ux.colors.red(err.message))
return
}
await ux.print(LOGO)
await ux.print(greeting)
// Attempt to retrieve previous auth config and prompt the user if they
// want to use it or enter new details, use old if we have a runtime job
const { authData, user } = await retrieveAuth(!!initialJob)
// Extract job names from our jobs mapping
const jobChoices = Object.keys(jobs)
// Continue to prompt for jobs until the user is finished
let run = true
let initial = true
while (run) {
if (initial && initialJob) {
// Run the runtime selected job
initial = false
await runJob(initialJob, authData, user)
} else {
// Run the user's selected job
const { job } = await ux.prompt(whichJobPrompt(jobChoices))
await runJob(job, authData, user)
}
// Let the user decide if they would like to preform another task
const { shouldContinue } = await ux.prompt(shouldContinuePrompt)
run = shouldContinue
}
await ux.print(ux.colors.magenta(`👋 Thanks for using Incident.sh! 👋`))
}
/**
* runJob calls the appropriate function from our jobs mapping.
*
* @param {string} job The key for the selected job to run
* @param {object} authData The auth data entered by the user
* @param {object} user The current SDK user
*/
async function runJob(job, authData, user) {
await jobs[job](authData, user)
}
main()