replace

package module
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 28, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

README

replace

Utility for searching and replacing text.

INSTALLATION

> go install github.com/go-curses/coreutils-replace/cmd/rpl@latest

DOCUMENTATION

> rpl --help
NAME:
   rpl - search and replace

USAGE:
   rpl [options] <search> <replace> <path> [path...]

VERSION:
   v0.2.4 (trunk)

DESCRIPTION:
   command line search and replace

GLOBAL OPTIONS:
   Configuration

   --all, -a                           include things that start with a "." (default: false)
   --backup, -b                        make backups before replacing content (default: false)
   --backup-extension value, -B value  specify the backup file suffix to use (default: ".bak")
   --dry-run, -n                       report what would have otherwise been done (default: false)
   --interactive, -I                   selectively apply replacement edits (default: false)
   --recurse, -R                       recurse into sub-directories (default: false)
   --show-diff, -D                     include unified diffs of all changes in the output (default: false)

   Expressions

   --dot-match-nl, -s                            set the dot-match-nl (?s) regexp flag (implies -P) (default: false)
   --ignore-case, -i                             perform a case-insensitive search (default: false)
   --multi-line, -m                              set the multi-line (?m) regexp flag (implies -P) (default: false)
   --multi-line-dot-match-nl, --ms, -p           convenience flag to set -m and -s (implies -P) (default: false)
   --multi-line-dot-match-nl-insensitive, --msi  convenience flag to set -m, -s and -i (implies -P) (default: false)
   --regex, -P                                   search and replace arguments are regular expressions (default: false)

   General

   --help, -h, --usage  display command-line usage information (default: false)
   --quiet, -q          run silently, ignored if --dry-run is also used (default: false)
   --verbose, -v        run loudly, ignored if --quiet is also used (default: false)
   --version, -V        display the version (default: false)

LICENSE

Copyright 2023  The Go-Curses Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use file except in compliance with the License.
You may obtain a copy of the license at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	CliFlags = []cli.Flag{

		&cli.BoolFlag{
			Category: "Configuration",
			Name:     "nop",
			Usage:    "report what would have otherwise been done",
			Aliases:  []string{"n"},
		},
		&cli.BoolFlag{
			Category: "Configuration",
			Name:     "interactive",
			Usage:    "selectively apply changes per file",
			Aliases:  []string{"I"},
		},
		&cli.BoolFlag{
			Category: "Configuration",
			Name:     "backup",
			Usage:    "make backups before replacing content",
			Aliases:  []string{"b"},
		},
		&cli.StringFlag{
			Category: "Configuration",
			Name:     "bak",
			Usage:    "specify the backup file suffix to use (implies -b)",
			Aliases:  []string{"B"},
		},
		&cli.BoolFlag{
			Category: "Configuration",
			Name:     "show-diff",
			Usage:    "include unified diffs of all changes in the output",
			Aliases:  []string{"d"},
		},
		&cli.BoolFlag{
			Category: "Configuration",
			Name:     "ignore-case",
			Usage:    "perform a case-insensitive search (literal or regex)",
			Aliases:  []string{"i"},
		},

		&cli.BoolFlag{
			Category: "File Selection",
			Name:     "recurse",
			Usage:    "recurse into sub-directories",
			Aliases:  []string{"r"},
		},
		&cli.BoolFlag{
			Category: "File Selection",
			Name:     "all",
			Usage:    `include files that start with a "."`,
			Aliases:  []string{"a"},
		},
		&cli.StringSliceFlag{
			Category: "File Selection",
			Name:     "x",
			Usage:    "exclude files matching glob patterns",
		},

		&cli.BoolFlag{
			Category: "Expressions",
			Name:     "regex",
			Usage:    "search and replace arguments are regular expressions",
			Aliases:  []string{"p"},
		},
		&cli.BoolFlag{
			Category: "Expressions",
			Name:     "multi-line",
			Usage:    "set the multi-line (?m) regexp flag (implies -P)",
			Aliases:  []string{"m"},
		},
		&cli.BoolFlag{
			Category: "Expressions",
			Name:     "dot-match-nl",
			Usage:    "set the dot-match-nl (?s) regexp flag (implies -P)",
			Aliases:  []string{"s"},
		},

		&cli.BoolFlag{
			Category: "General",
			Name:     "quiet",
			Usage:    "run silently, ignored if --nop is also used",
			Aliases:  []string{"q"},
		},
		&cli.BoolFlag{
			Category: "General",
			Name:     "verbose",
			Usage:    "run loudly, ignored if --quiet is also used",
			Aliases:  []string{"v"},
		},
	}
)
View Source
var (
	DefaultBackupExtension = ".bak"
)

Functions

func FindAllIncluded

func FindAllIncluded(exclude []*glob.Glob, targets []string) (found []string)

func FindAllMatcher

func FindAllMatcher(targets []string, exclude []*glob.Glob, fn FindAllMatchingFn, matcher FindAllMatcherFn) (files, matches []string)

func FindAllMatchingRegexp

func FindAllMatchingRegexp(search *regexp.Regexp, targets []string, exclude []*glob.Glob, fn FindAllMatchingFn) (files, matches []string)

func FindAllMatchingString

func FindAllMatchingString(search string, targets []string, exclude []*glob.Glob, fn FindAllMatchingFn) (files, matches []string)

func FindAllMatchingStringInsensitive

func FindAllMatchingStringInsensitive(search string, targets []string, exclude []*glob.Glob, fn FindAllMatchingFn) (files, matches []string)

func IsIncluded

func IsIncluded(exclude []*glob.Glob, input string) (included bool)

func MakeRegexp

func MakeRegexp(search string, o *Worker) (rx *regexp.Regexp, err error)

func ProcessTargetRegex

func ProcessTargetRegex(search *regexp.Regexp, replace, target string) (original, modified string, delta *diff.Diff, err error)

func ProcessTargetString

func ProcessTargetString(search, replace, target string) (original, modified string, delta *diff.Diff, err error)

func ProcessTargetStringInsensitive

func ProcessTargetStringInsensitive(search, replace, target string) (original, modified string, delta *diff.Diff, err error)

func RegexpReplace

func RegexpReplace(search *regexp.Regexp, replace, contents string) (modified string)

func StringReplace

func StringReplace(search, replace, contents string) (modified string)

func StringReplaceInsensitive

func StringReplaceInsensitive(search, replace, contents string) string

Types

type FindAllMatcherFn

type FindAllMatcherFn func(data []byte) (matched bool)

type FindAllMatchingFn

type FindAllMatchingFn func(file string, matched bool)

type Iterator

type Iterator struct {
	// contains filtered or unexported fields
}

func (*Iterator) Apply

func (i *Iterator) Apply() (count int, unified string, err error)

func (*Iterator) ApplyChanges

func (i *Iterator) ApplyChanges(delta *diff.Diff) (count int, unified string, err error)

func (*Iterator) Name

func (i *Iterator) Name() (path string)

func (*Iterator) Next

func (i *Iterator) Next()

func (*Iterator) Pos

func (i *Iterator) Pos() (pos int)

func (*Iterator) Replace

func (i *Iterator) Replace() (original, modified string, delta *diff.Diff, err error)

func (*Iterator) Valid

func (i *Iterator) Valid() (valid bool)

type Worker

type Worker struct {
	Regex           bool
	MultiLine       bool
	DotMatchNl      bool
	Recurse         bool
	DryRun          bool
	All             bool
	IgnoreCase      bool
	Backup          bool
	BackupExtension string
	ShowDiff        bool
	Interactive     bool
	Quiet           bool
	Verbose         bool

	Context *cli.Context
	Argv    []string
	Argc    int

	Search  string
	Pattern *regexp.Regexp
	Replace string
	Exclude []*glob.Glob

	Targets []string
	Files   []string
	Matched []string

	Notifier *notify.Notifier
	// contains filtered or unexported fields
}

func MakeWorker

func MakeWorker(ctx *cli.Context, notifier *notify.Notifier) (w *Worker, eventFlag cenums.EventFlag, err error)

func (*Worker) FileWriterErr

func (w *Worker) FileWriterErr() (fwe filewriter.Writer)

func (*Worker) FileWriterOut

func (w *Worker) FileWriterOut() (fwo filewriter.Writer)

func (*Worker) Init

func (w *Worker) Init(fn FindAllMatchingFn)

func (*Worker) Start

func (w *Worker) Start() (iter *Iterator)

func (*Worker) String

func (w *Worker) String() (s string)

Directories

Path Synopsis
cmd
rpl

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL