2023-03-14 13:18:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-14 13:18:55 +00:00
|
|
|
*/
|
|
|
|
|
2020-10-19 16:29:43 +00:00
|
|
|
const read = require('fs').readFileSync;
|
|
|
|
const exec = require('child_process').execSync;
|
|
|
|
|
|
|
|
// See tests ../node-tests/config/utils.js
|
2022-09-15 08:43:17 +00:00
|
|
|
const repositoryYear = function (date = exec('git show -s --format=%ci HEAD')) {
|
|
|
|
return date.toString().trim().split('-').shift();
|
2020-10-19 16:29:43 +00:00
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
const repositorySHA = function (sha = exec('git rev-parse --short HEAD')) {
|
2020-10-19 16:29:43 +00:00
|
|
|
return sha.toString().trim();
|
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
const binaryVersion = function (repositoryRoot) {
|
2023-01-05 20:16:47 +00:00
|
|
|
return function (versionFileContents = read(`${repositoryRoot}/version/VERSION`)) {
|
2020-10-19 16:29:43 +00:00
|
|
|
// see /scripts/dist.sh:8
|
2023-01-11 17:48:04 +00:00
|
|
|
return versionFileContents.toString();
|
2020-10-19 16:29:43 +00:00
|
|
|
};
|
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
const env = function ($) {
|
|
|
|
return function (flag, fallback) {
|
2021-07-06 15:57:53 +00:00
|
|
|
// a fallback value MUST be set
|
|
|
|
if (typeof fallback === 'undefined') {
|
|
|
|
throw new Error(`Please provide a fallback value for $${flag}`);
|
|
|
|
}
|
|
|
|
// return the env var if set
|
|
|
|
if (typeof $[flag] !== 'undefined') {
|
|
|
|
if (typeof fallback === 'boolean') {
|
|
|
|
// if we are expecting a boolean JSON parse strings to numbers/booleans
|
|
|
|
return !!JSON.parse($[flag]);
|
|
|
|
}
|
|
|
|
return $[flag];
|
|
|
|
}
|
|
|
|
// If the fallback is a function call it and return the result.
|
|
|
|
// Lazily calling the function means binaries used for fallback don't need
|
|
|
|
// to be available if we are sure the environment variables will be set
|
|
|
|
if (typeof fallback === 'function') {
|
|
|
|
return fallback();
|
|
|
|
}
|
|
|
|
// just return the fallback value
|
|
|
|
return fallback;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-10-19 16:29:43 +00:00
|
|
|
module.exports = {
|
|
|
|
repositoryYear: repositoryYear,
|
|
|
|
repositorySHA: repositorySHA,
|
|
|
|
binaryVersion: binaryVersion,
|
2021-07-06 15:57:53 +00:00
|
|
|
env: env,
|
2020-10-19 16:29:43 +00:00
|
|
|
};
|