Skip to content

Commit

Permalink
feat(config): add local.username and project.name config keys
Browse files Browse the repository at this point in the history
Also added a Provider configuration section to the template strings
reference.
  • Loading branch information
edvald committed Jun 22, 2019
1 parent 52ad5fa commit 8fb9b5f
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 13 deletions.
109 changes: 105 additions & 4 deletions docs/reference/template-strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions garden-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions garden-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
42 changes: 40 additions & 2 deletions garden-service/src/config/config-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<string>

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
}
}
}

Expand All @@ -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()
Expand Down Expand Up @@ -238,18 +269,25 @@ 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.")
.example(providersExample),
)
public providers: Map<string, ProviderContext>

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)],
Expand Down Expand Up @@ -322,7 +360,7 @@ export class ModuleConfigContext extends ProviderConfigContext {
variables: PrimitiveMap,
moduleConfigs: ModuleConfig[],
) {
super(environmentName, resolvedProviders)
super(environmentName, garden.projectName, resolvedProviders)

const _this = this

Expand Down
8 changes: 6 additions & 2 deletions garden-service/src/docs/template-strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand Down
18 changes: 14 additions & 4 deletions garden-service/src/docs/templates/template-strings.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}
Expand Down
2 changes: 1 addition & 1 deletion garden-service/src/tasks/resolve-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8fb9b5f

Please sign in to comment.