Skip to content

Commit c6de50d

Browse files
add Untag option
1 parent 7b65cb1 commit c6de50d

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 untag = new Command({
9+
root: true,
10+
command: 'untag <id> [tags..]',
11+
category: 'Images',
12+
description: 'Untag an image',
13+
webDocs: {
14+
category: 'Images',
15+
title: 'Untag Image',
16+
},
17+
builder: (yargs) => {
18+
yargs
19+
.positional('id', {
20+
description: 'Docker image id',
21+
})
22+
.positional('names', {
23+
description: 'Tag names',
24+
})
25+
.example('codefresh untag 2dfacdaad466 1.0.0', "Remove tag '1.0.0' from image '2dfacdaad466'")
26+
.example('codefresh untag 2dfacdaad466 1.0.0 my-tag', "Remove tags '1.0.0' and 'my-tag' from image'2dfacdaad466'");
27+
28+
return yargs;
29+
},
30+
handler: async (argv) => {
31+
const imageId = argv.id;
32+
const tags = argv.tags;
33+
for (let i = 0; i < tags.length; i += 1) {
34+
const tag = tags[i];
35+
await image.untagImage({
36+
imageId,
37+
tag,
38+
});
39+
console.log(`Tag: ${tag} was successfully removed from image: ${imageId}`);
40+
}
41+
},
42+
});
43+
44+
module.exports = untag;

lib/logic/api/image.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const _extractFieldsForImageEntity = (image, tag) => {
1313
size: filesize(image.size),
1414
_id: image._id,
1515
annotations: _.get(image, 'metadata', {}),
16+
tagId: tag._id,
1617
};
1718
newImage.image_id = image.internalImageId ? image.internalImageId.substring(0, 12) : '\b';
1819
if (_.isEqual(tag, '<none>')) {
@@ -147,10 +148,25 @@ const addImageTag = async (options) => {
147148
return sendHttpRequest(RequestOptions);
148149
};
149150

151+
const untagImage = async (options) => {
152+
const images = await getImageById({ imageId: options.imageId });
153+
const imageId = images[0].info._id;
154+
const image = _.find(images, currimage => _.isEqual(currimage.info.tag, options.tag));
155+
if (image) {
156+
const RequestOptions = {
157+
url: `/api/images/${encodeURIComponent(imageId)}/tag/${image.info.tagId}`,
158+
method: 'DELETE',
159+
};
160+
return sendHttpRequest(RequestOptions);
161+
}
162+
throw new CFError(`Error from server (NotFound): Tag ${options.tag} not found`);
163+
};
164+
150165
module.exports = {
151166
annotateImage,
152167
getDockerImageId,
153168
getAll,
154169
getImageById,
155170
addImageTag,
171+
untagImage,
156172
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codefresh",
3-
"version": "0.8.21",
3+
"version": "0.8.22",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,

0 commit comments

Comments
 (0)