Skip to content

Commit

Permalink
feat(core): lower case username for templates
Browse files Browse the repository at this point in the history
In addition to the `local.username` template variable, we now provide
`local.usernameLowerCase`.

This is useful e.g. when templating in the local username in namespaces
or hostnames.
  • Loading branch information
thsig authored and edvald committed Feb 19, 2021
1 parent 15f2ab5 commit d6b94aa
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 7 deletions.
16 changes: 15 additions & 1 deletion core/src/config/config-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,32 @@ class LocalContext extends ConfigContext {
@schema(
joi
.string()
.description("The current username (as resolved by https://github.com/sindresorhus/username)")
.description("The current username (as resolved by https://github.com/sindresorhus/username).")
.example("tenzing_norgay")
)
public username?: string

@schema(
joi
.string()
.description(
deline`
The current username (as resolved by https://github.com/sindresorhus/username), with any upper case
characters converted to lower case.
`
)
.example("tenzing_norgay")
)
public usernameLowerCase?: string

constructor(root: ConfigContext, artifactsPath: string, projectRoot: string, username?: string) {
super(root)
this.artifactsPath = artifactsPath
this.env = process.env
this.platform = process.platform
this.projectPath = projectRoot
this.username = username
this.usernameLowerCase = username ? username.toLowerCase() : undefined
}
}

Expand Down
17 changes: 17 additions & 0 deletions core/test/unit/src/config/config-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,23 @@ describe("ProjectConfigContext", () => {
resolved: process.platform,
})
})

it("should resolve the local username (both regular and lower case versions)", () => {
const c = new ProjectConfigContext({
projectName: "some-project",
projectRoot: "/tmp",
artifactsPath: "/tmp",
branch: "main",
username: "SomeUser",
secrets: {},
})
expect(c.resolve({ key: ["local", "username"], nodePath: [], opts: {} })).to.eql({
resolved: "SomeUser",
})
expect(c.resolve({ key: ["local", "usernameLowerCase"], nodePath: [], opts: {} })).to.eql({
resolved: "someuser",
})
})
})

describe("ProviderConfigContext", () => {
Expand Down
96 changes: 90 additions & 6 deletions docs/reference/template-strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ my-variable: ${local.projectPath}

### `${local.username}`

The current username (as resolved by https://github.com/sindresorhus/username)
The current username (as resolved by https://github.com/sindresorhus/username).

| Type |
| -------- |
Expand All @@ -94,6 +94,20 @@ Example:
my-variable: ${local.username}
```

### `${local.usernameLowerCase}`

The current username (as resolved by https://github.com/sindresorhus/username), with any upper case characters converted to lower case.

| Type |
| -------- |
| `string` |

Example:

```yaml
my-variable: ${local.usernameLowerCase}
```

### `${project.*}`

Information about the Garden project.
Expand Down Expand Up @@ -220,7 +234,7 @@ my-variable: ${local.projectPath}

### `${local.username}`

The current username (as resolved by https://github.com/sindresorhus/username)
The current username (as resolved by https://github.com/sindresorhus/username).

| Type |
| -------- |
Expand All @@ -232,6 +246,20 @@ Example:
my-variable: ${local.username}
```

### `${local.usernameLowerCase}`

The current username (as resolved by https://github.com/sindresorhus/username), with any upper case characters converted to lower case.

| Type |
| -------- |
| `string` |

Example:

```yaml
my-variable: ${local.usernameLowerCase}
```

### `${project.*}`

Information about the Garden project.
Expand Down Expand Up @@ -390,7 +418,7 @@ my-variable: ${local.projectPath}

### `${local.username}`

The current username (as resolved by https://github.com/sindresorhus/username)
The current username (as resolved by https://github.com/sindresorhus/username).

| Type |
| -------- |
Expand All @@ -402,6 +430,20 @@ Example:
my-variable: ${local.username}
```

### `${local.usernameLowerCase}`

The current username (as resolved by https://github.com/sindresorhus/username), with any upper case characters converted to lower case.

| Type |
| -------- |
| `string` |

Example:

```yaml
my-variable: ${local.usernameLowerCase}
```

### `${project.*}`

Information about the Garden project.
Expand Down Expand Up @@ -653,7 +695,7 @@ my-variable: ${local.projectPath}

### `${local.username}`

The current username (as resolved by https://github.com/sindresorhus/username)
The current username (as resolved by https://github.com/sindresorhus/username).

| Type |
| -------- |
Expand All @@ -665,6 +707,20 @@ Example:
my-variable: ${local.username}
```

### `${local.usernameLowerCase}`

The current username (as resolved by https://github.com/sindresorhus/username), with any upper case characters converted to lower case.

| Type |
| -------- |
| `string` |

Example:

```yaml
my-variable: ${local.usernameLowerCase}
```

### `${project.*}`

Information about the Garden project.
Expand Down Expand Up @@ -1084,7 +1140,7 @@ my-variable: ${local.projectPath}

### `${local.username}`

The current username (as resolved by https://github.com/sindresorhus/username)
The current username (as resolved by https://github.com/sindresorhus/username).

| Type |
| -------- |
Expand All @@ -1096,6 +1152,20 @@ Example:
my-variable: ${local.username}
```

### `${local.usernameLowerCase}`

The current username (as resolved by https://github.com/sindresorhus/username), with any upper case characters converted to lower case.

| Type |
| -------- |
| `string` |

Example:

```yaml
my-variable: ${local.usernameLowerCase}
```

### `${project.*}`

Information about the Garden project.
Expand Down Expand Up @@ -1512,7 +1582,7 @@ my-variable: ${local.projectPath}

### `${local.username}`

The current username (as resolved by https://github.com/sindresorhus/username)
The current username (as resolved by https://github.com/sindresorhus/username).

| Type |
| -------- |
Expand All @@ -1524,6 +1594,20 @@ Example:
my-variable: ${local.username}
```

### `${local.usernameLowerCase}`

The current username (as resolved by https://github.com/sindresorhus/username), with any upper case characters converted to lower case.

| Type |
| -------- |
| `string` |

Example:

```yaml
my-variable: ${local.usernameLowerCase}
```

### `${project.*}`

Information about the Garden project.
Expand Down

0 comments on commit d6b94aa

Please sign in to comment.