replacer

Replacer is a go generator to find-and-replace in go source files

https://github.com/psapezhka/replacer

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (14.2%) to scientific vocabulary

Keywords

code-generation go-generate golang regex
Last synced: 6 months ago · JSON representation

Repository

Replacer is a go generator to find-and-replace in go source files

Basic Info
  • Host: GitHub
  • Owner: psapezhka
  • License: mpl-2.0
  • Language: Go
  • Default Branch: main
  • Homepage:
  • Size: 71.3 KB
Statistics
  • Stars: 10
  • Watchers: 1
  • Forks: 0
  • Open Issues: 2
  • Releases: 4
Topics
code-generation go-generate golang regex
Created 11 months ago · Last pushed 9 months ago
Metadata Files
Readme Contributing Funding License Code of conduct Citation Security Authors

README.md

Replacer

[![Go Report Card](https://goreportcard.com/badge/github.com/weastur/replacer)](https://goreportcard.com/report/github.com/weastur/replacer) [![codecov](https://codecov.io/gh/weastur/replacer/graph/badge.svg?token=QANQ7BIQY9)](https://codecov.io/gh/weastur/replacer) [![test](https://github.com/weastur/replacer/actions/workflows/test.yaml/badge.svg)](https://github.com/weastur/replacer/actions/workflows/test.yaml) [![lint](https://github.com/weastur/replacer/actions/workflows/lint.yaml/badge.svg)](https://github.com/weastur/replacer/actions/workflows/lint.yaml)
![GitHub Release](https://img.shields.io/github/v/release/weastur/replacer) ![GitHub Downloads (all assets, all releases)](https://img.shields.io/github/downloads/weastur/replacer/total) ![GitHub commits since latest release](https://img.shields.io/github/commits-since/weastur/replacer/latest) ![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/weastur/replacer) ![GitHub License](https://img.shields.io/github/license/weastur/replacer)

Replacer is a Go code generator that applies regex-based transformations to your source files. It is designed to work with Go's //go:generate directive, allowing you to automate repetitive code modifications.

Why?

I often need to repeat the same set of comments in my Go code. For example, I use swaggo to generate Swagger documentation for an API. My API endpoints always return the same headers, like X-API-Version or X-Request-ID. Unfortunately, there's no way to define common headers for all endpoints yet. Instead of manually copying and pasting these headers every time, I created replacer to automate this process. Now, I can simply write:

go // @COMMON-HEADERS

run go generate, which is a part of my build pipeline, and get:

go // @Header all {string} X-Request-ID "UUID of the request" // @Header all {string} X-API-Version "API version, e.g. v1alpha" // @Header all {int} X-Ratelimit-Limit "Rate limit value" // @Header all {int} X-Ratelimit-Remaining "Rate limit remaining" // @Header all {int} X-Ratelimit-Reset "Rate limit reset interval in seconds"

Why not use sed or awk?

In short, it's hard to maintain when you have a lot of transformations, rules look more complicated. Also, there is a huge issues with multi-platform support. Even, unix-like macos and linux have different versions of sed and awk.

IDE's snippets?

IDE's snippets are great, but it's hard to maintain them in a team. Also, it's hard to share them between different IDE's.

Features

  • Full-featured regex (docs), including expands
  • Automatically searches for a configuration file (.replacer.yml or .replacer.yaml) in the current directory and parent directories.
  • Stops searching at the root directory or when a go.mod file is encountered.

Installation

To install replacer, run:

bash go install github.com/weastur/replacer/@latest

Make sure that $GOPATH/bin is in your $PATH.

Of course, you can also download the binary from the releases page

Usage

  1. Create a configuration file (.replacer.yml or .replacer.yaml) in the root of your project or in the directory where you want to run replacer.

    ```yaml

    rules:

    • regex: '(?m)^\/\/ MY RULE$' repl: |- // MY NEW AWESOME // MULTI-LINE REPLACEMENT
    • regex: '(?m)^\/\/ MY 2 RULE$' repl: |- // REPLACEMENT No2
    • regex: '(?m)^\/\/ (?P\w+) are also work$' repl: |- // ${group} are great ```
  2. Add a //go:generate directive to your source file. Despite config search mechanism, the transformation rules will be applied only to the file where the directive is placed.

    go //go:generate replacer

  3. Run go generate to apply the transformations.

    bash go generate ./...

    Pay attention that generate command will only work if you have the replacer binary in your $PATH.

    Also, the go build command doesn't run go generate automatically. You need to run it manually. Refer to go help generate for more information.

  4. Optional Flags:

- `-config` - specify the path to the configuration file.

    Example: `//go:generate replacer -config my-replacer-config.yml`

Configuration file search

If the -config flag is not provided, replacer will search for a configuration file in the following order:

  1. The current directory.
  2. Parent directories, moving up one level at a time.
  3. Stops searching when it reaches the root directory or encounters a go.mod file.

Development

See CONTRIBUTING.md for information on how to contribute to replacer.

tl;dr

```bash

fork/clone the repository

install go

put some config in .replacer.yml

task build task test GOFILE=my-test-file.go replacer

commit/push/PR

```

Project structure

  • main - the main command.
  • config - handles configuration file parsing and validation.
  • generator - applies regex-based transformations to source files.

Security

Refer to the SECURITY.md file for more information.

License

Mozilla Public License 2.0

Refer to the LICENSE file for more information.

GitHub Events

Total
  • Issue comment event: 1
Last Year
  • Issue comment event: 1

Dependencies

go.sum go
  • github.com/goccy/go-yaml v1.17.1
.github/workflows/lint.yaml actions
  • actions/checkout v4 composite
  • actions/setup-go v5 composite
  • golangci/golangci-lint-action v6 composite
.github/workflows/release.yaml actions
  • actions/checkout v4 composite
  • actions/setup-go v5 composite
  • goreleaser/goreleaser-action v6 composite
.github/workflows/test.yaml actions
  • actions/checkout v4 composite
  • actions/setup-go v5 composite
  • codecov/codecov-action v5 composite
go.mod go