Skip to content

Commit

Permalink
feat: add a short option for replace
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

The short flag of `--recursive` is changed from `-r` to `-R`
  • Loading branch information
suzuki-shunsuke committed Dec 22, 2024
1 parent 65d165e commit a6fd145
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ resource "github_branch" "example" {
```

Let's replace `-` with `_`.
You need to specify either `--replace` or `--jsonnet (-j)`.
In this case, let's use `--replace`.
You need to specify either `-r` or `--jsonnet (-j)`.
In this case, let's use `-r`.
[If you need more flexible renaming, you can use Jsonnet. For details, please see here](#jsonnet).

Run `tfmv --replace "-/_"`.
Run `tfmv -r "-/_"`.
You don't need to run `terraform init`.

```sh
tfmv --replace "-/_"
tfmv -r "-/_"
```

Then a resource name is changed and `moved.tf` is created.
Expand Down Expand Up @@ -83,21 +83,21 @@ moved {
You can also pass *.tf via arguments:

```sh
tfmv --replace "-/_" foo/aws_s3_bucket.tf foo/aws_instance.tf
tfmv -r "-/_" foo/aws_s3_bucket.tf foo/aws_instance.tf
```

tfmv supports modules too.

```sh
tfmv --replace "production/prod" foo/module_foo.tf
tfmv -r "production/prod" foo/module_foo.tf
```

### Dry Run: --dry-run

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

```sh
tfmv --replace "-/_" --dry-run bar/main.tf
tfmv -r "-/_" --dry-run bar/main.tf
```

### Change the filename for moved blocks
Expand All @@ -106,22 +106,22 @@ By default tfmv writes moved blocks to `moved.tf`.
You can change the file name via `-m` option.

```sh
tfmv --replace "-/_" -m moved_blocks.tf bar/main.tf
tfmv -r "-/_" -m moved_blocks.tf bar/main.tf
```

You can also write moved blocks to the same file with renamed resources and modules.

```sh
tfmv --replace "-/_" -m same bar/foo.tf
tfmv -r "-/_" -m same bar/foo.tf
```

### `-r` Recursive option
### `--recursive (-R)` Recursive option

By default, tfmv finds *.tf on the current directory.
You can find files recursively using `-r` option.
You can find files recursively using `-R` option.

```sh
tfmv -r --replace "-/_"
tfmv -Rr "-/_"
```

The following directories are ignored:
Expand All @@ -132,7 +132,7 @@ The following directories are ignored:

## Jsonnet

`--replace` is simple and useful, but sometimes you need more flexible renaming.
`-r` is simple and useful, but sometimes you need more flexible renaming.
In that case, you can use `--jsonnet (-j)`.
[Jsonnet](https://jsonnet.org) is a powerful data configuration language.

Expand Down
8 changes: 4 additions & 4 deletions pkg/cli/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ type Flag struct {
func parseFlags(f *Flag) {
flag.StringVarP(&f.Jsonnet, "jsonnet", "j", "", "Jsonnet file path")
flag.StringVarP(&f.Moved, "moved", "m", "moved.tf", "The destination file name")
flag.StringVar(&f.Replace, "replace", "", "Replace strings in block names. The format is <new>/<old>. e.g. -/_")
flag.StringVarP(&f.Replace, "replace", "r", "", "Replace strings in block names. The format is <new>/<old>. e.g. -/_")
flag.StringVar(&f.LogLevel, "log-level", "info", "The log level")
flag.StringVar(&f.LogColor, "log-color", "auto", "The log color")
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.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 @@ -99,8 +99,8 @@ Options:
--help, -h Show help
--version, -v Show sort-issue-template version
--jsonnet, -j Jsonnet file path
--recursive, -r If this is set, tfmv finds files recursively
--replace Replace strings in block names. The format is <new>/<old>. e.g. -/_
--recursive, -R If this is set, tfmv finds files recursively
--replace, -r Replace strings in block names. The format is <new>/<old>. e.g. -/_
--dry-run Dry Run
--log-level Log level
--log-color Log color. "auto", "always", "never" are available
Expand Down

0 comments on commit a6fd145

Please sign in to comment.