/
list.js
129 lines (113 loc) · 3.23 KB
/
list.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
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
/*
Copyright 2019 Adobe Inc. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
const { flags } = require('@oclif/command')
const { cli } = require('cli-ux')
function processApi (api) {
const data = []
const paths = api.value.apidoc.paths
Object.keys(paths).forEach(key => {
if (!key.startsWith('/')) {
return
}
const path = paths[key]
Object.keys(path).forEach(verb => {
const operation = path[verb]
const item = {}
item.Action = `/${operation.operationId}`
item.Verb = verb
item.APIName = api.value.apidoc.basePath
item.URL = `${api.value.gwApiUrl}${key}`
data.push(item)
})
})
return data
}
class RouteList extends RuntimeBaseCommand {
async run () {
const { args, flags } = this.parse(RouteList)
try {
const ow = await this.wsk()
const options = {
basepath: args.basePath,
relpath: args.relPath,
operation: args.apiVerb,
skip: flags.skip,
limit: flags.limit
}
const result = await ow.routes.list(options)
if (flags.json) {
this.logJSON('', result.apis[0].value.apidoc)
} else {
let data = []
result.apis.forEach(api => {
// join the two arrays by reduce
data = processApi(api).reduce((coll, item) => {
coll.push(item)
return coll
}, data)
})
cli.table(data, {
Action: { minWidth: 10 },
Verb: { minWidth: 10 },
APIName: { header: 'API Name', minWidth: 10 },
URL: { minWidth: 15, 'no-truncate': true }
},
{
printLine: this.log,
...flags // parsed flags
})
}
} catch (err) {
this.handleError('failed to list the api', err)
}
}
}
RouteList.args = [
{
name: 'basePath',
description: 'The base path of the api'
},
{
name: 'relPath',
description: 'The path of the api relative to the base path'
},
{
name: 'apiVerb',
description: 'The http verb',
options: ['get', 'post', 'put', 'patch', 'delete', 'head', 'options']
}
]
RouteList.flags = {
...RuntimeBaseCommand.flags,
limit: flags.integer({
char: 'l',
description: 'only return LIMIT number of triggers'
}),
skip: flags.integer({
char: 's',
description: 'exclude the first SKIP number of triggers from the result'
}),
json: flags.boolean({
description: 'output raw json'
})
}
RouteList.description = 'list route/apis for Adobe I/O Runtime'
RouteList.aliases = [
'runtime:route:ls',
'runtime:api:list',
'runtime:api:ls',
'rt:route:list',
'rt:route:ls',
'rt:api:list',
'rt:api:ls'
]
module.exports = RouteList