Giter Club home page Giter Club logo

go-replace's Introduction

go-replace

GitHub release license Build Status Github All Releases Github Releases

Cli utility for replacing text in files, written in golang and compiled for usage in Docker images

Inspired by https://github.com/piranha/goreplace

Features

  • Simple search&replace for terms specified as normal shell argument (for escaping only normal shell quotes needed)
  • Can use regular expressions for search&replace with and without backrefs (--regex and --regex-backrefs)
  • Supports multiple changesets (search&replace terms)
  • Replace the whole line with replacement when line is matching (--mode=line)
  • ... and add the line at the bottom if there is no match (--mode=lineinfile)
  • Use golang template with [Sprig template functions]](https://masterminds.github.io/sprig/) (--mode=template)
  • Can store file as other filename (eg. go-replace ./configuration.tmpl:./configuration.conf)
  • Can replace files in directory (--path) and offers file pattern matching functions (--path-pattern and --path-regex)
  • Can read also stdin for search&replace or template handling
  • Supports Linux, MacOS, Windows and ARM/ARM64 (Rasbperry Pi and others)

Usage

Usage:
  go-replace

Application Options:
      --threads=                                Set thread concurrency for replacing in multiple files at same time (default: 20)
  -m, --mode=[replace|line|lineinfile|template] replacement mode - replace: replace match with term; line: replace line with term; lineinfile: replace line with term or
                                                if not found append to term to file; template: parse content as golang template, search value have to start uppercase
                                                (default: replace)
  -s, --search=                                 search term
  -r, --replace=                                replacement term
      --lineinfile-before=                      add line before this regex
      --lineinfile-after=                       add line after this regex
  -i, --case-insensitive                        ignore case of pattern to match upper and lowercase characters
      --stdin                                   process stdin as input
  -o, --output=                                 write changes to this file (in one file mode)
      --output-strip-ext=                       strip file extension from written files (also available in multi file mode)
      --once=[keep|unique]                      replace search term only one in a file, keep duplicaes (keep, default) or remove them (unique)
      --regex                                   treat pattern as regex
      --regex-backrefs                          enable backreferences in replace term
      --regex-posix                             parse regex term as POSIX regex
      --path=                                   use files in this path
      --path-pattern=                           file pattern (* for wildcard, only basename of file)
      --path-regex=                             file pattern (regex, full path)
      --ignore-empty                            ignore empty file list, otherwise this will result in an error
  -v, --verbose                                 verbose mode
      --dry-run                                 dry run mode
  -V, --version                                 show version and exit
      --dumpversion                             show only version number and exit
  -h, --help                                    show this help message

Files must be specified as arguments and will be overwritten after parsing. If you want an alternative location for saving the file the argument can be specified as source:destination, eg. go-replace -s foobar -r barfoo daemon.conf.tmpl:daemon.conf.

If --path (with or without --path-pattern or --path-regex) the files inside path are used as source and will be overwritten. If daemon.conf.tmpl should be written as daemon.conf the option --output-strip-ext=.tmpl will do this based on the source file name.

Regular expression's back references can be activated with --regex-backrefs and must be specified as $1, $2 ... $9.

Mode Description
replace Replace search term inside one line with replacement.
line Replace line (if matched term is inside) with replacement.
lineinfile Replace line (if matched term is inside) with replacement. If no match is found in the whole file the line will be appended to the bottom of the file.
template Parse content as golang template, arguments are available via {{.Arg.Name}} or environment vars via {{.Env.Name}}

Examples

Command Description
go-replace -s foobar -r barfoo file1 file2 Replaces foobar to barfoo in file1 and file2
go-replace --regex -s 'foo.*' -r barfoo file1 file2 Replaces the regex foo.* to barfoo in file1 and file2
go-replace --regex --ignore-case -s 'foo.*' -r barfoo file1 file2 Replaces the regex foo.* (and ignore case) to barfoo in file1 and file2
go-replace --mode=line -s 'foobar' -r barfoo file1 file2 Replaces all lines with content foobar to barfoo (whole line) in file1 and file2
go-replace -s 'foobar' -r barfoo --path=./ --path-pattern='*.txt' Replaces all lines with content foobar to barfoo (whole line) in *.txt files in current path

Example with golang templates

Withing the template there are Template functions available from Sprig.

Configuration file daemon.conf.tmpl:

<VirtualHost ...>
    ServerName {{env "SERVERNAME"}}
    DocumentRoot {{env "DOCUMENTROOT"}}
<VirtualHost>

Process file with:

export SERVERNAME=www.foobar.example
export DOCUMENTROOT=/var/www/foobar.example/
go-replace --mode=template daemon.conf.tmpl:daemon.conf

Result file daemon.conf:

<VirtualHost ...>
    ServerName www.foobar.example
    DocumentRoot /var/www/foobar.example/
<VirtualHost>

Installation

GOREPLACE_VERSION=22.9.0 \
&& wget -O /usr/local/bin/go-replace https://github.com/webdevops/go-replace/releases/download/$GOREPLACE_VERSION/go-replace.linux.amd64 \
&& chmod +x /usr/local/bin/go-replace

Docker images

Image Description
webdevops/go-replace:latest Latest release, binary only
webdevops/go-replace:master Current development version in branch master
webdevops/go-replace:develop Current development version in branch develop

go-replace's People

Contributors

htuscher avatar mblaschke avatar ndzoesch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-replace's Issues

Add path searching

Implement --path=./conf/ and --path-pattern=*.conf for simple matching and --path-regex=\.conf$ for regex file matching

Build is failing

Hard to have a good feeling about adoption if a build has been failing for the last 20 days. Is this being investigated?

Cannot use path pattern ignoring nested directories

Trying to replace envs in all YAML files in directory go-replace --mode=template --path=./k8s/ --path-pattern='*.yaml' . But can't find any reasonable way to exclude nested directories. How can I process only first level of selected path?

[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x5d2068]

Hi folks,

when re-starting one of our containers, go-replace exits with a SEGV:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x5d2068]

goroutine 1 [running]:
main.searchFilesInPath.func1(0x7ffe6f609c49, 0x16, 0x0, 0x0, 0x73cb20, 0xc420017560, 0x20, 0x618f40)
        /Users/mblaschke/Projects/go-replace/filehandling.go:56 +0x38
path/filepath.Walk(0x7ffe6f609c49, 0x16, 0xc42000b240, 0x10, 0x610960)
        /usr/local/Cellar/go/1.8.1/libexec/src/path/filepath/path.go:396 +0x8f
main.searchFilesInPath(0x7ffe6f609c49, 0x16, 0xc420013b80)
        /Users/mblaschke/Projects/go-replace/filehandling.go:84 +0xb0
main.buildFileitems(0xc4200db340, 0x0, 0x7, 0x7, 0x0, 0x0)
        /Users/mblaschke/Projects/go-replace/main.go:463 +0x45e
main.main()
        /Users/mblaschke/Projects/go-replace/main.go:482 +0x10a
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x5d2068]

goroutine 1 [running]:
main.searchFilesInPath.func1(0x7ffd3d8e7c49, 0x16, 0x0, 0x0, 0x73cb20, 0xc4200174d0, 0x20, 0x618f40)
        /Users/mblaschke/Projects/go-replace/filehandling.go:56 +0x38
path/filepath.Walk(0x7ffd3d8e7c49, 0x16, 0xc42000af80, 0x10, 0x610960)
        /usr/local/Cellar/go/1.8.1/libexec/src/path/filepath/path.go:396 +0x8f
main.searchFilesInPath(0x7ffd3d8e7c49, 0x16, 0xc420013930)
        /Users/mblaschke/Projects/go-replace/filehandling.go:84 +0xb0
main.buildFileitems(0xc4200df420, 0x0, 0x7, 0x7, 0x0, 0x0)
        /Users/mblaschke/Projects/go-replace/main.go:463 +0x45e
main.main()
        /Users/mblaschke/Projects/go-replace/main.go:482 +0x10a

At the moment, I don't have the exact command being run - nevertheless, let me know if you need more information to resolve this.

Thank you very much for your time!

Best,
Anton

CVE-2022-29526 - golang.org/x/sys

Hello,

When scanning a Docker image from webdevops with any inspector (eg: AWS Inspector). It only has one CVE remaining in the image.
CVE-2022-29526 on file path: usr/local/bin/go-replace.

The recommanded remediation is :
Upgrade your installed software packages to the proposed fixed in version and release.

  • Update sys to 0.1.0

Is it possible to upgrade this package to 0.1.0 ? Actually it is v0.0.0-20220928140112-f11e5e49a4ec

Regards.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.