gotag is a CLI tool written in Go that allows you to manipulate struct tags in your Go source files. It supports adding, deleting, or overwriting struct tags on specific structs, entire files, or directories. This tool ensures that all tags are uniformly ordered across fields and supports interactive mode with confirmation prompts (which can be bypassed using the force flag).
- Targeting:
- Single Struct: Specify a struct in a file using
pkg/here/struct.go@StructName. - Entire File: Process all structs in a file using
pkg/here/struct.go. - Directory: Process all structs in a directory (with an optional recursive flag) using
pkg/here/.
- Single Struct: Specify a struct in a file using
- Directives:
- Add (
-a): Add a tag if it does not exist. - Delete (
-d): Delete a tag. - Overwrite (
-o): Overwrite an existing tag's value (use an empty value to trigger interactive prompts/defaults).
- Add (
- Case Styles: Specify a case style for tag values (default is
camelCase; alternatives includesnake_caseandkebab-case). - Uniform Ordering: Tags are stored in a uniform, sorted order across all fields.
- Interactive Mode (
-i): Optionally prompt for tag values or confirmations when adding or overwriting tags. - Force Mode (
-f): Bypass confirmation prompts and force changes.
Ensure you have Go (1.18+) installed.
Clone the repository:
git clone https://github.com/joshua-temple/gotag.git
cd gotagBuild the CLI tool:
go build -o gotag ./cmdOr install it:
go install github.com/joshua-temple/gotag/cmd/gotag@latest-
Single struct update:
gotag --target pkg/here/struct.go@MyStruct -a json -o json=new_value --case snake_case
-
Entire file update:
gotag --target pkg/here/struct.go -a xml -d json
-
Recursive directory update:
gotag --target pkg/here/ --recursive -a db
-
Multi-tag update:
gotag --target pkg/here/struct.go@MyStruct -a json,xml -c kebab
Run in interactive mode to be prompted for tag values and confirmations:
gotag --target pkg/here/struct.go@MyStruct -o json= -iYou will see prompts like:
Enter new value for tag 'json' in field 'FieldName' (default: camelCase conversion):
To bypass confirmation prompts (for overwrites and uniform ordering updates), use the -f flag:
gotag --target pkg/here/struct.go -a json -o json=new_value -f-t, --target: Target file, directory, or struct (required).-r, --recursive: Recursively scan directories.-i, --interactive: Interactive mode for tag prompts/confirmations.-f, --force: Force changes and bypass confirmation prompts.-c, --case: Case style for tag values (camelCase,snake_case,kebab-case). Default iscamelCase.- aliases are also available:
camel,snake,kebab.
- aliases are also available:
-a, --add: Tag keys to add (comma-separated for multiple, e.g.-a json,xml,db).-d, --delete: Tag keys to delete (comma-separated for multiple, e.g.-d json,xml).-o, --overwrite: Tag keys to overwrite in the formatkey=newValue(use an empty value for interactive/defaults).
Run all tests with:
go test ./...Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License.