https://github.com/yoheimuta/go-rewrite
go-rewrite is a thin go package which helps replacing files.
https://github.com/yoheimuta/go-rewrite
go golang grep library rewrite sed
Last synced: 23 days ago
JSON representation
go-rewrite is a thin go package which helps replacing files.
- Host: GitHub
- URL: https://github.com/yoheimuta/go-rewrite
- Owner: yoheimuta
- Created: 2018-07-15T00:50:57.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-07-16T00:37:22.000Z (almost 8 years ago)
- Last Synced: 2025-01-20T10:47:55.892Z (over 1 year ago)
- Topics: go, golang, grep, library, rewrite, sed
- Language: Go
- Homepage:
- Size: 16.6 KB
- Stars: 2
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# go-rewrite [](https://godoc.org/github.com/yoheimuta/go-rewrite/rewrite) [](https://travis-ci.org/yoheimuta/go-rewrite)
go-rewrite is a thin go package which helps replacing files.
You can use this package...
- To focus on coding to filter files and overwrite the file with a new content.
- To search for files under intricate conditions instead of grep.
- To overwrite the files under intricate conditions instead of sed.
- To traverse a directory much faster with utilizing goroutines.
### Motivation
For example, if you want to replace comments on multiple lines of `func or class or var declaration`
from `//` to `///` for files with extension .swift.
It is easier to write Go's code than to do it with grep and sed.
### Installation
```
go get github.com/yoheimuta/go-rewrite
```
### Usage
See `_example/simple` and `_example/intricate` in detail.
```go
func main() {
rule := &myrule{}
rewrite.Run(".", rule)
}
type myrule struct{}
// Filter filters the file using the filepath.
func (*myrule) Filter(filepath string) (bool, error) {
if !strings.HasSuffix(filepath, ".txt") {
return false, nil
}
return true, nil
}
// Mapping maps the file content with new one.
func (*myrule) Mapping(content []byte) ([]byte, bool, error) {
content = bytes.Replace(content, []byte("hoge"), []byte("fuga"), -1)
return content, true, nil
}
// Output writes the content to the file.
func (*myrule) Output(_ string, content []byte) error {
return ioutil.WriteFile(filepath, content, 0644)
}
```