-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HCL placeholder. Need a library that can support dot notation.
- Loading branch information
Showing
4 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"use strict" | ||
import {getProperty, setProperty} from 'dot-prop'; | ||
import fs from 'fs'; | ||
import hcl2 from 'hcl2-json-parser'; | ||
import hcl from 'js-hcl-parser'; | ||
|
||
/** | ||
* Set version value in the HCL resource's property (defined in dot-notation). | ||
* | ||
* @param {String} version: version value to set | ||
* @param {Object} resource: resource configuration which contains type, path, and params | ||
* @param {Object} opts: optional settings | ||
* - dryRun: when true, HCL file won't be modified | ||
* @param {Function} cb: standard cb(err, result) callback | ||
*/ | ||
function setVersion(version, resource, opts, cb) { | ||
const property = resource.params.property; | ||
let data = hcl.parse(fs.readFileSync(resource.path, 'UTF-8')); | ||
console.dir("OBJECTIFY"); | ||
console.dir(data); | ||
setProperty(data, property, version); | ||
if (!opts.dryRun) { | ||
console.dir("STRINGIFY") | ||
console.dir(hcl.stringify(data)) | ||
fs.writeFile(resource.path, hcl.stringify(data), cb); | ||
} else { | ||
cb(); | ||
} | ||
} | ||
|
||
/** | ||
* Get version value from the HCL resource's property (defined in dot-notation). | ||
* | ||
* @param {Object} resource: resource configuration which contains type, path, and params | ||
* @param {Function} cb: standard cb(err, result) callback | ||
*/ | ||
function getVersion(resource, cb) { | ||
const property = resource.params.property; | ||
|
||
function readCb(err, result) { | ||
let version; | ||
if (!err) { | ||
const data = hcl2.parseToObject(result); | ||
version = getProperty(data, property); | ||
} | ||
cb(err, version); | ||
} | ||
fs.readFile(resource.path, 'UTF-8', readCb); | ||
} | ||
|
||
const exports = { | ||
setReleaseVersion: setVersion, | ||
setPostReleaseVersion: setVersion, | ||
getVersion: getVersion | ||
}; | ||
|
||
export { | ||
exports as default | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
"use strict" | ||
/* eslint no-unused-vars: 0 */ | ||
import assert from 'assert'; | ||
import fs from 'fs'; | ||
import resourceType from '../../lib/resource-types/hcl.js'; | ||
import sinon from 'sinon'; | ||
|
||
describe('hcl', function() { | ||
|
||
beforeEach(function (done) { | ||
this.mockFs = sinon.mock(fs); | ||
done(); | ||
}); | ||
afterEach(function (done) { | ||
this.mockFs.verify(); | ||
sinon.restore(); | ||
done(); | ||
}); | ||
|
||
describe('setVersion', function() { | ||
it('should only set version but not modify hcl file when dry run is enabled', function(done) { | ||
let resource = { | ||
path: 'variables.tf', | ||
type: 'hcl', | ||
params: { | ||
property: 'version' | ||
} | ||
}; | ||
this.mockFs.expects('readFileSync').once().withExactArgs('variables.tf', 'UTF-8').returns('variable "tags" { default = { version = "0.0.0" }}'); | ||
this.mockFs.expects('writeFile').never(); | ||
function cb(err, result) { | ||
assert.equal(err, null); | ||
done(); | ||
} | ||
resourceType.setReleaseVersion('1.2.3', resource, { dryRun: true }, cb); | ||
}); | ||
it('should set version and modify hcl file when dry run is disabled', function(done) { | ||
let resource = { | ||
path: 'variables.tf', | ||
type: 'hcl', | ||
params: { | ||
property: 'variable.tags.default.version' | ||
} | ||
}; | ||
this.mockFs.expects('readFileSync').once().withExactArgs('variables.tf', 'UTF-8').returns('variable "tags" { default = { version = "0.0.0" }}'); | ||
this.mockFs.expects('writeFile').once().withExactArgs('variables.tf', 'variable "tags" { default = { version = "1.2.3" }}\n', sinon.match.func).callsArgWith(2, null); | ||
function cb(err, result) { | ||
assert.equal(err, null); | ||
done(); | ||
} | ||
resourceType.setReleaseVersion('1.2.3', resource, { dryRun: false }, cb); | ||
}); | ||
// it('should set array property under a section', function(done) { | ||
// let resource = { | ||
// path: 'variables.tf', | ||
// type: 'hcl', | ||
// params: { | ||
// property: 'package.versions[1]' | ||
// } | ||
// }; | ||
// this.mockFs.expects('readFileSync').once().withExactArgs('variables.tf', 'UTF-8').returns('[package]\nversions = ["9.9.9", "0.0.0", "8.8.8"]'); | ||
// this.mockFs.expects('writeFile').once().withExactArgs('variables.tf', '[package]\nversions = [ "9.9.9", "1.2.3", "8.8.8" ]\n', sinon.match.func).callsArgWith(2, null); | ||
// function cb(err, result) { | ||
// assert.equal(err, null); | ||
// done(); | ||
// } | ||
// resourceType.setReleaseVersion('1.2.3', resource, { dryRun: false }, cb); | ||
// }); | ||
}); | ||
|
||
// describe('getVersion', function() { | ||
// it('should get version from resource property', function(done) { | ||
// let resource = { | ||
// path: 'variables.tf', | ||
// type: 'hcl', | ||
// params: { | ||
// property: 'version' | ||
// } | ||
// }; | ||
// this.mockFs.expects('readFile').once().withExactArgs('variables.tf', 'UTF-8', sinon.match.func).callsArgWith(2, null, 'version = "0.0.0"'); | ||
// function cb(err, result) { | ||
// assert.equal(err, null); | ||
// assert.equal(result, '0.0.0'); | ||
// done(); | ||
// } | ||
// resourceType.getVersion(resource, cb); | ||
// }); | ||
// it('should get array property under a section', function(done) { | ||
// let resource = { | ||
// path: 'variables.tf', | ||
// type: 'hcl', | ||
// params: { | ||
// property: 'package.versions[1]' | ||
// } | ||
// }; | ||
// this.mockFs.expects('readFile').once().withExactArgs('variables.tf', 'UTF-8', sinon.match.func).callsArgWith(2, null, '[package]\nversions = ["9.9.9", "0.0.0", "8.8.8"]'); | ||
// function cb(err, result) { | ||
// assert.equal(err, null); | ||
// assert.equal(result, '0.0.0'); | ||
// done(); | ||
// } | ||
// resourceType.getVersion(resource, cb); | ||
// }); | ||
// }); | ||
|
||
}); |