forked from graphprotocol/graph-tooling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
141 lines (128 loc) · 3.11 KB
/
auth.js
File metadata and controls
141 lines (128 loc) · 3.11 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const chalk = require('chalk')
const { saveDeployKey } = require('../command-helpers/auth')
const { chooseNodeUrl } = require('../command-helpers/node')
const { fixParameters } = require('../command-helpers/gluegun')
const HELP = `
${chalk.bold('graph auth')} [options] ${chalk.bold('<node>')} ${chalk.bold(
'<deploy-key>'
)}
${chalk.dim('Options:')}
--product <subgraph-studio|hosted-service>
Selects the product for which to authenticate
--studio Shortcut for --product subgraph-studio
-h, --help Show usage information
`
const processForm = async (
toolbox,
{
product,
studio,
node,
deployKey,
},
) => {
const questions = [
{
type: 'select',
name: 'product',
message: 'Product for which to initialize',
choices: ['subgraph-studio', 'hosted-service'],
skip:
product === 'subgraph-studio' ||
product === 'hosted-service' ||
studio !== undefined || node !== undefined,
},
{
type: 'password',
name: 'deployKey',
message: 'Deploy key',
skip: deployKey !== undefined,
},
]
try {
const answers = await toolbox.prompt.ask(questions)
return answers
} catch (e) {
return undefined
}
}
module.exports = {
description: 'Sets the deploy key to use when deploying to a Graph node',
run: async toolbox => {
// Obtain tools
let { filesystem, print, system } = toolbox
// Read CLI parameters
let {
product,
studio,
h,
help,
} = toolbox.parameters.options
// Show help text if requested
if (help || h) {
print.info(HELP)
return
}
let firstParam, secondParam
try {
;[firstParam, secondParam] = fixParameters(toolbox.parameters, {
h,
help,
studio
})
} catch (e) {
print.error(e.message)
process.exitCode = 1
return
}
// if user specifies --product or --studio then deployKey is the first parameter
let node
let deployKey
if (product || studio) {
;({ node } = chooseNodeUrl({ product, studio, node }))
deployKey = firstParam
} else {
node = firstParam
deployKey = secondParam
}
if (!node || !deployKey) {
const inputs = await processForm(toolbox, {
product,
studio,
node,
deployKey
})
if (inputs === undefined) {
process.exit(1)
}
if (!node) {
;({ node } = chooseNodeUrl({
product: inputs.product,
studio,
node,
}))
}
if (!deployKey) {
deployKey = inputs.deployKey
}
}
if (!deployKey) {
print.error(`No deploy key provided`)
print.info(HELP)
process.exitCode = 1
return
}
if (deployKey.length > 200) {
print.error(`Deploy key must not exceed 200 characters`)
process.exitCode = 1
return
}
try {
await saveDeployKey(node, deployKey)
print.success(`Deploy key set for ${node}`)
} catch (e) {
print.error(e)
process.exitCode = 1
}
},
}