Skip to content

Commit 08c336c

Browse files
authored
Merge pull request #591 from HubSpot/project-logs-v2
Update project logs
2 parents f1108e5 + 45eff4c commit 08c336c

File tree

4 files changed

+68
-56
lines changed

4 files changed

+68
-56
lines changed

packages/cli-lib/api/functions.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ async function getProjectAppFunctionLogs(
4141
const { limit = 5 } = query;
4242

4343
return http.get(accountId, {
44-
uri: `${FUNCTION_API_PATH}/app-function/logs/project/${projectName}/function/${functionName}`,
44+
uri: `${FUNCTION_API_PATH}/app-function/logs/project/${encodeURIComponent(
45+
projectName
46+
)}/function/${encodeURIComponent(functionName)}`,
4547
query: { ...query, limit, appPath },
4648
});
4749
}
@@ -53,7 +55,9 @@ async function getLatestProjectAppFunctionLog(
5355
appPath
5456
) {
5557
return http.get(accountId, {
56-
uri: `${FUNCTION_API_PATH}/app-function/logs/project/${projectName}/function/${functionName}/latest`,
58+
uri: `${FUNCTION_API_PATH}/app-function/logs/project/${encodeURIComponent(
59+
projectName
60+
)}/function/${encodeURIComponent(functionName)}/latest`,
5761
query: { appPath },
5862
});
5963
}

packages/cli-lib/api/results.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ async function getFunctionLogs(accountId, route, query = {}) {
66
const { limit = 5 } = query;
77

88
return http.get(accountId, {
9-
uri: `${RESULTS_API_PATH}/by-route/${route}`,
9+
uri: `${RESULTS_API_PATH}/by-route/${encodeURIComponent(route)}`,
1010
query: { ...query, limit },
1111
});
1212
}
1313

1414
async function getLatestFunctionLog(accountId, route) {
1515
return http.get(accountId, {
16-
uri: `${RESULTS_API_PATH}/by-route/${route}/latest`,
16+
uri: `${RESULTS_API_PATH}/by-route/${encodeURIComponent(route)}/latest`,
1717
});
1818
}
1919

packages/cli-lib/lang/en.lyaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,9 @@ en:
441441
logs:
442442
describe: "Get logs for a function within a project"
443443
errors:
444-
appPathRequired: "You must specify an app path using the --appPath argument."
445444
functionNameRequired: "You must pass a function name to retrieve logs for."
446-
logs: "No logs were found for the function name \"{{ functionName }}\" in the app path \"{{ appPath }}\" within the project \"{{ projectName }}\" in account {{ accountId }}."
445+
noAppFunctionLogs: "No logs were found for the function name \"{{ functionName }}\" in the app path \"{{ appPath }}\" within the project \"{{ projectName }}\" in account {{ accountId }}."
446+
noEndpointLogs: "No logs were found for the function name \"{{ functionName }}\" within the project \"{{ projectName }}\" in account {{ accountId }}."
447447
projectNameRequired: "You must specify a project name using the --projectName argument."
448448
examples:
449449
default: "Get 5 most recent logs for function named \"my-function\" within the app named \"app\" within the project named \"my-project\""

packages/cli/commands/project/logs.js

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const {
1313
getProjectAppFunctionLogs,
1414
getLatestProjectAppFunctionLog,
1515
} = require('@hubspot/cli-lib/api/functions');
16+
const {
17+
getFunctionLogs,
18+
getLatestFunctionLog,
19+
} = require('@hubspot/cli-lib/api/results');
1620
const { getProjectConfig } = require('../../lib/projects');
1721
const { loadAndValidateOptions } = require('../../lib/validation');
1822
const { tailLogs } = require('../../lib/serverlessLogs');
@@ -24,17 +28,23 @@ const { EXIT_CODES } = require('../../lib/enums/exitCodes');
2428
const handleLogsError = (e, accountId, projectName, appPath, functionName) => {
2529
if (e.statusCode === 404) {
2630
logger.error(
27-
i18n(`${i18nKey}.errors.logs`, {
28-
accountId,
29-
appPath,
30-
functionName,
31-
projectName,
32-
})
31+
appPath
32+
? i18n(`${i18nKey}.errors.noAppFunctionLogs`, {
33+
accountId,
34+
appPath,
35+
functionName,
36+
projectName,
37+
})
38+
: i18n(`${i18nKey}.errors.noEndpointLogs`, {
39+
accountId,
40+
functionName,
41+
projectName,
42+
})
3343
);
3444
}
3545
};
3646

37-
const appFunctionLog = async (accountId, options) => {
47+
const functionLog = async (accountId, options) => {
3848
const {
3949
latest,
4050
follow,
@@ -46,28 +56,40 @@ const appFunctionLog = async (accountId, options) => {
4656

4757
let logsResp;
4858

59+
const tailCall = async after => {
60+
try {
61+
return appPath
62+
? getProjectAppFunctionLogs(
63+
accountId,
64+
functionName,
65+
projectName,
66+
appPath,
67+
{
68+
after,
69+
}
70+
)
71+
: getFunctionLogs(accountId, functionName, { after });
72+
} catch (e) {
73+
handleLogsError(e, accountId, projectName, appPath, functionName);
74+
}
75+
};
76+
const fetchLatest = async () => {
77+
return appPath
78+
? getLatestProjectAppFunctionLog(
79+
accountId,
80+
functionName,
81+
projectName,
82+
appPath
83+
)
84+
: getLatestFunctionLog(accountId, functionName);
85+
};
86+
4987
if (follow) {
5088
const spinnies = new Spinnies();
5189

5290
spinnies.add('tailLogs', {
5391
text: i18n(`${i18nKey}.loading`),
5492
});
55-
const tailCall = after =>
56-
getProjectAppFunctionLogs(accountId, functionName, projectName, appPath, {
57-
after,
58-
});
59-
const fetchLatest = () => {
60-
try {
61-
return getLatestProjectAppFunctionLog(
62-
accountId,
63-
functionName,
64-
projectName,
65-
appPath
66-
);
67-
} catch (e) {
68-
handleLogsError(e, accountId, projectName, appPath, functionName);
69-
}
70-
};
7193

7294
await tailLogs({
7395
accountId,
@@ -78,24 +100,13 @@ const appFunctionLog = async (accountId, options) => {
78100
});
79101
} else if (latest) {
80102
try {
81-
logsResp = await getLatestProjectAppFunctionLog(
82-
accountId,
83-
functionName,
84-
projectName,
85-
appPath
86-
);
103+
logsResp = await fetchLatest();
87104
} catch (e) {
88105
handleLogsError(e, accountId, projectName, appPath, functionName);
89106
}
90107
} else {
91108
try {
92-
logsResp = await getProjectAppFunctionLogs(
93-
accountId,
94-
functionName,
95-
projectName,
96-
appPath,
97-
{}
98-
);
109+
logsResp = await tailCall();
99110
} catch (e) {
100111
handleLogsError(e, accountId, projectName, appPath, functionName);
101112
}
@@ -106,50 +117,47 @@ const appFunctionLog = async (accountId, options) => {
106117
}
107118
};
108119

109-
exports.command = 'logs [functionName]';
120+
exports.command = 'logs';
110121
exports.describe = i18n(`${i18nKey}.describe`);
111122

112123
exports.handler = async options => {
113124
await loadAndValidateOptions(options);
114125

115-
const { latest, functionName, appPath } = options;
126+
const { latest, functionName } = options;
116127
let projectName = options.projectName;
117128

118129
if (!functionName) {
119130
logger.error(i18n(`${i18nKey}.errors.functionNameRequired`));
120131
process.exit(EXIT_CODES.ERROR);
121132
} else if (!projectName) {
122-
const projectConfig = await getProjectConfig(getCwd());
133+
const { projectConfig } = await getProjectConfig(getCwd());
123134
if (projectConfig && projectConfig.name) {
124135
projectName = projectConfig.name;
125136
} else {
126137
logger.error(i18n(`${i18nKey}.errors.projectNameRequired`));
127138
process.exit(EXIT_CODES.ERROR);
128139
}
129-
} else if (!appPath) {
130-
logger.error(i18n(`${i18nKey}.errors.appPathRequired`));
131-
process.exit(EXIT_CODES.ERROR);
132140
}
133141

134142
const accountId = getAccountId(options);
135143

136144
trackCommandUsage('project-logs', { latest }, accountId);
137145

138-
appFunctionLog(accountId, { ...options, projectName });
146+
functionLog(accountId, { ...options, projectName });
139147
};
140148

141149
exports.builder = yargs => {
142-
yargs.positional('functionName', {
143-
describe: i18n(`${i18nKey}.positionals.functionName.describe`),
144-
type: 'string',
145-
demandOption: true,
146-
});
147150
yargs
148151
.options({
152+
functionName: {
153+
alias: 'function',
154+
describe: i18n(`${i18nKey}.positionals.functionName.describe`),
155+
type: 'string',
156+
demandOption: true,
157+
},
149158
appPath: {
150159
describe: i18n(`${i18nKey}.options.appPath.describe`),
151160
type: 'string',
152-
demandOption: true,
153161
},
154162
projectName: {
155163
describe: i18n(`${i18nKey}.options.projectName.describe`),
@@ -179,7 +187,7 @@ exports.builder = yargs => {
179187

180188
yargs.example([
181189
[
182-
'$0 project logs my-function --appName="app" --projectName="my-project"',
190+
'$0 project logs --function=my-function --appPath="app" --projectName="my-project"',
183191
i18n(`${i18nKey}.examples.default`),
184192
],
185193
]);

0 commit comments

Comments
 (0)