sourceutil

package
v1.20220411.3 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2022 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultFilePerms = 0644
	DefaultDirPerms  = 0755
)

Defaults

Variables

This section is empty.

Functions

func Copy

func Copy(ctx context.Context, destination, source string) error

Copy copies a file from a source to a destination.

func CopyAll

func CopyAll(destination, source string, opts ...CopyAllOption) error

CopyAll copies all files and directories from a source path to a destination path recurrsively.

func FindAllFiles

func FindAllFiles(ctx context.Context, startPath, matchGlob string) (output []string, err error)

FindAllFiles finds all the files that match a given glob recursively.

func FindFiles

func FindFiles(ctx context.Context, targetPath string, matchGlob string) (output []string, err error)

FindFiles finds all files in a given path that matches a given glob but does not traverse recursively.

func MatchInject

func MatchInject(corpus []byte, expr, inject string) []byte

MatchInject injects a given value after the any instances of a given expression.

func MatchRemove

func MatchRemove(corpus []byte, expr string) []byte

MatchRemove removes a line if it matches a given expression.

func RemoveFile

func RemoveFile(ctx context.Context, path string) error

RemoveFile removes a file and prints a debug message if the context sets that flag.

func RemoveQuotes

func RemoveQuotes(value string) string

RemoveQuotes removes quotes from a string

Types

type CopyAllOption

type CopyAllOption func(*CopyAllOptions)

CopyAllOption is a mutator for copy all options

func OptCopyAllSkipGlobs

func OptCopyAllSkipGlobs(globs ...string) CopyAllOption

OptCopyAllSkipGlobs sets the skip provider to a glob matcher based on a given set of glob(s).

func OptCopyAllSymlinkMode

func OptCopyAllSymlinkMode(mode CopyAllSymlinkMode) CopyAllOption

OptCopyAllSymlinkMode sets the symlink mode.

type CopyAllOptions

type CopyAllOptions struct {
	SymlinkMode  CopyAllSymlinkMode
	SkipProvider func(os.FileInfo) bool
}

CopyAllOptions are the options for copy all.

type CopyAllSymlinkMode

type CopyAllSymlinkMode int

CopyAllSymlinkMode is how symlinks should be handled

var (
	// CopyAllSymlinkModeShallow will copy links from the source to the destination as links.
	CopyAllSymlinkModeShallow CopyAllSymlinkMode = 0
	// CopyAllSymlinkModeDeep will traverse into the link destination and copy any files recursively.
	CopyAllSymlinkModeDeep CopyAllSymlinkMode = 1
	// CopyAllSymlinkModeSkip will skip any links discovered.
	CopyAllSymlinkModeSkip CopyAllSymlinkMode = 2
)

CopyAllSymlinkMode(s)

type CopyRewriter

type CopyRewriter struct {
	Source              string
	Destination         string
	SkipGlobs           []string
	GoImportVisitors    []GoImportVisitor
	GoAstVistiors       []GoAstVisitor
	StringSubstitutions []StringSubstitution
	DryRun              bool
	RemoveDestination   bool
	KeepTemporary       bool

	Quiet   *bool
	Verbose *bool
	Debug   *bool

	Stdout io.Writer
	Stderr io.Writer
}

CopyRewriter copies a source to a destination, and applies rewrite rules to the file(s) it copies.

func (CopyRewriter) DebugOrDefault

func (cr CopyRewriter) DebugOrDefault() bool

DebugOrDefault returns a value or a default.

func (CopyRewriter) Debugf

func (cr CopyRewriter) Debugf(format string, args ...interface{})

Debugf writes to stdout if the `Debug` flag is true.

func (CopyRewriter) Execute

func (cr CopyRewriter) Execute(ctx context.Context) error

Execute is the command body.

func (CopyRewriter) GetStderr

func (cr CopyRewriter) GetStderr() io.Writer

GetStderr returns standard error.

func (CopyRewriter) GetStdout

func (cr CopyRewriter) GetStdout() io.Writer

GetStdout returns standard out.

func (CopyRewriter) QuietOrDefault

func (cr CopyRewriter) QuietOrDefault() bool

QuietOrDefault returns a value or a default.

func (CopyRewriter) VerboseOrDefault

func (cr CopyRewriter) VerboseOrDefault() bool

VerboseOrDefault returns a value or a default.

func (CopyRewriter) Verbosef

func (cr CopyRewriter) Verbosef(format string, args ...interface{})

Verbosef writes to stdout if the `Verbose` flag is true.

type FileInfo

type FileInfo struct {
	os.FileInfo
	FullPath string
}

FileInfo extends os.FileInfo with the full path.

func LS

func LS(root string) (output []FileInfo, err error)

LS returns a list of files for a given path.

type GoAstFilter

type GoAstFilter func(context.Context, ast.Node) (visit, recurse bool)

GoAstFilter is a delegate type that filters ast nodes for visiting.

type GoAstNodeVisitor

type GoAstNodeVisitor func(context.Context, ast.Node)

GoAstNodeVisitor mutates a given node.

type GoAstRewriteOption

type GoAstRewriteOption func(*GoAstRewriteOptions)

GoAstRewriteOption the ast rewrite options.

func GoIsCall

func GoIsCall(fn string) GoAstRewriteOption

GoIsCall returns a filter that determines if a function is a given name.

It will only evaluate for function calls that appear local to the current package, that is, function calls that do not have a selector.

func GoIsPackageCall

func GoIsPackageCall(pkg, fn string) GoAstRewriteOption

GoIsPackageCall returns a filter that determines if a function is a given sel.Fn.

It will only evaluate for function calls that use a package selector that is, function calls that have a selector.

func GoRewriteCall

func GoRewriteCall(fn string) GoAstRewriteOption

GoRewriteCall changes a given function as filtered by a filter to a given call noted by Fn.

func GoRewritePackageCall

func GoRewritePackageCall(sel, fn string) GoAstRewriteOption

GoRewritePackageCall changes a given function as filtered by a filter to a given call noted by sel.Fn.

type GoAstRewriteOptions

type GoAstRewriteOptions struct {
	Filter      GoAstFilter
	NodeVisitor GoAstNodeVisitor
}

GoAstRewriteOptions breaks the mutator out into field specific mutators.

func (GoAstRewriteOptions) Apply

func (opts GoAstRewriteOptions) Apply(ctx context.Context, node ast.Node) bool

Apply applies the options to the ast node.

type GoAstVisitor

type GoAstVisitor func(context.Context, ast.Node) bool

GoAstVisitor mutates an ast node.

func GoAstRewrite

func GoAstRewrite(opts ...GoAstRewriteOption) GoAstVisitor

GoAstRewrite returns a go ast visitor with a given set of options.

type GoImportRewriteOption

type GoImportRewriteOption func(*GoImportRewriteOptions)

GoImportRewriteOption mutates the import rewrite options

func OptGoImportAddName

func OptGoImportAddName(name string) GoImportRewriteOption

OptGoImportAddName adds a name if one is not already specified.

func OptGoImportNameMatches

func OptGoImportNameMatches(expr string) GoImportRewriteOption

OptGoImportNameMatches returns a rewrite filter that returns if an import name matches a given expression.

func OptGoImportPathMatches

func OptGoImportPathMatches(expr string) GoImportRewriteOption

OptGoImportPathMatches returns a rewrite filter that returns if an import path matches a given expression.

func OptGoImportPathRewrite

func OptGoImportPathRewrite(matchExpr, outputExpr string) GoImportRewriteOption

OptGoImportPathRewrite returns a path filter and rewrite expression.

func OptGoImportSetAlias

func OptGoImportSetAlias(name string) GoImportRewriteOption

OptGoImportSetAlias sets the import alias to a given value.

Setting to "" will remove the alias.

func OptGoImportSetPath

func OptGoImportSetPath(path string) GoImportRewriteOption

OptGoImportSetPath sets an import path to a given value.

type GoImportRewriteOptions

type GoImportRewriteOptions struct {
	Filter         func(context.Context, *ast.ImportSpec) (bool, error)
	CommentVisitor func(context.Context, *ast.CommentGroup) error
	DocVisitor     func(context.Context, *ast.CommentGroup) error
	NameVisitor    func(context.Context, *ast.Ident) error
	PathVisitor    func(context.Context, *ast.BasicLit) error
}

GoImportRewriteOptions breaks the mutator out into field specific mutators.

func (GoImportRewriteOptions) Apply

func (opts GoImportRewriteOptions) Apply(ctx context.Context, importSpec *ast.ImportSpec) error

Apply applies the options to the import.

type GoImportVisitor

type GoImportVisitor func(context.Context, *ast.ImportSpec) error

GoImportVisitor mutates an ast import.

func GoImportRewrite

func GoImportRewrite(opts ...GoImportRewriteOption) GoImportVisitor

GoImportRewrite visits and optionally mutates imports for go files.

func GoImportRewritePrefix

func GoImportRewritePrefix(sourcePrefix, destinationPrefix string) GoImportVisitor

GoImportRewritePrefix is a helper that returns a visitor that takes a source path and rewrites it as the destination path preserving any path segments after the source if it matches the sourcePrefix.

func ParseGoImportRewriteRules

func ParseGoImportRewriteRules(rewriteRules []string) (output []GoImportVisitor)

ParseGoImportRewriteRules parses go import rewrite rules as strings.

type StringSubstitution

type StringSubstitution func(context.Context, string) (string, bool)

StringSubstitution is a mutator for a string. It returns the modified string, and a bool if the rule matched or not.

func SubstituteString

func SubstituteString(before, after string) StringSubstitution

SubstituteString rewrites a string literal.

type TempDir

type TempDir struct {
	Path string
}

TempDir is a directory that can be cleaned up with Close.

func CreateTempDir

func CreateTempDir(prefix string) (*TempDir, error)

CreateTempDir creates a temporary directory with a given prefix.

func (TempDir) Close

func (td TempDir) Close() error

Close removes the directory.

Jump to

Keyboard shortcuts

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