Skip to content

Commit 7bc01b6

Browse files
committed
logs: add support for sinceTime option
This commit adds sinceTime support to LogOptions. Fixes: #1673
1 parent 2c1c5ff commit 7bc01b6

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/log.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ export interface LogOptions {
3636
*/
3737
sinceSeconds?: number;
3838

39+
/**
40+
* Only return logs after a specific date (RFC3339). Defaults to all logs.
41+
* Only one of sinceSeconds or sinceTime may be specified.
42+
*/
43+
sinceTime?: string;
44+
3945
/**
4046
* If set, the number of lines from the end of the logs to show. If not specified, logs are shown from the creation
4147
* of the container or sinceSeconds or sinceTime
@@ -64,6 +70,12 @@ export function AddOptionsToSearchParams(
6470
if (options?.sinceSeconds) {
6571
searchParams.set('sinceSeconds', options?.sinceSeconds?.toString() || 'false');
6672
}
73+
if (options?.sinceTime) {
74+
if (options?.sinceSeconds) {
75+
throw new Error('at most one of sinceTime or sinceSeconds may be specified');
76+
}
77+
searchParams.set('sinceTime', options?.sinceTime);
78+
}
6779
if (options?.tailLines) {
6880
searchParams.set('tailLines', options?.tailLines?.toString() || 'false');
6981
}

src/log_test.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { URLSearchParams } from 'url';
55
describe('Log', () => {
66
describe('AddOptionsToSearchParams', () => {
77
it('should add options to search params', () => {
8-
const searchParams = new URLSearchParams();
9-
const options: LogOptions = {
8+
let searchParams = new URLSearchParams();
9+
let options: LogOptions = {
1010
follow: true,
1111
limitBytes: 100,
1212
pretty: true,
@@ -23,6 +23,12 @@ describe('Log', () => {
2323
expect(searchParams.get('sinceSeconds')).to.equal('1');
2424
expect(searchParams.get('tailLines')).to.equal('1');
2525
expect(searchParams.get('timestamps')).to.equal('true');
26+
27+
const sinceTime = new Date().toISOString();
28+
searchParams = new URLSearchParams();
29+
options = { sinceTime };
30+
AddOptionsToSearchParams(options, searchParams);
31+
expect(searchParams.get('sinceTime')).to.equal(sinceTime);
2632
});
2733
it('should use defaults for', () => {
2834
const searchParams = new URLSearchParams();
@@ -33,5 +39,16 @@ describe('Log', () => {
3339
expect(searchParams.get('previous')).to.equal('false');
3440
expect(searchParams.get('timestamps')).to.equal('false');
3541
});
42+
it('sinceTime and sinceSeconds cannot be used together', () => {
43+
const searchParams = new URLSearchParams();
44+
const sinceTime = new Date().toISOString();
45+
const options: LogOptions = {
46+
sinceSeconds: 1,
47+
sinceTime,
48+
};
49+
expect(() => {
50+
AddOptionsToSearchParams(options, searchParams);
51+
}).to.throw('at most one of sinceTime or sinceSeconds may be specified');
52+
});
3653
});
3754
});

0 commit comments

Comments
 (0)