Skip to content

Commit

Permalink
Added --output-folder flag to scaffold (#3805)
Browse files Browse the repository at this point in the history
  • Loading branch information
KabaevRoman authored Feb 18, 2025
1 parent aa34396 commit 0b550a3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
13 changes: 9 additions & 4 deletions cli/commands/scaffold/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ func Run(ctx context.Context, opts *options.TerragruntOptions, moduleURL, templa
}
}()

outputDir := opts.ScaffoldOutputFolder
if outputDir == "" {
outputDir = opts.WorkingDir
}

// scaffold only in empty directories
if empty, err := util.IsDirectoryEmpty(opts.WorkingDir); !empty || err != nil {
if err != nil {
Expand Down Expand Up @@ -149,7 +154,7 @@ func Run(ctx context.Context, opts *options.TerragruntOptions, moduleURL, templa
return errors.New(err)
}

opts.Logger.Infof("Scaffolding a new Terragrunt module %s to %s", moduleURL, opts.WorkingDir)
opts.Logger.Infof("Scaffolding a new Terragrunt module %s to %s", moduleURL, outputDir)

if _, err := getter.GetAny(ctx, tempDir, moduleURL); err != nil {
return errors.New(err)
Expand Down Expand Up @@ -188,9 +193,9 @@ func Run(ctx context.Context, opts *options.TerragruntOptions, moduleURL, templa
opts.Logger.Warnf("The %s variable is already set in the var flag(s). The --%s flag will be ignored.", rootFileName, NoIncludeRootFlagName)
}

opts.Logger.Infof("Running boilerplate generation to %s", opts.WorkingDir)
opts.Logger.Infof("Running boilerplate generation to %s", outputDir)
boilerplateOpts := &boilerplate_options.BoilerplateOptions{
OutputFolder: opts.WorkingDir,
OutputFolder: outputDir,
OnMissingKey: boilerplate_options.DefaultMissingKeyAction,
OnMissingConfig: boilerplate_options.DefaultMissingConfigAction,
Vars: vars,
Expand All @@ -205,7 +210,7 @@ func Run(ctx context.Context, opts *options.TerragruntOptions, moduleURL, templa
return errors.New(err)
}

opts.Logger.Infof("Running fmt on generated code %s", opts.WorkingDir)
opts.Logger.Infof("Running fmt on generated code %s", outputDir)

if err := hclfmt.Run(opts); err != nil {
return errors.New(err)
Expand Down
7 changes: 7 additions & 0 deletions cli/commands/scaffold/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (

RootFileNameFlagName = "root-file-name"
NoIncludeRootFlagName = "no-include-root"
OutputFolderFlagName = "output-folder"
VarFlagName = "var"
VarFileFlagName = "var-file"
)
Expand Down Expand Up @@ -54,6 +55,12 @@ func NewFlags(opts *options.TerragruntOptions, prefix flags.Prefix) cli.Flags {
Usage: "Do not include root unit in scaffolding done by catalog.",
}),

flags.NewFlag(&cli.GenericFlag[string]{
Name: OutputFolderFlagName,
Destination: &opts.ScaffoldOutputFolder,
Usage: "Output folder for scaffold output.",
}),

flags.NewFlag(&cli.SliceFlag[string]{
Name: VarFlagName,
EnvVars: tgPrefix.EnvVars(VarFlagName),
Expand Down
3 changes: 2 additions & 1 deletion docs/_docs/02_features/05-catalog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Launch the user interface for searching and managing your module catalog.
Example:

```bash
terragrunt catalog <repo-url> [--no-include-root] [--root-file-name]
terragrunt catalog <repo-url> [--no-include-root] [--root-file-name] [--output-folder]
```

[![screenshot](/assets/img/screenshots/catalog-screenshot.png){: width="50%" }](https://terragrunt.gruntwork.io/assets/img/screenshots/catalog-screenshot.png)
Expand Down Expand Up @@ -52,3 +52,4 @@ The following `catalog` flags control behavior of the underlying `scaffold` comm

- `--no-include-root` - Do not include the root configuration file in any generated `terragrunt.hcl` during scaffolding.
- `--root-file-name` - The name of the root configuration file to include in any generated `terragrunt.hcl` during scaffolding. This value also controls the name of the root configuration file to search for when trying to determine Catalog urls.
- `--output-folder` - Location for generated `terragrunt.hcl`. If flag is not provided current working directory is selected.
3 changes: 3 additions & 0 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ type TerragruntOptions struct {
// Name of the root Terragrunt configuration file, if used.
ScaffoldRootFileName string

// Path to folder of scaffold output
ScaffoldOutputFolder string

// Root directory for graph command.
GraphRoot string

Expand Down
12 changes: 12 additions & 0 deletions test/integration_scaffold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,15 @@ func TestScaffold3rdPartyModule(t *testing.T) {
_, _, err = helpers.RunTerragruntCommandWithOutput(t, "terragrunt hclvalidate --terragrunt-non-interactive --terragrunt-working-dir "+tmpEnvPath)
require.NoError(t, err)
}

func TestScaffoldOutputFolderFlag(t *testing.T) {
t.Parallel()

tmpEnvPath := t.TempDir()

outputFolder := tmpEnvPath + "/foo/bar"
_, stderr, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt --terragrunt-non-interactive --terragrunt-working-dir %s scaffold %s --output-folder %s", tmpEnvPath, testScaffoldModuleURL, outputFolder))
require.NoError(t, err)
assert.Contains(t, stderr, "Scaffolding completed")
assert.FileExists(t, outputFolder+"/terragrunt.hcl")
}

0 comments on commit 0b550a3

Please sign in to comment.