Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dry run #4

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ You can also pass *.tf via arguments:
tfmv -j tfmv.jsonnet foo/aws_s3_bucket.tf foo/aws_instance.tf
```

### Dry Run: --dry-run

With `--dry-run`, tfmv outputs logs but doesn't rename blocks.

```sh
tfmv -j tfmv.jsonnet --dry-run bar/main.tf
```

### Change the filename for moved blocks

By default tfmv writes moved blocks to `moved.tf`.
Expand Down
6 changes: 5 additions & 1 deletion pkg/cli/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (r *Runner) Run(ctx context.Context) error {
File: flg.Jsonnet,
Dest: flg.Moved,
Recursive: flg.Recursive,
DryRun: flg.DryRun,
Args: flg.Args,
})
}
Expand All @@ -65,10 +66,11 @@ type Flag struct {
Moved string
LogLevel string
LogColor string
Args []string
Help bool
Version bool
Recursive bool
Args []string
DryRun bool
}

func parseFlags(f *Flag) {
Expand All @@ -79,6 +81,7 @@ func parseFlags(f *Flag) {
flag.BoolVarP(&f.Help, "help", "h", false, "Show help")
flag.BoolVarP(&f.Version, "version", "v", false, "Show version")
flag.BoolVarP(&f.Recursive, "recursive", "r", false, "If this is set, tfmv finds files recursively")
flag.BoolVar(&f.DryRun, "dry-run", false, "Dry Run")
flag.Parse()
f.Args = flag.Args()
}
Expand All @@ -94,6 +97,7 @@ Options:
--version, -v Show sort-issue-template version
--jsonnet, -j Jsonnet file path
--recursive, -r If this is set, tfmv finds files recursively
--dry-run Dry Run
--log-level Log level
--log-color Log color. "auto", "always", "never" are available
--moved, -m The destination file name. If this is "same", the file is same with the resource`
3 changes: 2 additions & 1 deletion pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ func (c *Controller) Init(fs afero.Fs, stdout, stderr io.Writer) {
type Input struct {
File string
Dest string
Recursive bool
Args []string
Recursive bool
DryRun bool
}

const wordResource = "resource"
Expand Down
10 changes: 8 additions & 2 deletions pkg/controller/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (c *Controller) Run(_ context.Context, logE *logrus.Entry, input *Input) er
// read *.tf
editor := &Editor{
stderr: c.stderr,
dryRun: input.DryRun,
}
for _, file := range files {
logE := logE.WithField("file", file)
Expand Down Expand Up @@ -96,9 +97,14 @@ func (c *Controller) handleBlock(logE *logrus.Entry, editor *Editor, ja ast.Node
}
movedFile := filepath.Join(filepath.Dir(block.File), fileName)
logE.WithField("moved_file", movedFile).Debug("generating a moved block")
if err := c.writeMovedBlock(block, dest, movedFile); err != nil {
return fmt.Errorf("write a moved block: %w", err)
if input.DryRun {
logE.WithField("moved_file", movedFile).Info("[DRY RUN] generate a moved block")
} else {
if err := c.writeMovedBlock(block, dest, movedFile); err != nil {
return fmt.Errorf("write a moved block: %w", err)
}
}

// rename resources
logE.Debug("moving a block")
if err := editor.Move(logE, &MoveBlockOpt{
Expand Down
Loading