Skip to content

Commit

Permalink
Merge pull request #609 from CleverCloud/feat/logs-duration
Browse files Browse the repository at this point in the history
feat(logs): support duration in since parameters
  • Loading branch information
aurrelhebert committed Mar 5, 2024
2 parents 40a24ed + a223f3a commit 49f30ea
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bin/clever.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ function run () {
metavar: 'before',
aliases: ['until'],
parser: Parsers.date,
description: 'Fetch logs before this date/time (ISO8601)',
description: 'Fetch logs before this date/time (ISO8601 date or duration, positive number in seconds or duration Ex: 1h)',
}),
branch: cliparse.option('branch', {
aliases: ['b'],
Expand Down
13 changes: 9 additions & 4 deletions src/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,16 @@ function instances (instances) {

function date (dateString) {
const date = new Date(dateString);
if (isNaN(date.getTime())) {
return cliparse.parsers.error('Invalid date: ' + dateString + ' (timestamps or IS0 8601 dates are accepted)');
if (isNaN(dateString) && !isNaN(date.getTime())) {
return cliparse.parsers.success(date);
}
return cliparse.parsers.success(date);

const duration = durationInSeconds(dateString);
if (duration.success) {
return cliparse.parsers.success(new Date(Date.now() - (duration.success * 1000)));
}

return duration;
}

const appIdRegex = /^app_[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
Expand Down Expand Up @@ -170,7 +176,6 @@ function durationInSeconds (durationStr = '') {
}
catch (err) {
const n = Number.parseInt(durationStr);
console.log(`N: ${n}`);
if (isNaN(n) || n < 0) {
return failed;
}
Expand Down

0 comments on commit 49f30ea

Please sign in to comment.