Skip to content

Commit 9ce88d1

Browse files
authored
feat: Generate dependencies concurrently (#283)
* feat: Generate dependencies concurrently * docs: Documenting `--parallelism` * fix: Adding some testing * fix: Sorting with best practices for Golang funcs * fix: Removing silly tests * fix: Removing unused imports * fix: Use no limit when `Parallelism()` isn't set * fix: Setting `ctx` parameter for `doProcess` * fix: Resolving conflicts * fix: Preventing non-deterministic ordering of dependencies, despite concurrent generation * fix: Propagating manifest management to child templates * fix: Addressing lint findings * feat: Adding external manifest management controls * fix: Use byte slice / file instead of parsed manifest for validate * fix: Making it more straightforward to handle future versions of the schema * fix: Addressing lint findings * fix: Adding tests to the `v1` package
1 parent a9c6c3e commit 9ce88d1

File tree

17 files changed

+1913
-406
lines changed

17 files changed

+1913
-406
lines changed

cli/boilerplate_cli.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import (
55
"context"
66
"fmt"
77
"path/filepath"
8+
"runtime"
89

910
"github.com/urfave/cli/v2"
1011

11-
"github.com/gruntwork-io/boilerplate/internal/manifest"
12+
"github.com/gruntwork-io/boilerplate/manifest"
1213
"github.com/gruntwork-io/boilerplate/options"
1314
"github.com/gruntwork-io/boilerplate/templates"
1415
"github.com/gruntwork-io/boilerplate/variables"
@@ -105,6 +106,11 @@ func CreateBoilerplateCli() *cli.App {
105106
Name: options.OptManifestFile,
106107
Usage: "Write the manifest to `FILE` instead of the default location. Implies --manifest. Format is auto-detected from extension (.yaml/.yml for YAML, otherwise JSON).",
107108
},
109+
&cli.IntFlag{
110+
Name: options.OptParallelism,
111+
Value: runtime.NumCPU(),
112+
Usage: "Maximum number of parallel operations Boilerplate will perform at once (default: number of CPUs).",
113+
},
108114
}
109115

110116
// We pass JSON/YAML content to various CLI flags, such as --var, and this JSON/YAML content may contain commas or

cli/parse_options.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,21 @@ func ParseCLIContext(cliContext *cli.Context) (*options.BoilerplateOptions, erro
4141
}
4242

4343
opts := &options.BoilerplateOptions{
44+
Vars: vars,
45+
ShellCommandAnswers: make(map[string]bool),
4446
TemplateURL: templateURL,
4547
TemplateFolder: templateFolder,
4648
OutputFolder: cliContext.String(options.OptOutputFolder),
47-
NonInteractive: cliContext.Bool(options.OptNonInteractive),
4849
OnMissingKey: missingKeyAction,
4950
OnMissingConfig: missingConfigAction,
50-
Vars: vars,
51+
NonInteractive: cliContext.Bool(options.OptNonInteractive),
5152
NoHooks: cliContext.Bool(options.OptNoHooks),
5253
NoShell: cliContext.Bool(options.OptNoShell),
5354
DisableDependencyPrompt: cliContext.Bool(options.OptDisableDependencyPrompt),
5455
ExecuteAllShellCommands: false,
55-
ShellCommandAnswers: make(map[string]bool),
5656
Manifest: cliContext.Bool(options.OptManifest) || cliContext.String(options.OptManifestFile) != "",
5757
ManifestFile: cliContext.String(options.OptManifestFile),
58+
Parallelism: cliContext.Int(options.OptParallelism),
5859
}
5960

6061
if err := validateOptions(opts); err != nil {

docs/src/content/docs/cli/flags.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ boilerplate [options]
5656
| `--disable-dependency-prompt` | `false` | Skip confirmation prompts for dependencies (keeps variable prompts) |
5757
| `--no-hooks` | `false` | Don't execute any hooks |
5858
| `--no-shell` | `false` | Don't execute shell helpers (returns `"replace-me"` instead) |
59+
| `--parallelism` | Number of CPUs | Maximum number of concurrent parallel operations Boilerplate will perform. Use `--parallelism=1` to disable concurrency |
5960

6061
## Manifest Flags
6162

docs/src/content/docs/configuration/dependencies.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ dependencies:
169169

170170
Inside the child template, access the current item with `{{ "{{" }} .__each__ {{ "}}" }}`.
171171

172+
`for_each` iterations are processed **concurrently** by default, using up to one goroutine per CPU. You can control this with the [`--parallelism`](/docs/cli/flags/) flag. If your dependency has side effects that require sequential execution (e.g., writing to the same file, depending on ordering), disable concurrency with:
173+
174+
```bash
175+
boilerplate --parallelism=1 ...
176+
```
177+
172178
### `for_each_reference`
173179

174180
Reference a list variable instead of a static list:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ require (
1818
github.com/stuart-warren/yamlfmt v0.1.2
1919
github.com/urfave/cli/v2 v2.27.7
2020
github.com/xeipuuv/gojsonschema v1.2.0
21+
golang.org/x/sync v0.19.0
2122
golang.org/x/text v0.33.0
2223
gopkg.in/yaml.v3 v3.0.1
2324
)
@@ -119,7 +120,6 @@ require (
119120
golang.org/x/crypto v0.47.0 // indirect
120121
golang.org/x/net v0.49.0 // indirect
121122
golang.org/x/oauth2 v0.34.0 // indirect
122-
golang.org/x/sync v0.19.0 // indirect
123123
golang.org/x/sys v0.40.0 // indirect
124124
golang.org/x/term v0.39.0 // indirect
125125
golang.org/x/time v0.14.0 // indirect

integration-tests/examples_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
"github.com/gruntwork-io/boilerplate/cli"
1515
"github.com/gruntwork-io/boilerplate/internal/fileutil"
16-
"github.com/gruntwork-io/boilerplate/internal/manifest"
16+
"github.com/gruntwork-io/boilerplate/manifest"
1717
"github.com/gruntwork-io/boilerplate/options"
1818
)
1919

internal/manifest/manifest_test.go

Lines changed: 0 additions & 290 deletions
This file was deleted.

0 commit comments

Comments
 (0)