Skip to content

Commit f32b3b0

Browse files
feat: add new output formats: jsonArray and yamlArray (#897)
## What ## Why ## Notes --------- Co-authored-by: Mikhail Klimko <mikhail.klimko@octopus.com>
1 parent 7eec9b5 commit f32b3b0

File tree

13 files changed

+126
-8
lines changed

13 files changed

+126
-8
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# go hub binary
22
FROM golang:alpine AS go
3-
RUN apk --update add ca-certificates git
3+
RUN apk --update add --no-scripts ca-certificates git
44
RUN go install github.com/github/hub@latest
55

66
# python yq binary

Dockerfile-debian

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# go hub binary
22
FROM golang:alpine as go
3-
RUN apk --update add ca-certificates git
3+
RUN apk --update add --no-scripts ca-certificates git
44
RUN go install github.com/github/hub@latest
55

66
# python yq binary

Dockerfile-debian-rootless

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# go hub binary
22
FROM golang:alpine as go
3-
RUN apk --update add ca-certificates git
3+
RUN apk --update add --no-scripts ca-certificates git
44
RUN go install github.com/github/hub@latest
55

66
# python yq binary

Dockerfile-rootless

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# go hub binary
22
FROM golang:alpine as go
3-
RUN apk --update add ca-certificates git
3+
RUN apk --update add --no-scripts ca-certificates git
44
RUN go install github.com/github/hub@latest
55

66
# python yq binary

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const get = new Command({
2222
.option('output', {
2323
describe: 'Output format',
2424
alias: 'o',
25-
choices: ['json', 'yaml', 'wide', 'name', 'id'],
25+
choices: ['json', 'yaml', 'wide', 'name', 'id', 'jsonArray', 'yamlArray'],
2626
group: 'Output Options',
2727
})
2828
.option('date-format', {

lib/interface/cli/commands/root/get.completion.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function optionHandler({ word, argv, option }) {
88
return fileDir(word);
99
}
1010
if (OUTPUT_OPTIONS.includes(option)) {
11-
return ['json', 'yaml', 'wide', 'name', 'id'];
11+
return ['json', 'yaml', 'wide', 'name', 'id', 'jsonArray', 'yamlArray'];
1212
}
1313
return [];
1414
}

lib/output/Output.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const TYPES = {
1313
table: require('./types/table.output'),
1414
id: require('./types/id.output'),
1515
name: require('./types/name.output'),
16+
jsonArray: require('./types/jsonArray.output'),
17+
yamlArray: require('./types/yamlArray.output'),
1618
};
1719

1820
const TABLE_TYPES = ['default', 'wide'];
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const CFError = require('cf-errors'); // eslint-disable-line
2+
const Entity = require('../../logic/entities/Entity');
3+
const _ = require('lodash');
4+
5+
function output(data) {
6+
const entities = _.isArray(data) ? data : [data];
7+
const jsonArray = [];
8+
9+
_.forEach(entities, (entity) => {
10+
if (!(entity instanceof Entity)) {
11+
throw new CFError('Cannot extract data for "jsonArray" output -- data contains not Entity');
12+
}
13+
jsonArray.push(entity.info);
14+
});
15+
16+
return JSON.stringify(jsonArray, null, '\t');
17+
}
18+
19+
module.exports = output;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const CFError = require('cf-errors');
2+
const Entity = require('../../logic/entities/Entity');
3+
const output = require('./jsonArray.output');
4+
5+
describe('jsonArray output', () => {
6+
it('should return empty array for an empty array', () => {
7+
expect(output([])).toBe('[]');
8+
});
9+
10+
it('should wrap single Entity into array', () => {
11+
const entity = new Entity();
12+
entity.info = { id: '1', status: 'ok' };
13+
14+
const result = JSON.parse(output(entity));
15+
expect(Array.isArray(result)).toBe(true);
16+
expect(result).toHaveLength(1);
17+
expect(result[0]).toEqual(entity.info);
18+
});
19+
20+
it('should keep multiple Entities as array', () => {
21+
const entity1 = new Entity();
22+
entity1.info = { id: '1' };
23+
const entity2 = new Entity();
24+
entity2.info = { id: '2' };
25+
26+
const result = JSON.parse(output([entity1, entity2]));
27+
expect(Array.isArray(result)).toBe(true);
28+
expect(result).toHaveLength(2);
29+
expect(result[0]).toEqual(entity1.info);
30+
expect(result[1]).toEqual(entity2.info);
31+
});
32+
33+
it('should throw CFError if element is not an Entity', () => {
34+
expect(() => output({ id: '1' })).toThrow(CFError);
35+
expect(() => output([new Entity(), {}])).toThrow(CFError);
36+
});
37+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const CFError = require('cf-errors');
2+
const Entity = require('../../logic/entities/Entity');
3+
const _ = require('lodash');
4+
const yaml = require('js-yaml');
5+
6+
function output(data) {
7+
const entities = _.isArray(data) ? data : [data];
8+
const yamlArray = [];
9+
10+
_.forEach(entities, (entity) => {
11+
if (!(entity instanceof Entity)) {
12+
throw new CFError('Cannot extract data for "yamlArray" output -- data contains not Entity');
13+
}
14+
yamlArray.push(entity.info);
15+
});
16+
17+
return yaml.dump(yamlArray);
18+
}
19+
20+
module.exports = output;

0 commit comments

Comments
 (0)