Compare commits

...

48 Commits

Author SHA1 Message Date
8e7909e367 3.1.1 2019-01-14 22:42:25 +01:00
ff9302f745 fix(core): update 2019-01-14 22:42:24 +01:00
3577fa22e1 3.1.0 2019-01-14 01:54:05 +01:00
f5275adfbd feat(docker secret): docker secret.json can now also be named [anything].secret.json 2019-01-14 01:54:04 +01:00
31bf1b27a4 3.0.7 2019-01-13 22:33:19 +01:00
a77def5844 fix(core): update 2019-01-13 22:33:18 +01:00
2357699f3e 3.0.6 2019-01-13 22:04:20 +01:00
01f5784488 fix(core): update 2019-01-13 22:04:20 +01:00
80f35c39aa 3.0.5 2019-01-13 21:35:57 +01:00
87f55773bd fix(core): update 2019-01-13 21:35:57 +01:00
1a25341232 3.0.4 2019-01-13 18:25:09 +01:00
ccd41d86bf fix(core): update 2019-01-13 18:25:09 +01:00
0a3a7e480e 3.0.3 2019-01-13 00:00:32 +01:00
33a91c6ae7 fix(core): update 2019-01-13 00:00:32 +01:00
0cfbdf2c9e 3.0.2 2019-01-06 03:36:40 +01:00
f7362e5444 fix(core): update 2019-01-06 03:36:40 +01:00
7844fdb954 3.0.1 2019-01-06 03:20:59 +01:00
81d1a4f15e fix(core): update 2019-01-06 03:20:59 +01:00
da02923679 3.0.0 2019-01-06 01:30:56 +01:00
d7927ead86 2.0.3 2019-01-06 01:30:07 +01:00
9214ac49d5 fix(core): update 2019-01-06 01:30:07 +01:00
309b3e100d 2.0.2 2018-08-13 21:58:19 +02:00
eac42f50d6 fix(core): update 2018-08-13 21:58:18 +02:00
c83b7688e9 2.0.1 2018-08-13 00:45:57 +02:00
524fc5265f fix(docs): update 2018-08-13 00:45:57 +02:00
692168c8c2 2.0.0 2018-08-13 00:09:37 +02:00
2b3198c5bb BREAKING CHANGE(scope): change scope to @pushrocks/ 2018-08-13 00:09:37 +02:00
26104a231d 1.1.7 2017-05-13 11:40:13 +02:00
44bc565efe fix path resolution for env.yml 2017-05-13 11:40:10 +02:00
09c6a0aa8a 1.1.6 2017-05-12 19:12:07 +02:00
5a307f4c2c add relative path compatibility 2017-05-12 19:12:03 +02:00
2925a88e5b 1.1.5 2017-05-12 18:17:29 +02:00
0a4bad1ad0 refactor code 2017-05-12 18:17:22 +02:00
e34d37da19 1.1.4 2017-05-12 17:57:11 +02:00
a029eeb6c1 update to latest standards 2017-05-12 17:56:51 +02:00
7527049fd0 1.1.3 2017-01-21 11:57:09 +01:00
25589d4ca2 cleanup dependencies 2017-01-21 11:57:04 +01:00
73c223af6c 1.1.2 2017-01-21 11:51:35 +01:00
ead69f3bb9 remove beautylog dependency 2017-01-21 11:51:30 +01:00
7de17bbefa add better README 2016-12-31 19:26:32 +01:00
4e39970dd7 1.1.1 2016-08-04 16:44:48 +02:00
2a7c239176 fix interface naming 2016-08-04 16:44:42 +02:00
35e71ccf00 1.1.0 2016-08-04 16:09:44 +02:00
11a2cf83ee add keyValueObjectArray 2016-08-04 16:09:39 +02:00
d7a0541d81 1.0.8 2016-06-21 16:25:09 +02:00
c03e17d522 improve log output 2016-06-21 16:25:03 +02:00
b9d1080829 1.0.7 2016-06-21 16:13:45 +02:00
a84a257e4b improve log output 2016-06-21 16:13:29 +02:00
19 changed files with 1502 additions and 208 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
node_modules/
coverage/
docs/
pages/
public/

View File

@ -1,35 +1,150 @@
image: hosttoday/ht-docker-node:npmts
# gitzone standard
image: hosttoday/ht-docker-node:npmci
cache:
paths:
- .npmci_cache/
key: "$CI_BUILD_STAGE"
stages:
- security
- test
- release
- metadata
testLEGACY:
stage: test
# ====================
# security stage
# ====================
mirror:
stage: security
script:
- npmci test lts
- npmci git mirror
tags:
- docker
- docker
- notpriv
snyk:
stage: security
script:
- npmci npm prepare
- npmci command npm install -g snyk
- npmci command npm install --ignore-scripts
- npmci command snyk test
tags:
- docker
- notpriv
sast:
stage: security
image: registry.gitlab.com/hosttoday/ht-docker-dbase:npmci
variables:
DOCKER_DRIVER: overlay2
allow_failure: true
services:
- docker:stable-dind
script:
- npmci npm prepare
- npmci npm install
- npmci command npm run build
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
- docker run
--env SAST_CONFIDENCE_LEVEL="${SAST_CONFIDENCE_LEVEL:-3}"
--volume "$PWD:/code"
--volume /var/run/docker.sock:/var/run/docker.sock
"registry.gitlab.com/gitlab-org/security-products/sast:$SP_VERSION" /app/bin/run /code
artifacts:
reports:
sast: gl-sast-report.json
tags:
- docker
- priv
# ====================
# test stage
# ====================
testLTS:
stage: test
script:
- npmci test lts
- npmci npm prepare
- npmci node install lts
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- docker
- notpriv
testSTABLE:
stage: test
script:
- npmci test stable
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- docker
- notpriv
release:
stage: release
script:
- npmci publish
- npmci node install stable
- npmci npm publish
only:
- tags
- tags
tags:
- docker
- notpriv
# ====================
# metadata stage
# ====================
codequality:
stage: metadata
image: docker:stable
allow_failure: true
services:
- docker:stable-dind
script:
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
- docker run
--env SOURCE_CODE="$PWD"
--volume "$PWD":/code
--volume /var/run/docker.sock:/var/run/docker.sock
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
artifacts:
paths: [codeclimate.json]
tags:
- docker
- priv
trigger:
stage: metadata
script:
- npmci trigger
only:
- tags
tags:
- docker
- notpriv
pages:
image: hosttoday/ht-docker-node:npmci
stage: metadata
script:
- npmci command npm install -g typedoc typescript
- npmci npm prepare
- npmci npm install
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
tags:
- docker
- notpriv
only:
- tags
artifacts:
expire_in: 1 week
paths:
- public
allow_failure: true

View File

@ -1,19 +1,46 @@
# qenv
provides an easy way for promised environments
easy promised environments
## Availabililty
[![npm](https://pushrocks.gitlab.io/assets/repo-button-npm.svg)](https://www.npmjs.com/package/qenv)
[![git](https://pushrocks.gitlab.io/assets/repo-button-git.svg)](https://GitLab.com/pushrocks/qenv)
[![git](https://pushrocks.gitlab.io/assets/repo-button-mirror.svg)](https://github.com/pushrocks/qenv)
[![docs](https://pushrocks.gitlab.io/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/qenv/)
## Status for master
[![build status](https://GitLab.com/pushrocks/qenv/badges/master/build.svg)](https://GitLab.com/pushrocks/qenv/commits/master)
[![coverage report](https://GitLab.com/pushrocks/qenv/badges/master/coverage.svg)](https://GitLab.com/pushrocks/qenv/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/qenv.svg)](https://www.npmjs.com/package/qenv)
[![Dependency Status](https://david-dm.org/pushrocks/qenv.svg)](https://david-dm.org/pushrocks/qenv)
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/qenv/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/qenv/master/dependencies/npm)
[![bitHound Code](https://www.bithound.io/github/pushrocks/qenv/badges/code.svg)](https://www.bithound.io/github/pushrocks/qenv)
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
## Usage
Use TypeScript for best in class instellisense.
qenv works with two files:
* **qenv.yml** - specifies which ENV vars are required.
* **env.yml** - specifies all env vars that are not already set in the current environment.
- **qenv.yml** - specifies which ENV vars are required.
- **env.yml** - specifies all env vars that are not already set in the current environment.
Now obviously you can set build specific env vars in many CI environments.
So there we do not need an **env.yml** since all ENV vars are in place
However when on another machine you can have a env.yml that will be added to the environment by qenv.
```typescript
import {Qenv} from "qenv";
myQenv = new Qenv("path/to/dir/where/qenv.yml/is/","path/to/dir/where/env.yml/is(");
```javascript
import { Qenv } from 'qenv';
const myQenv = new Qenv('path/to/dir/where/qenv.yml/is/', 'path/to/dir/where/env.yml/is(');
```
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://push.rocks)

6
dist/index.d.ts vendored
View File

@ -1,6 +0,0 @@
export declare class Qenv {
requiredEnvVars: string[];
availableEnvVars: string[];
missingEnvVars: string[];
constructor(basePathArg: string, envYmlPathArg: any);
}

52
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1,5 +0,0 @@
import "typings-global";
export import beautylog = require("beautylog");
export declare let lodash: any;
export import path = require("path");
export import smartfile = require("smartfile");

View File

@ -1,8 +0,0 @@
"use strict";
require("typings-global");
exports.beautylog = require("beautylog");
exports.lodash = require("lodash");
exports.path = require("path");
exports.smartfile = require("smartfile");
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInFlbnYucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsUUFBTyxnQkFBZ0IsQ0FBQyxDQUFBO0FBQ1YsaUJBQVMsV0FBVyxXQUFXLENBQUMsQ0FBQztBQUNwQyxjQUFNLEdBQUcsT0FBTyxDQUFDLFFBQVEsQ0FBQyxDQUFDO0FBQ3hCLFlBQUksV0FBVyxNQUFNLENBQUMsQ0FBQztBQUN2QixpQkFBUyxXQUFXLFdBQVcsQ0FBQyxDQUFDIiwiZmlsZSI6InFlbnYucGx1Z2lucy5qcyIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCBcInR5cGluZ3MtZ2xvYmFsXCI7XG5leHBvcnQgaW1wb3J0IGJlYXV0eWxvZyA9IHJlcXVpcmUoXCJiZWF1dHlsb2dcIik7XG5leHBvcnQgbGV0IGxvZGFzaCA9IHJlcXVpcmUoXCJsb2Rhc2hcIik7XG5leHBvcnQgaW1wb3J0IHBhdGggPSByZXF1aXJlKFwicGF0aFwiKTtcbmV4cG9ydCBpbXBvcnQgc21hcnRmaWxlID0gcmVxdWlyZShcInNtYXJ0ZmlsZVwiKTtcbiJdfQ==

6
npmextra.json Normal file
View File

@ -0,0 +1,6 @@
{
"npmci": {
"npmGlobalTools": [],
"npmAccessLevel": "public"
}
}

View File

@ -1,3 +0,0 @@
{
"mode":"default"
}

1057
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,13 @@
{
"name": "qenv",
"version": "1.0.6",
"description": "promised environments",
"name": "@pushrocks/qenv",
"version": "3.1.1",
"private": false,
"description": "easy promised environments",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"scripts": {
"test": "(npmts)"
"test": "(tstest test/)",
"build": "(tsbuild)"
},
"repository": {
"type": "git",
@ -23,15 +25,16 @@
},
"homepage": "https://gitlab.com/pushrocks/qenv#README",
"devDependencies": {
"npmts-g": "^5.2.6",
"should": "^9.0.2",
"typings-test": "^1.0.1"
"@gitzone/tsbuild": "^2.0.22",
"@gitzone/tsrun": "^1.1.12",
"@gitzone/tstest": "^1.0.13",
"@pushrocks/tapbundle": "^3.0.5",
"@types/node": "^10.5.8",
"tslint": "^5.12.0",
"tslint-config-prettier": "^1.17.0"
},
"dependencies": {
"beautylog": "^5.0.12",
"lodash": "^4.13.1",
"q": "^1.4.1",
"smartfile": "^3.0.10",
"typings-global": "^1.0.3"
"@pushrocks/smartfile": "^6.0.6",
"@pushrocks/smartlog": "^2.0.9"
}
}

View File

@ -1,4 +1,4 @@
vars:
required:
- key1
- key2
- key3

2
test/test.d.ts vendored
View File

@ -1,2 +0,0 @@
import "typings-test";
import "should";

View File

@ -1,23 +0,0 @@
"use strict";
require("typings-test");
var path = require("path");
require("should");
var index_1 = require("../dist/index");
process.cwd = function () {
return path.join(__dirname, "./assets/");
};
process.env["key1"] = "original";
var qenvTestObject;
describe("Qenv class", function () {
it("should create a new class", function () {
qenvTestObject = new index_1.Qenv(process.cwd(), process.cwd());
});
it("key1 should be not be overwritten since it is already present", function () {
process.env.key1.should.equal("original");
});
it("key2 should be read from Yml", function () {
process.env.key2.should.equal("fromYml");
});
});
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FBYyxDQUFDLENBQUE7QUFDdEIsSUFBWSxJQUFJLFdBQU0sTUFBTSxDQUFDLENBQUE7QUFDN0IsUUFBTyxRQUFRLENBQUMsQ0FBQTtBQUNoQixzQkFBbUIsZUFBZSxDQUFDLENBQUE7QUFFbkMsT0FBTyxDQUFDLEdBQUcsR0FBRztJQUNWLE1BQU0sQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLFNBQVMsRUFBQyxXQUFXLENBQUMsQ0FBQztBQUM1QyxDQUFDLENBQUE7QUFFRCxPQUFPLENBQUMsR0FBRyxDQUFDLE1BQU0sQ0FBQyxHQUFHLFVBQVUsQ0FBQTtBQUVoQyxJQUFJLGNBQW1CLENBQUM7QUFDeEIsUUFBUSxDQUFDLFlBQVksRUFBQztJQUNsQixFQUFFLENBQUMsMkJBQTJCLEVBQUM7UUFDM0IsY0FBYyxHQUFHLElBQUksWUFBSSxDQUFDLE9BQU8sQ0FBQyxHQUFHLEVBQUUsRUFBQyxPQUFPLENBQUMsR0FBRyxFQUFFLENBQUMsQ0FBQztJQUMzRCxDQUFDLENBQUMsQ0FBQztJQUNILEVBQUUsQ0FBQywrREFBK0QsRUFBQztRQUMvRCxPQUFPLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQyxNQUFNLENBQUMsS0FBSyxDQUFDLFVBQVUsQ0FBQyxDQUFDO0lBQzlDLENBQUMsQ0FBQyxDQUFDO0lBQ0gsRUFBRSxDQUFDLDhCQUE4QixFQUFDO1FBQzlCLE9BQU8sQ0FBQyxHQUFHLENBQUMsSUFBSSxDQUFDLE1BQU0sQ0FBQyxLQUFLLENBQUMsU0FBUyxDQUFDLENBQUM7SUFDN0MsQ0FBQyxDQUFDLENBQUE7QUFFTixDQUFDLENBQUMsQ0FBQyIsImZpbGUiOiJ0ZXN0LmpzIiwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IFwidHlwaW5ncy10ZXN0XCI7XG5pbXBvcnQgKiBhcyBwYXRoIGZyb20gXCJwYXRoXCI7XG5pbXBvcnQgXCJzaG91bGRcIjtcbmltcG9ydCB7UWVudn0gZnJvbSBcIi4uL2Rpc3QvaW5kZXhcIjtcblxucHJvY2Vzcy5jd2QgPSAoKSA9PiB7XG4gICAgcmV0dXJuIHBhdGguam9pbihfX2Rpcm5hbWUsXCIuL2Fzc2V0cy9cIik7XG59XG5cbnByb2Nlc3MuZW52W1wia2V5MVwiXSA9IFwib3JpZ2luYWxcIlxuXG5sZXQgcWVudlRlc3RPYmplY3Q6UWVudjtcbmRlc2NyaWJlKFwiUWVudiBjbGFzc1wiLGZ1bmN0aW9uKCl7XG4gICAgaXQoXCJzaG91bGQgY3JlYXRlIGEgbmV3IGNsYXNzXCIsZnVuY3Rpb24oKXtcbiAgICAgICAgcWVudlRlc3RPYmplY3QgPSBuZXcgUWVudihwcm9jZXNzLmN3ZCgpLHByb2Nlc3MuY3dkKCkpO1xuICAgIH0pO1xuICAgIGl0KFwia2V5MSBzaG91bGQgYmUgbm90IGJlIG92ZXJ3cml0dGVuIHNpbmNlIGl0IGlzIGFscmVhZHkgcHJlc2VudFwiLGZ1bmN0aW9uKCl7XG4gICAgICAgIHByb2Nlc3MuZW52LmtleTEuc2hvdWxkLmVxdWFsKFwib3JpZ2luYWxcIik7XG4gICAgfSk7XG4gICAgaXQoXCJrZXkyIHNob3VsZCBiZSByZWFkIGZyb20gWW1sXCIsZnVuY3Rpb24oKXtcbiAgICAgICAgcHJvY2Vzcy5lbnYua2V5Mi5zaG91bGQuZXF1YWwoXCJmcm9tWW1sXCIpO1xuICAgIH0pXG5cbn0pOyJdfQ==

View File

@ -1,24 +1,33 @@
import "typings-test";
import * as path from "path";
import "should";
import {Qenv} from "../dist/index";
import * as path from 'path';
import { tap, expect } from '@pushrocks/tapbundle';
import * as qenv from '../ts/index';
process.cwd = () => {
return path.join(__dirname,"./assets/");
}
process.env['key1'] = 'original';
process.env["key1"] = "original"
let qenvTestObject:Qenv;
describe("Qenv class",function(){
it("should create a new class",function(){
qenvTestObject = new Qenv(process.cwd(),process.cwd());
});
it("key1 should be not be overwritten since it is already present",function(){
process.env.key1.should.equal("original");
});
it("key2 should be read from Yml",function(){
process.env.key2.should.equal("fromYml");
})
let qenvTestObject: qenv.Qenv;
tap.test('should create a new class', async () => {
qenvTestObject = new qenv.Qenv(
path.join(__dirname, 'assets'),
path.join(__dirname, 'assets'),
false
);
expect(qenvTestObject).to.be.instanceof(qenv.Qenv);
});
tap.test('key1 should be not be overwritten since it is already present', async () => {
expect(process.env.key1).to.equal('original');
expect(qenvTestObject.getEnvVarOnDemand('key1')).to.equal('original');
});
tap.test('key2 should be read from Yml', async () => {
expect(process.env.key2).to.equal('fromYml');
expect(qenvTestObject.getEnvVarOnDemand('key2')).to.equal('fromYml');
});
tap.test('keyValueObjectArray should hold all retrieved values', async () => {
expect(qenvTestObject.keyValueObjectArray[0].value).to.equal('original');
expect(qenvTestObject.keyValueObjectArray[1].value).to.equal('fromYml');
});
tap.start();

View File

@ -1,51 +1 @@
import * as plugins from "./qenv.plugins";
export class Qenv {
requiredEnvVars:string[];
availableEnvVars:string[];
missingEnvVars:string[];
constructor(basePathArg = process.cwd(),envYmlPathArg){
this.requiredEnvVars = getRequiredEnvVars(basePathArg);
this.availableEnvVars = getAvailableEnvVars(this.requiredEnvVars,envYmlPathArg);
this.missingEnvVars = getMissingEnvVars(this.requiredEnvVars,this.availableEnvVars);
for(let keyArg in this.missingEnvVars){
plugins.beautylog.warn(this.missingEnvVars[keyArg] + " is required, but missing!")
}
}
};
let getRequiredEnvVars = (pathArg:string):string[] => {
let result:string[] = [];
let qenvFilePath = plugins.path.join(pathArg,"qenv.yml");
let qenvFile = plugins.smartfile.local.toObjectSync(qenvFilePath);
for(let keyArg in qenvFile.vars){
result.push(qenvFile.vars[keyArg]);
}
return result;
}
let getAvailableEnvVars = (requiredEnvVarsArg:string[],envYmlPathArg:string):string[] => {
let result = [];
envYmlPathArg = plugins.path.join(envYmlPathArg,"env.yml")
let envYml;
try {
envYml = plugins.smartfile.local.toObjectSync(envYmlPathArg);
}
catch(err){
envYml = {};
}
for(let keyArg in requiredEnvVarsArg){
let requiredEnvVar:string = requiredEnvVarsArg[keyArg];
if(process.env[requiredEnvVar]){
result.push(requiredEnvVar);
} else if(envYml.hasOwnProperty(requiredEnvVar)){
process.env[requiredEnvVar] = envYml[requiredEnvVar];
result.push(requiredEnvVar);
}
}
return result;
}
let getMissingEnvVars = (requiredEnvVarsArray:string[],availableEnvVarsArray:string[]) => {
return plugins.lodash.difference(requiredEnvVarsArray,availableEnvVarsArray);
}
export * from './qenv.classes.qenv';

197
ts/qenv.classes.qenv.ts Normal file
View File

@ -0,0 +1,197 @@
import * as plugins from './qenv.plugins';
plugins.smartlog.defaultLogger.enableConsole();
export interface IKeyValueObject {
key: string;
value: string;
}
/**
* class Qenv
* allows to make assertions about the environments while being more flexibel in how to meet them
*/
export class Qenv {
public requiredEnvVars: string[] = [];
public availableEnvVars: string[] = [];
public missingEnvVars: string[] = [];
public keyValueObjectArray: IKeyValueObject[] = [];
public logger: plugins.smartlog.Smartlog;
// filePaths
public qenvFilePathAbsolute: string;
public envFilePathAbsolute: string;
constructor(
qenvFileBasePathArg = process.cwd(),
envFileBasePathArg,
failOnMissing = true,
loggerArg: plugins.smartlog.Smartlog = plugins.smartlog.defaultLogger
) {
this.logger = loggerArg;
// lets make sure paths are absolute
this.qenvFilePathAbsolute = plugins.path.join(
plugins.path.resolve(qenvFileBasePathArg),
'qenv.yml'
);
this.envFilePathAbsolute = plugins.path.join(
plugins.path.resolve(envFileBasePathArg),
'env.yml'
);
this.getRequiredEnvVars();
this.getAvailableEnvVars();
this.missingEnvVars = this.getMissingEnvVars();
// handle missing variables
if (this.missingEnvVars.length > 0) {
console.info('Required Env Vars are:');
console.log(this.requiredEnvVars);
console.error('However some Env variables could not be resolved:');
console.log(this.missingEnvVars);
if (failOnMissing) {
console.error('Exiting!');
process.exit(1);
}
}
}
/**
* only gets an environment variable if it is required within a read qenv.yml file
* @param envVarName
*/
public getEnvVarRequired(envVarName): string {
return process.env[envVarName];
}
/**
* tries to get any env var even if it is not required
* @param requiredEnvVar
*/
public getEnvVarOnDemand(requiredEnvVar: string): string {
// lets determine the actual env yml
let envYml;
try {
envYml = plugins.smartfile.fs.toObjectSync(this.envFilePathAbsolute);
} catch (err) {
envYml = {};
}
let envVar: string;
let envFileVar: string;
let dockerSecret: string;
let dockerSecretJson: string;
// env var check
if (process.env[requiredEnvVar]) {
this.availableEnvVars.push(requiredEnvVar);
envVar = process.env[requiredEnvVar];
}
// env file check
if (envYml.hasOwnProperty(requiredEnvVar)) {
envFileVar = envYml[requiredEnvVar];
this.availableEnvVars.push(requiredEnvVar);
}
// docker secret check
if (
plugins.smartfile.fs.isDirectory('/run') &&
plugins.smartfile.fs.isDirectory('/run/secrets') &&
plugins.smartfile.fs.fileExistsSync(`/run/secrets/${requiredEnvVar}`)
) {
dockerSecret = plugins.smartfile.fs.toStringSync(`/run/secrets/${requiredEnvVar}`);
}
// docker secret.json
if (
plugins.smartfile.fs.isDirectory('/run') &&
plugins.smartfile.fs.isDirectory('/run/secrets')
) {
const availableSecrets = plugins.smartfile.fs.listAllItemsSync('/run/secrets');
for (const secret of availableSecrets) {
if (secret.includes('secret.json') && !dockerSecret) {
const secretObject = plugins.smartfile.fs.toObjectSync(`/run/secrets/${secret}`);
dockerSecret = secretObject[requiredEnvVar];
}
}
}
// warn if there is more than one candidate
let candidatesCounter = 0;
[envVar, envFileVar, dockerSecret, dockerSecretJson].forEach(candidate => {
if (candidate) {
candidatesCounter++;
}
});
if (candidatesCounter > 1) {
this.logger.log(
'warn',
`found multiple candidates for ${requiredEnvVar} Choosing in the order of envVar, envFileVar, dockerSecret, dockerSecretJson`
);
}
let chosenVar: string = null;
if (envVar) {
this.logger.log('ok', `found ${requiredEnvVar} as environment variable`);
chosenVar = envVar;
} else if (envFileVar) {
this.logger.log('ok', `found ${requiredEnvVar} as env.yml variable`);
chosenVar = envFileVar;
} else if (dockerSecret) {
this.logger.log('ok', `found ${requiredEnvVar} as docker secret`);
chosenVar = dockerSecret;
} else if (dockerSecretJson) {
this.logger.log('ok', `found ${requiredEnvVar} as docker secret.json`);
chosenVar = dockerSecretJson;
}
return chosenVar;
}
/**
* gets the required env values
*/
private getRequiredEnvVars = () => {
let qenvFile: any = {};
if (plugins.smartfile.fs.fileExistsSync(this.qenvFilePathAbsolute)) {
qenvFile = plugins.smartfile.fs.toObjectSync(this.qenvFilePathAbsolute);
}
if (!qenvFile || !qenvFile.required || !Array.isArray(qenvFile.required)) {
this.logger.log('warn', `env File does not contain a 'required' Array!`);
} else {
for (const keyArg of Object.keys(qenvFile.required)) {
this.requiredEnvVars.push(qenvFile.required[keyArg]);
}
}
};
/**
* gets the available env vars
*/
private getAvailableEnvVars = () => {
for (const requiredEnvVar of this.requiredEnvVars) {
const chosenVar = this.getEnvVarOnDemand(requiredEnvVar);
if (chosenVar) {
this.availableEnvVars.push(requiredEnvVar);
process.env[requiredEnvVar] = chosenVar;
this.keyValueObjectArray.push({
key: requiredEnvVar,
value: chosenVar
});
}
}
};
/**
* gets missing env vars
*/
private getMissingEnvVars = (): string[] => {
const missingEnvVars: string[] = [];
for (const envVar of this.requiredEnvVars) {
if (!this.availableEnvVars.includes(envVar)) {
missingEnvVars.push(envVar);
}
}
return missingEnvVars;
};
}

View File

@ -1,5 +1,16 @@
import "typings-global";
export import beautylog = require("beautylog");
export let lodash = require("lodash");
export import path = require("path");
export import smartfile = require("smartfile");
// native
import * as path from 'path';
export {
path
}
// @pushrocks scope
import * as smartfile from '@pushrocks/smartfile';
import * as smartlog from '@pushrocks/smartlog';
export {
smartfile,
smartlog
}

17
tslint.json Normal file
View File

@ -0,0 +1,17 @@
{
"extends": ["tslint:latest", "tslint-config-prettier"],
"rules": {
"semicolon": [true, "always"],
"no-console": false,
"ordered-imports": false,
"object-literal-sort-keys": false,
"member-ordering": {
"options":{
"order": [
"static-method"
]
}
}
},
"defaultSeverity": "warning"
}