Skip to content

Commit 2eddc68

Browse files
add support for add tag for image (#86)
1 parent 4a7d361 commit 2eddc68

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const debug = require('debug')('codefresh:cli:logs');
2+
const Command = require('../../Command');
3+
const _ = require('lodash');
4+
const CFError = require('cf-errors');
5+
const { image } = require('../../../../logic').api;
6+
7+
8+
const tag = new Command({
9+
root: true,
10+
command: 'tag <id>',
11+
description: 'Add an image tag',
12+
builder: (yargs) => {
13+
yargs
14+
.option('tag', {
15+
describe: 'image tags',
16+
type: 'array',
17+
require: true,
18+
});
19+
20+
return yargs;
21+
},
22+
handler: async (argv) => {
23+
const imageId = argv.id;
24+
const tags = _.isArray(argv.tag) ? argv.tag : [argv.tag];
25+
for (let i = 0; i < tags.length; i += 1) {
26+
const tag = tags[i];
27+
await image.addImageTag({
28+
imageId,
29+
tag,
30+
});
31+
console.log(`tag: ${tag} successfully added to image ${imageId}`);
32+
}
33+
},
34+
});
35+
36+
module.exports = tag;

lib/logic/api/image.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const _extractFieldsForImageEntity = (image, tag) => {
1111
const newImage = {
1212
name: image.imageDisplayName,
1313
size: filesize(image.size),
14+
_id: image._id,
1415
};
1516
newImage.image_id = image.internalImageId ? image.internalImageId.substring(0, 12) : '\b';
1617
if (_.isEqual(tag, '<none>')) {
@@ -134,9 +135,20 @@ const annotateImage = async (dockerImageId, annotations) => {
134135
return sendHttpRequest(options);
135136
};
136137

138+
const addImageTag = async (options) => {
139+
const image = await getImageById(options.imageId);
140+
const imageId = image[0].info._id;
141+
const RequestOptions = {
142+
url: `/api/images/${encodeURIComponent(imageId)}/tag/${options.tag}`,
143+
method: 'POST',
144+
};
145+
return sendHttpRequest(RequestOptions);
146+
};
147+
137148
module.exports = {
138149
annotateImage,
139150
getDockerImageId,
140151
getAll,
141152
getImageById,
153+
addImageTag,
142154
};

0 commit comments

Comments
 (0)