pulumi/sdk/nodejs/runtime/config.ts
Joe Duffy 444ebdd1b5
Improve failure messages (#932)
This improves the failure messages in two circumstances:

1) If the resource monitor RPC connection is missing.  This can happen
   two ways: either you run a Pulumi program using vanilla Node.js, instead
   of the CLI, or you've accidentally loaded the Pulumi SDK more than once.

2) Failure to load the custom Pulumi SDK Node.js extension.  This is a new
   addition and would happen if you tried running a Pulumi program using a
   vanilla Node.js, rather than using the Pulumi CLI.
2018-02-14 09:55:02 -08:00

71 lines
2.1 KiB
TypeScript

// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
/**
* configEnvKey is the environment variable key for configuration that we will check in the event that a
* configuration variable is missing. Explicit overrides take precedence.
*/
export const configEnvKey = "PULUMI_CONFIG";
const config: {[key: string]: string} = {};
/**
* setConfig sets a configuration variable.
*/
export function setConfig(k: string, v: string): void {
config[k] = v;
}
/**
* getConfig returns a configuration variable's value or undefined if it is unset.
*/
export function getConfig(k: string): string | undefined {
// If the config has been set explicitly, use it.
if (config.hasOwnProperty(k)) {
return config[k];
}
// If there is a specific PULUMI_CONFIG_<k> variable, use it.
const envKey: string = getConfigEnvKey(k);
if (process.env.hasOwnProperty(envKey)) {
return process.env[envKey];
}
// If the config hasn't been set, but there is a process-wide PULUMI_CONFIG envvar, use it.
const envObject: {[key: string]: string} = getConfigEnv();
if (envObject.hasOwnProperty(k)) {
return envObject[k];
}
return undefined;
}
/**
* getConfigEnvKey returns a scrubbed environment variable key, PULUMI_CONFIG_<k>, that can be used for
* setting explicit varaibles. This is unlike PULUMI_CONFIG which is just a JSON-serialized bag.
*/
export function getConfigEnvKey(key: string): string {
let envkey: string = "";
for (const c of key) {
if (c === "_" || (c >= "A" && c <= "Z") || (c >= "0" && c <= "9")) {
envkey += c;
}
else if (c >= "a" && c <= "z") {
envkey += c.toUpperCase();
}
else {
envkey += "_";
}
}
return `${configEnvKey}_${envkey}`;
}
/**
* getConfigEnv returns the environment map that will be used for config checking when variables aren't set.
*/
export function getConfigEnv(): {[key: string]: string} {
const envConfig = process.env.PULUMI_CONFIG;
if (envConfig) {
return JSON.parse(envConfig);
}
return {};
}