Skip to content

go-rewrite is a thin go package which helps replacing files.

Notifications You must be signed in to change notification settings

yoheimuta/go-rewrite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-rewrite GoDoc Build Status

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.

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)
}

About

go-rewrite is a thin go package which helps replacing files.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published