diff --git a/docs/reference/template-strings.md b/docs/reference/template-strings.md index 796ec0dd8e..0dcb9ea305 100644 --- a/docs/reference/template-strings.md +++ b/docs/reference/template-strings.md @@ -3,12 +3,13 @@ Below you'll find the schema for the keys available when interpolating template strings (see our [Configuration Files](../using-garden/configuration-files.md) guide for information and usage examples). -Note that there are two sections below, because Project configs and Module configs have different keys available to -them. Please make sure to refer to the correct section. +Note that there are three sections below, because Project configs and Module configs have different keys available to +them, and additional keys are available under `providers` in Project configs. +Please make sure to refer to the correct section. ## Project configuration context -The following keys are available in template strings under the `project` key in `garden.yml` files: +The following keys are available in template strings anywhere in Project `garden.yml` config files: ```yaml # Type: object @@ -29,11 +30,90 @@ local: # Example: "posix" # platform: + + # The current username (as resolved by https://github.com/sindresorhus/username) + # + # Type: string + # + # Example: "tenzing_norgay" + # + username: +``` + +## Provider configuration context + +The following keys are available in template strings under the `providers` key (or `environments[].providers) +in Project `garden.yml` config files: + +```yaml +# Type: object +# +local: + # A map of all local environment variables (see + # https://nodejs.org/api/process.html#process_process_env). + # + # Type: object + # + env: + + # A string indicating the platform that the framework is running on (see + # https://nodejs.org/api/process.html#process_process_platform) + # + # Type: string + # + # Example: "posix" + # + platform: + + # The current username (as resolved by https://github.com/sindresorhus/username) + # + # Type: string + # + # Example: "tenzing_norgay" + # + username: + +# Information about the environment that Garden is running against. +# +# Type: object +# +environment: + # The name of the environment Garden is running against. + # + # Type: string + # + # Example: "local" + # + name: + +# Information about the Garden project. +# +# Type: object +# +project: + # The name of the Garden project. + # + # Type: string + # + # Example: "my-project" + # + name: + +# Retrieve information about providers that are defined in the project. +# +# Type: object +# +# Example: +# kubernetes: +# config: +# clusterHostname: my-cluster.example.com +# +providers: {} ``` ## Module configuration context -The following keys are available in template strings under the `module` key in `garden.yml` files: +The following keys are available in template strings under Module `garden.yml` config files: ```yaml # Type: object @@ -55,6 +135,14 @@ local: # platform: + # The current username (as resolved by https://github.com/sindresorhus/username) + # + # Type: string + # + # Example: "tenzing_norgay" + # + username: + # Information about the environment that Garden is running against. # # Type: object @@ -68,6 +156,19 @@ environment: # name: +# Information about the Garden project. +# +# Type: object +# +project: + # The name of the Garden project. + # + # Type: string + # + # Example: "my-project" + # + name: + # Retrieve information about providers that are defined in the project. # # Type: object diff --git a/garden-service/package-lock.json b/garden-service/package-lock.json index 1e3be98f72..f86b6c5387 100644 --- a/garden-service/package-lock.json +++ b/garden-service/package-lock.json @@ -12936,6 +12936,15 @@ "integrity": "sha512-cwESVXlO3url9YWlFW/TA9cshCEhtu7IKJ/p5soJ/gGpj7vbvFrAY/eIioQ6Dw23KjZhYgiIo8HOs1nQ2vr/oQ==", "dev": true }, + "username": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/username/-/username-5.1.0.tgz", + "integrity": "sha512-PCKbdWw85JsYMvmCv5GH3kXmM66rCd9m1hBEDutPNv94b/pqCMT4NtcKyeWYvLFiE8b+ha1Jdl8XAaUdPn5QTg==", + "requires": { + "execa": "^1.0.0", + "mem": "^4.3.0" + } + }, "util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", diff --git a/garden-service/package.json b/garden-service/package.json index 9f925f65db..4fe295848b 100644 --- a/garden-service/package.json +++ b/garden-service/package.json @@ -99,6 +99,7 @@ "typescript-memoize": "^1.0.0-alpha.3", "uniqid": "^5.0.3", "unzipper": "^0.9.11", + "username": "^5.1.0", "uuid": "^3.3.2", "winston": "^3.2.1", "wrap-ansi": "^5.1.0", diff --git a/garden-service/src/config/config-context.ts b/garden-service/src/config/config-context.ts index 8597a47f75..1ba09b6b85 100644 --- a/garden-service/src/config/config-context.ts +++ b/garden-service/src/config/config-context.ts @@ -6,6 +6,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +import username = require("username") import { isString } from "lodash" import { PrimitiveMap, isPrimitive, Primitive, joiIdentifierMap, joiStringMap, joiPrimitive } from "./common" import { Provider, ProviderConfig } from "./provider" @@ -173,10 +174,26 @@ class LocalContext extends ConfigContext { ) public platform: string + @schema( + Joi.string() + .description( + "The current username (as resolved by https://github.com/sindresorhus/username)", + ) + .example("tenzing_norgay"), + ) + public username: () => Promise + constructor(root: ConfigContext) { super(root) this.env = process.env this.platform = process.platform + this.username = async () => { + const name = await username() + if (name === undefined) { + throw new ConfigurationError(`Could not resolve current username`, {}) + } + return name + } } } @@ -193,6 +210,20 @@ export class ProjectConfigContext extends ConfigContext { } } +class ProjectContext extends ConfigContext { + @schema( + Joi.string() + .description("The name of the Garden project.") + .example("my-project"), + ) + public name: string + + constructor(root: ConfigContext, name: string) { + super(root) + this.name = name + } +} + class EnvironmentContext extends ConfigContext { @schema( Joi.string() @@ -238,6 +269,12 @@ export class ProviderConfigContext extends ProjectConfigContext { ) public environment: EnvironmentContext + @schema( + ProjectContext.getSchema() + .description("Information about the Garden project."), + ) + public project: ProjectContext + @schema( joiIdentifierMap(ProviderContext.getSchema()) .description("Retrieve information about providers that are defined in the project.") @@ -245,11 +282,12 @@ export class ProviderConfigContext extends ProjectConfigContext { ) public providers: Map - constructor(environmentName: string, resolvedProviders: Provider[]) { + constructor(environmentName: string, projectName: string, resolvedProviders: Provider[]) { super() const _this = this this.environment = new EnvironmentContext(this, environmentName) + this.project = new ProjectContext(this, projectName) this.providers = new Map(resolvedProviders.map(p => <[string, ProviderContext]>[p.name, new ProviderContext(_this, p)], @@ -322,7 +360,7 @@ export class ModuleConfigContext extends ProviderConfigContext { variables: PrimitiveMap, moduleConfigs: ModuleConfig[], ) { - super(environmentName, resolvedProviders) + super(environmentName, garden.projectName, resolvedProviders) const _this = this diff --git a/garden-service/src/docs/template-strings.ts b/garden-service/src/docs/template-strings.ts index a0eb9dd182..eb6218f99e 100644 --- a/garden-service/src/docs/template-strings.ts +++ b/garden-service/src/docs/template-strings.ts @@ -8,7 +8,7 @@ import { resolve } from "path" import { renderSchemaDescriptionYaml, normalizeDescriptions, TEMPLATES_DIR } from "./config" -import { ProjectConfigContext, ModuleConfigContext } from "../config/config-context" +import { ProjectConfigContext, ModuleConfigContext, ProviderConfigContext } from "../config/config-context" import { readFileSync, writeFileSync } from "fs" import * as handlebars from "handlebars" import { GARDEN_SERVICE_ROOT } from "../constants" @@ -19,12 +19,16 @@ export function writeTemplateStringReferenceDocs(docsRoot: string) { const projectDescriptions = normalizeDescriptions(ProjectConfigContext.getSchema().describe()) const projectContext = renderSchemaDescriptionYaml(projectDescriptions, { showRequired: false }) + + const providerDescriptions = normalizeDescriptions(ProviderConfigContext.getSchema().describe()) + const providerContext = renderSchemaDescriptionYaml(providerDescriptions, { showRequired: false }) + const moduleDescriptions = normalizeDescriptions(ModuleConfigContext.getSchema().describe()) const moduleContext = renderSchemaDescriptionYaml(moduleDescriptions, { showRequired: false }) const templatePath = resolve(TEMPLATES_DIR, "template-strings.hbs") const template = handlebars.compile(readFileSync(templatePath).toString()) - const markdown = template({ projectContext, moduleContext }) + const markdown = template({ projectContext, providerContext, moduleContext }) writeFileSync(outputPath, markdown) } diff --git a/garden-service/src/docs/templates/template-strings.hbs b/garden-service/src/docs/templates/template-strings.hbs index e3f4aebf27..3053fe29e1 100644 --- a/garden-service/src/docs/templates/template-strings.hbs +++ b/garden-service/src/docs/templates/template-strings.hbs @@ -3,20 +3,30 @@ Below you'll find the schema for the keys available when interpolating template strings (see our [Configuration Files](../using-garden/configuration-files.md) guide for information and usage examples). -Note that there are two sections below, because Project configs and Module configs have different keys available to -them. Please make sure to refer to the correct section. +Note that there are three sections below, because Project configs and Module configs have different keys available to +them, and additional keys are available under `providers` in Project configs. +Please make sure to refer to the correct section. ## Project configuration context -The following keys are available in template strings under the `project` key in `garden.yml` files: +The following keys are available in template strings anywhere in Project `garden.yml` config files: ```yaml {{{projectContext}}} ``` +## Provider configuration context + +The following keys are available in template strings under the `providers` key (or `environments[].providers) +in Project `garden.yml` config files: + +```yaml +{{{providerContext}}} +``` + ## Module configuration context -The following keys are available in template strings under the `module` key in `garden.yml` files: +The following keys are available in template strings under Module `garden.yml` config files: ```yaml {{{moduleContext}}} diff --git a/garden-service/src/tasks/resolve-provider.ts b/garden-service/src/tasks/resolve-provider.ts index a209356150..e574897748 100644 --- a/garden-service/src/tasks/resolve-provider.ts +++ b/garden-service/src/tasks/resolve-provider.ts @@ -76,7 +76,7 @@ export class ResolveProviderTask extends BaseTask { async process(dependencyResults: TaskResults) { const resolvedProviders: Provider[] = Object.values(dependencyResults).map(result => result.output) - const context = new ProviderConfigContext(this.garden.environmentName, resolvedProviders) + const context = new ProviderConfigContext(this.garden.environmentName, this.garden.projectName, resolvedProviders) let resolvedConfig = await resolveTemplateStrings(this.config, context) resolvedConfig.path = this.garden.projectRoot