-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathlogs.js
145 lines (130 loc) · 4.81 KB
/
logs.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
Copyright 2019 Adobe. 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 { Flags } = require('@oclif/core')
const BaseCommand = require('../../BaseCommand')
const { wrapError } = require('../../lib/app-helper')
const rtLib = require('@adobe/aio-lib-runtime')
const LogForwarding = require('../../lib/log-forwarding')
const SECURED = 'require-adobe-auth'
class Logs extends BaseCommand {
_processEachAction (fullConfig, processFn) {
const isSecuredAction = (pkg, aName) => pkg && pkg.actions && pkg.actions[aName] &&
pkg.actions[aName].annotations && pkg.actions[aName].annotations[SECURED]
Object.entries(fullConfig.all).forEach(([, config]) => {
Object.entries(config.manifest.full.packages).forEach(([packageName, pkg]) => {
// handle default package
packageName = packageName.replace(/__APP_PACKAGE__/g, config.ow.package)
Object.keys(pkg.actions).forEach((aName) => {
processFn(packageName, aName, isSecuredAction(pkg, aName))
})
})
})
}
async run () {
const { flags } = await this.parse(Logs)
const fullConfig = await this.getFullConfig()
// has any backend
const hasAnyBackend = Object.values(fullConfig.all).reduce((hasBackend, config) => hasBackend && config.app.hasBackend, true)
if (!hasAnyBackend) {
throw new Error('There are no backend implementations for this project folder.')
}
const lf = await LogForwarding.init(fullConfig.aio)
const serverConfig = await lf.getServerConfig()
const logForwardingDestination = serverConfig.getDestination()
if (logForwardingDestination !== 'adobe_io_runtime') {
this.log(`Namespace is configured with custom log forwarding destination: '${logForwardingDestination}'. ` +
'Please use corresponding logging platform to view logs.')
return
}
if (flags.limit < 1) {
this.log('--limit should be > 0, using --limit=1')
flags.limit = 1
} else if (flags.limit > 50) {
this.log('--limit should be <= 50, using --limit=50')
flags.limit = 50
}
const filterActions = []
if (flags.action) {
flags.action.forEach((actionName) => {
if (actionName.includes('/')) {
filterActions.push(actionName)
return
}
// handle action name without package
const actionsToAdd = []
this._processEachAction(fullConfig, (packageName, aName) => {
const normalizedActionName = `${packageName}/${aName}`
if (normalizedActionName.includes(actionName)) {
actionsToAdd.push(normalizedActionName)
}
})
if (actionsToAdd.length === 0) {
throw new Error(`There is no match for action '${actionName}' in any of the packages.`)
} else {
filterActions.push(...actionsToAdd)
}
})
} else {
this._processEachAction(fullConfig, (packageName, aName, isSecured) => {
if (isSecured) {
const securedAName = '__secured_' + aName
filterActions.push(`${packageName}/${securedAName}`)
}
filterActions.push(`${packageName}/${aName}`)
})
}
try {
const owConfig = { ow: Object.values(fullConfig.all)[0].ow }
await rtLib.printActionLogs(owConfig, this.log.bind(this), flags.limit, filterActions, flags.strip, flags.poll || flags.tail || flags.watch)
} catch (error) {
this.error(wrapError(error))
}
}
}
Logs.description = `Fetch logs for an Adobe I/O App
`
Logs.flags = {
...BaseCommand.flags,
limit: Flags.integer({
description: 'Limit number of activations to fetch logs from ( 1-50 )',
default: 1,
char: 'l'
}),
action: Flags.string({
description: 'Fetch logs for a specific action',
char: 'a',
multiple: true
}),
strip: Flags.boolean({
char: 'r',
description: 'strip timestamp information and output first line only',
default: false
}),
tail: Flags.boolean({
description: 'Fetch logs continuously',
char: 't',
default: false,
exclusive: ['watch', 'poll']
}),
watch: Flags.boolean({
description: 'Fetch logs continuously',
default: false,
char: 'w',
exclusive: ['tail', 'poll']
}),
poll: Flags.boolean({
description: 'Fetch logs continuously',
default: false,
char: 'o',
exclusive: ['watch', 'tail']
})
}
module.exports = Logs