Skip to content

Commit b67cf30

Browse files
add filter options to images (#44)
1 parent c412702 commit b67cf30

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

lib/interface/cli/commands/image/get.cmd.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const command = new Command({
2121
describe: 'Limit amount of returned results',
2222
default: DEFAULTS.GET_LIMIT_RESULTS,
2323
})
24-
.option('all-registries', {
24+
.option('all', {
2525
describe: 'Return images from all possible registries (by default only r.cfcr.io images will be returned)',
2626
default: false,
2727
})
@@ -33,15 +33,30 @@ const command = new Command({
3333
.option('sha', {
3434
describe: 'Filter by specific commit sha',
3535
alias: 's',
36+
})
37+
.option('image-name', {
38+
describe: 'Filter by specific image name',
39+
type: 'array',
40+
})
41+
.option('branch', {
42+
describe: 'Filter by specific branch',
43+
type: 'array',
44+
})
45+
.option('page', {
46+
describe: 'Paginated page',
47+
default: DEFAULTS.GET_PAGINATED_PAGE,
3648
});
3749
},
3850
handler: async (argv) => {
3951
const imageId = argv.id;
4052
const labels = prepareKeyValueFromCLIEnvOption(argv.label);
4153
const sha = argv.sha;
4254
const limit = argv.limit;
43-
const volumeImages = argv['volume-images'];
44-
const allRegistries = argv['all-registries'];
55+
const type = argv.type;
56+
const branch = argv.branch;
57+
const allRegistries = argv.all;
58+
const offset = (argv.page - 1) * limit;
59+
const imageName = argv['image-name'];
4560
let filterRegistries;
4661
if (!allRegistries) {
4762
filterRegistries = DEFAULTS.CODEFRESH_REGISTRIES;
@@ -56,8 +71,11 @@ const command = new Command({
5671
labels,
5772
sha,
5873
limit,
59-
volumeImages,
6074
filterRegistries,
75+
type,
76+
branch,
77+
imageName,
78+
offset,
6179
});
6280
}
6381

lib/logic/api/image.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,41 @@ const { sendHttpRequest } = require('./helper');
44
const filesize = require('filesize');
55
const moment = require('moment');
66
const Image = require('../entities/Image');
7+
const DEFAULTS = require('../../interface/cli/defaults');
78

89

9-
const _extractFieldsForImageEntity = (image, tag) => ({
10-
name: image.imageDisplayName,
11-
tag: tag.tag,
12-
image_id: image.internalImageId.substring(0, 12),
13-
created: moment(tag.created).fromNow(),
14-
size: filesize(image.size),
15-
pull: `${tag.registry}/${tag.repository}:${tag.tag}`,
16-
});
10+
const _extractFieldsForImageEntity = (image, tag) => {
11+
const newImage = {
12+
name: image.imageDisplayName,
13+
size: filesize(image.size),
14+
};
15+
newImage.image_id = image.internalImageId ? image.internalImageId.substring(0, 12) : '';
16+
if (_.isEqual(tag, '<none>')) {
17+
newImage.tag = tag;
18+
newImage.pull = '';
19+
newImage.created = moment(image.created)
20+
.fromNow();
21+
} else {
22+
newImage.tag = tag.tag;
23+
newImage.pull = `${tag.registry}/${tag.repository}:${tag.tag}`;
24+
newImage.created = moment(tag.created)
25+
.fromNow();
26+
}
27+
return newImage;
28+
};
1729

1830

1931
const getAll = async (options) => {
2032
const qs = {
2133
limit: options.limit,
22-
offset: 0,
34+
offset: options.offset,
2335
metadata: options.labels,
36+
type: options.type,
37+
branch: options.branch,
38+
imageDisplayNameRegex: options.imageName,
2439
select: 'internalName tags internalImageId created size imageDisplayName',
2540
};
2641

27-
if (!options.volumeImages) {
28-
qs.metadata.cf_volume = false;
29-
}
30-
3142
if (options.sha) {
3243
qs.sha = options.sha;
3344
}
@@ -43,14 +54,22 @@ const getAll = async (options) => {
4354
}
4455
const res = [];
4556
_.forEach(images.docs, (image) => {
57+
let addedCfCrTag = false;
4658
_.forEach(image.tags, (tag) => {
4759
// in case we are filtering by registries, ignore the image if it is not from the registires list
48-
if (options.filterRegistries && options.filterRegistries.indexOf(tag.registry) === -1) {
60+
if ((options.filterRegistries && options.filterRegistries.indexOf(tag.registry) === -1) || _.isEqual(tag.tag, 'volume')) {
4961
return;
5062
}
63+
if (DEFAULTS.CODEFRESH_REGISTRIES.indexOf(tag.registry) !== -1) {
64+
addedCfCrTag = true;
65+
}
5166
const data = _extractFieldsForImageEntity(image, tag);
5267
res.push(new Image(data));
5368
});
69+
if (_.isEmpty(image.tags) || !addedCfCrTag) {
70+
const data = _extractFieldsForImageEntity(image, '<none>');
71+
res.push(new Image(data));
72+
}
5473
});
5574
return res;
5675
};

0 commit comments

Comments
 (0)