Skip to content

Commit c844fa1

Browse files
authored
Merge pull request #21 from cliffano/hcl-support
Add HCL support
2 parents 9c3c196 + 5eebc24 commit c844fa1

12 files changed

Lines changed: 246 additions & 6 deletions

File tree

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,22 @@ Example `toml` resource configuration:
151151
]
152152
}
153153

154+
### Managing version on HCL file
155+
156+
Example `hcl` resource configuration:
157+
158+
{
159+
"resources": [
160+
{
161+
"path": "somefile.hcl",
162+
"type": "hcl",
163+
"params": {
164+
"property": "version"
165+
}
166+
}
167+
]
168+
}
169+
154170
### Managing version on Makefile file
155171

156172
Example `makefile` resource configuration:

lib/release-schemes/rtk.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import stringFormat from 'string-format';
55
import util from 'util';
66
import semverVersionScheme from '../version-schemes/semver.js';
77
import gitScmScheme from '../scm-schemes/git.js';
8+
import hclResourceType from '../resource-types/hcl.js';
89
import jsonResourceType from '../resource-types/json.js';
910
import keepAChangelogResourceType from '../resource-types/keep-a-changelog.js';
1011
import makefileResourceType from '../resource-types/makefile.js';
@@ -29,6 +30,7 @@ class Rtk {
2930
git: gitScmScheme
3031
};
3132
this.resourceTypes = {
33+
hcl: hclResourceType,
3234
json: jsonResourceType,
3335
makefile: makefileResourceType,
3436
text: textResourceType,

lib/resource-types/hcl.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"use strict";
2+
import bag from 'bagofcli';
3+
import {getProperty} from 'dot-prop';
4+
import fs from 'fs';
5+
import hcl from 'hcl2-parser';
6+
7+
/**
8+
* Set version value in the HCL resource's property (defined in dot-notation).
9+
* This will use hcledit CLI tool (https://github.com/minamijoyo/hcledit) to
10+
* modify the HCL file because there is no node.js library that can write HCL
11+
* files in a safe way while preserving original formatting. NOTE: hcledit
12+
* must be installed and available in the PATH for this to work.
13+
*
14+
* @param {String} version: version value to set
15+
* @param {Object} resource: resource configuration which contains type, path, and params
16+
* @param {Object} opts: optional settings
17+
* - dryRun: when true, HCL file won't be modified
18+
* @param {Function} cb: standard cb(err, result) callback
19+
*/
20+
function setVersion(version, resource, opts, cb) {
21+
if (!opts.dryRun) {
22+
const property = resource.params.property;
23+
function execCb(err, stdOutOuput, stdErrOuput, result) {
24+
cb(err);
25+
};
26+
bag.exec(`hcledit attribute set ${property} ${version} -f ${resource.path} -u`, false, execCb);
27+
} else {
28+
cb();
29+
}
30+
}
31+
32+
/**
33+
* Get version value from the HCL resource's property (defined in dot-notation).
34+
*
35+
* @param {Object} resource: resource configuration which contains type, path, and params
36+
* @param {Function} cb: standard cb(err, result) callback
37+
*/
38+
function getVersion(resource, cb) {
39+
const property = resource.params.property;
40+
41+
function readCb(err, result) {
42+
let version;
43+
if (!err) {
44+
const data = hcl.parseToObject(result)[0];
45+
version = getProperty(data, property);
46+
}
47+
cb(err, version);
48+
}
49+
fs.readFile(resource.path, 'UTF-8', readCb);
50+
}
51+
52+
const exports = {
53+
setReleaseVersion: setVersion,
54+
setPostReleaseVersion: setVersion,
55+
getVersion: getVersion
56+
};
57+
58+
export {
59+
exports as default
60+
};

lib/rtk.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22
import async from 'async';
33
import rtkReleaseScheme from './release-schemes/rtk.js';
4+
import hclResourceType from './resource-types/hcl.js';
45
import jsonResourceType from './resource-types/json.js';
56
import keepAChangelogResourceType from './resource-types/keep-a-changelog.js';
67
import makefileResourceType from './resource-types/makefile.js';
@@ -27,6 +28,7 @@ class RTK {
2728
rtk: rtkReleaseScheme
2829
};
2930
this.resourceTypes = {
31+
hcl: hclResourceType,
3032
json: jsonResourceType,
3133
makefile: makefileResourceType,
3234
text: textResourceType,

package-lock.json

Lines changed: 10 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"bagofcli": "^2.4.1",
4545
"chalk": "^5.4.1",
4646
"dot-prop": "^9.0.0",
47+
"hcl2-parser": "^1.0.3",
4748
"js-yaml": "^4.1.0",
4849
"keep-a-changelog": "^0.10.4",
4950
"lodash": "^4.17.21",

test-integration/commands.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@
3131
command: '{rtk} release --dry-run --config-file {base}/test-integration/fixtures/confs/rtk-yaml.json'
3232
exitcode: 0
3333
output: 'Setting release version version-1.3.0 on yaml resource located at ../../../../../test-integration/fixtures/data/data.yaml'
34-
34+
35+
- description: Command release dry run config should format release value on a HCL data file
36+
command: '{rtk} release --dry-run --config-file {base}/test-integration/fixtures/confs/rtk-hcl.json'
37+
exitcode: 0
38+
output: 'Setting release version version-1.3.0 on hcl resource located at ../../../../../test-integration/fixtures/data/data.hcl'
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"resources": [
3+
{
4+
"path": "../../../../../test-integration/fixtures/data/data.hcl",
5+
"type": "hcl",
6+
"params": {
7+
"property": "app_version",
8+
"release_format": "version-{version}"
9+
}
10+
}
11+
]
12+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
app_version = "1.2.3"

test/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('cli - release', function() {
2828

2929
afterEach(function () {
3030
this.mockBag.verify();
31-
this.mockBag.restore();
31+
sinon.restore();
3232
});
3333

3434
it('should contain release command and delegate to rtk release when exec is called', function (done) {

0 commit comments

Comments
 (0)