mung

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2025 License: MIT Imports: 5 Imported by: 2

README

mung

Manipulate PATH-like environment variables

GoDoc Go Report Card Test Coverage

The mung package contains a reusable Go module and command-line utility (download). It is fully documented (with examples) and includes tests with 100% coverage.

The comand-line utility, also named mung, is a simple wrapper around the module. Like most native Go executables, the release packages are all statically-linked and have no dependencies. It is convenient for shell script usage.

Usage

See Go doc for all documentation and examples.

Installation

To use the module in your own Go packages:

go get -v github.com/ardnew/mung

To install the command-line utility:

go install -v github.com/ardnew/mung/cmd/mung@latest

Command-line Examples (GNU bash)

set -x

# prefix "bar" a given string "foo"
export X=$( mung -p bar foo )
++ mung -p bar foo
+ export X=bar:foo
+ X=bar:foo

# move "foo" to prefix the env var named X
export X=$( mung -p foo -n X )
++ mung -p foo -n X
+ export X=foo:bar
+ X=foo:bar

# suffix "baz" to X
export X=$( mung -s baz -n X )
++ mung -s baz -n X
+ export X=foo:bar:baz
+ X=foo:bar:baz

# multiple operations to show ordering and replacement
export X=$( mung -p two:three -p one -s x:y -s z -r baz -n X )
++ mung -p two:three -p one -s x:y -s z -r baz -n X
+ export X=one:two:three:foo:bar:x:y:z
+ X=one:two:three:foo:bar:x:y:z

# another complex example
export X=$( mung -s front:end -r next:bar -p front:next -n X )
++ mung -s front:end -r next:bar -p front:next -n X
+ export X=front:one:two:three:foo:x:y:z:end
+ X=front:one:two:three:foo:x:y:z:end

Packaging releases

A Makefile is provided that will generate versioned release packages.

Define VERSION and use the dist target to build for all platforms:

make dist VERSION=X.Y.Z

To build/package for a specific platform, call make [<target>-]${GOOS}-${GOARCH}. For example with a single platform linux-amd64:

make     clean-linux-amd64
make           linux-amd64 VERSION=X.Y.Z
make      dist-linux-amd64 VERSION=X.Y.Z
make distclean-linux-amd64 VERSION=X.Y.Z

Documentation

Overview

Package mung provides methods to manipulate strings commonly found in PATH-like environment variables.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Make

func Make[T any](opts ...Option[T]) (t T)

Make returns a new object of type T with the given options applied.

Example

ExampleMake demonstrates creating a new Config with options.

// Create a new Config with PATH-like variable
config := Make(
	WithSubject([]string{"/usr/local/bin:/usr/bin:/bin"}),
	WithDelim(":"),
)

fmt.Println(config.String())
Output:

/usr/local/bin:/usr/bin:/bin

func Version

func Version() string

Version returns the version of the mung package.

Example

ExampleVersion demonstrates how to get the version of the mung package.

version := Version()

fmt.Println(version)
Output:

0.4.0

func Wrap

func Wrap[T any](t T, opts ...Option[T]) T

Wrap returns t after applying the given options.

Example

ExampleWrap demonstrates applying options to an existing object.

// Create an empty Config
var config Config

// Apply options to the existing Config
config = Wrap(config,
	WithSubject([]string{"/usr/local/bin:/usr/bin"}),
	WithDelim(":"),
	WithSuffix([]string{"/opt/bin"}),
)

fmt.Println(config.String())
Output:

/usr/local/bin:/usr/bin:/opt/bin

Types

type Config

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

Config represents the configuration for string munging operations.

Example (Complex)

ExampleConfig_complex demonstrates a more complex workflow.

// Build a PATH-like variable with multiple operations
config := Make(
	WithSubject([]string{"/usr/bin:/bin:/usr/local/old:/opt/deprecated"}),
	WithDelim(":"),
	WithPrefix([]string{"/usr/local/bin"}),
	WithSuffix([]string{"/opt/bin"}),
	WithRemove([]string{"/usr/local/old", "/opt/deprecated"}),
	WithReplace(map[string]string{"/bin": "/sbin"}),
)

fmt.Println(config.String())
Output:

/usr/local/bin:/usr/bin:/sbin:/opt/bin

func (Config) All added in v0.3.0

func (c Config) All() iter.Seq[string]

All returns each string item from the munged sequence.

Example

ExampleConfig_All demonstrates iterating through the processed sequence.

config := Make(
	WithSubject([]string{"/usr/bin:/bin"}),
	WithDelim(":"),
	WithPrefix([]string{"/usr/local/bin"}),
	WithSuffix([]string{"/opt/bin"}),
)

// Collect results using the iterator
var result []string
config.All()(func(s string) bool {
	result = append(result, s)
	return true
})

fmt.Println(strings.Join(result, ", "))
Output:

/usr/local/bin, /usr/bin, /bin, /opt/bin

func (Config) Delim

func (c Config) Delim() string

Delim returns the delimiter used for splitting and joining strings.

Example

ExampleConfig_Delim demonstrates getting the delimiter.

config := Make(
	WithDelim(":"),
)

delim := config.Delim()
fmt.Printf("Delimiter: %q\n", delim)
Output:

Delimiter: ":"

func (Config) Filtered added in v0.3.0

func (c Config) Filtered() iter.Seq[string]

Filtered returns each string item from the munged sequence that satisfies the predicate function Config.Predicate.

func (Config) Predicate added in v0.3.0

func (c Config) Predicate() func(string) bool

Predicate returns the predicate function used to select yielded strings.

func (Config) Prefix

func (c Config) Prefix() []string

Prefix returns the list of strings to be prepended to the result.

Example

ExampleConfig_Prefix demonstrates accessing prefix strings.

config := Make(
	WithPrefix([]string{"/usr/local/bin", "/usr/bin"}),
)

prefix := config.Prefix()
fmt.Println(strings.Join(prefix, ", "))
Output:

/usr/local/bin, /usr/bin

func (Config) Remove

func (c Config) Remove() []string

Remove returns the list of strings to be removed during processing.

Example

ExampleConfig_Remove demonstrates accessing removal strings.

config := Make(
	WithRemove([]string{"/usr/bin", "/opt/bin"}),
)

remove := config.Remove()
fmt.Println(strings.Join(remove, ", "))
Output:

/usr/bin, /opt/bin

func (Config) Replace

func (c Config) Replace() map[string]string

Replace returns a copy of the string replacement map.

Example

ExampleConfig_Replace demonstrates accessing replacement rules.

config := Make(
	WithReplace(map[string]string{
		"/usr/bin": "/opt/bin",
		"/bin":     "/sbin",
	}),
)

replace := config.Replace()
// Sort for consistent output
keys := []string{"/bin", "/usr/bin"}
for _, k := range keys {
	if v, ok := replace[k]; ok {
		fmt.Printf("%s -> %s\n", k, v)
	}
}
Output:

/bin -> /sbin
/usr/bin -> /opt/bin

func (Config) String

func (c Config) String() string

String returns the munged strings joined with the configuration's delimiter.

Example

ExampleConfig_String demonstrates the String method.

config := Make(
	WithSubject([]string{"/usr/local/bin:/usr/bin:/bin"}),
	WithDelim(":"),
)

// Convert the munged result to a string
result := config.String()
fmt.Println(result)
Output:

/usr/local/bin:/usr/bin:/bin

func (Config) Subject

func (c Config) Subject() []string

Subject returns the subject strings to be processed.

Example

ExampleConfig_Subject demonstrates accessing the subject strings.

config := Make(
	WithSubject([]string{"/usr/local/bin", "/usr/bin", "/bin"}),
)

subject := config.Subject()
fmt.Println(strings.Join(subject, ", "))
Output:

/usr/local/bin, /usr/bin, /bin

func (Config) Suffix

func (c Config) Suffix() []string

Suffix returns the list of strings to be appended to the result.

Example

ExampleConfig_Suffix demonstrates accessing suffix strings.

config := Make(
	WithSuffix([]string{"/bin", "/opt/bin"}),
)

suffix := config.Suffix()
fmt.Println(strings.Join(suffix, ", "))
Output:

/bin, /opt/bin

type Option

type Option[T any] func(T) T

Option functions return their argument with modifications applied.

func WithDelim

func WithDelim(delim string) Option[Config]

WithDelim returns an option that sets the string tokenizing delimiter.

Example

ExampleWithDelim demonstrates setting the delimiter.

// Create config using semicolon as delimiter (Windows PATH-like)
config := Make(
	WithSubject([]string{"C:\\Program Files\\App;C:\\Windows"}),
	WithDelim(";"),
)

fmt.Println(config.String())
Output:

C:\Program Files\App;C:\Windows

func WithFilter added in v0.4.0

func WithFilter(predicate func(string) bool) Option[Config]

WithFilter returns an option that sets the predicate function used to select yielded strings.

Example

ExampleWithFilter demonstrates filtering elements using a predicate.

config := Make(
	WithSubject([]string{"/usr/local/bin:/usr/bin:/bin"}),
	WithDelim(":"),
	WithFilter(func(s string) bool {
		return !strings.Contains(s, "local")
	}),
)

fmt.Println(config.String())
Output:

/usr/bin:/bin

func WithPrefix

func WithPrefix(prefixes []string) Option[Config]

WithPrefix returns an option that sets all strings to prepend after processing.

Strings are prepended in the order they are given, meaning the trailing argument will lead the result; or, the leading argument is the first to be prepended.

Example

ExampleWithPrefix demonstrates prepending elements.

config := Make(
	WithSubject([]string{"/usr/bin:/bin"}),
	WithDelim(":"),
	WithPrefix([]string{"/usr/local/bin"}),
)

fmt.Println(config.String())
Output:

/usr/local/bin:/usr/bin:/bin
Example (Order)

ExampleWithPrefix_order shows how ordering works with multiple prefixes.

// Note: prefixes are prepended in reverse order of arguments
config := Make(
	WithSubject([]string{"/bin"}),
	WithDelim(":"),
	WithPrefix([]string{"/usr/local/bin", "/usr/bin"}),
)

fmt.Println(config.String())
Output:

/usr/bin:/usr/local/bin:/bin

func WithPrefixItems

func WithPrefixItems(prefixes ...string) Option[Config]

WithPrefixItems returns an option that adds strings to prepend after processing.

Strings are prepended in the order they are given, meaning the trailing argument will lead the result; or, the leading argument is the first to be prepended.

Example

ExampleWithPrefixItems demonstrates adding prefixes.

config := Make(
	WithSubject([]string{"/bin"}),
	WithDelim(":"),
	WithPrefix([]string{"/usr/bin"}),
	WithPrefixItems("/usr/local/bin"),
)

fmt.Println(config.String())
Output:

/usr/local/bin:/usr/bin:/bin

func WithRemove

func WithRemove(removes []string) Option[Config]

WithRemove returns an option that sets all strings to remove during processing.

Example

ExampleWithRemove demonstrates removing elements.

config := Make(
	WithSubject([]string{"/usr/local/bin:/usr/bin:/bin:/opt/bin"}),
	WithDelim(":"),
	WithRemove([]string{"/usr/bin", "/opt/bin"}),
)

fmt.Println(config.String())
Output:

/usr/local/bin:/bin

func WithRemoveItems

func WithRemoveItems(removes ...string) Option[Config]

WithRemoveItems returns an option that adds strings to remove during processing.

Example

ExampleWithRemoveItems demonstrates adding items to remove.

config := Make(
	WithSubject([]string{"/usr/local/bin:/usr/bin:/bin:/opt/bin"}),
	WithDelim(":"),
	WithRemove([]string{"/usr/bin"}),
	WithRemoveItems("/opt/bin"),
)

fmt.Println(config.String())
Output:

/usr/local/bin:/bin

func WithReplace

func WithReplace(replace map[string]string) Option[Config]

WithReplace returns an option that sets all whole/fixed-string substitution rules to apply after processing.

Example

ExampleWithReplace demonstrates replacing elements.

config := Make(
	WithSubject([]string{"/usr/local/bin:/usr/bin:/bin"}),
	WithDelim(":"),
	WithReplace(map[string]string{
		"/usr/bin": "/opt/bin",
		"/bin":     "/sbin",
	}),
)

fmt.Println(config.String())
Output:

/usr/local/bin:/opt/bin:/sbin

func WithReplaceEach added in v0.2.0

func WithReplaceEach(replacements iter.Seq2[string, string]) Option[Config]

WithReplaceEach returns an option that adds individual whole/fixed-string substitution rules to apply after processing.

The given sequence seq must yield paired strings in which the first string is the item to replace, and the second is the replacement.

Example

ExampleWithReplaceEach demonstrates adding replacement rules using an iterator.

// Define a replacement sequence
replacements := func(yield func(string, string) bool) {
	if !yield("/usr/bin", "/opt/bin") {
		return
	}
	_ = yield("/bin", "/sbin")
}

config := Make(
	WithSubject([]string{"/usr/local/bin:/usr/bin:/bin"}),
	WithDelim(":"),
	WithReplaceEach(replacements),
)

fmt.Println(config.String())
Output:

/usr/local/bin:/opt/bin:/sbin

func WithReplaceItem added in v0.2.0

func WithReplaceItem(from, to string) Option[Config]

WithReplaceItem returns an option that adds an individual whole/fixed-string substitution rule to apply after processing.

Example

ExampleWithReplaceItem demonstrates adding a single replacement rule.

config := Make(
	WithSubject([]string{"/usr/local/bin:/usr/bin:/bin"}),
	WithDelim(":"),
	WithReplaceItem("/usr/bin", "/opt/bin"),
)

fmt.Println(config.String())
Output:

/usr/local/bin:/opt/bin:/bin

func WithReplaceItems

func WithReplaceItems(replacements ...map[string]string) Option[Config]

WithReplaceItems returns an option that adds whole/fixed-string substitution rules to apply after processing.

The given replacements must yield maps in which each map's key is the item to replace, and the value is the replacement.

Example

ExampleWithReplaceItems demonstrates adding replacement rules using maps.

replacements := map[string]string{
	"/usr/bin": "/opt/bin",
	"/bin":     "/sbin",
}

config := Make(
	WithSubject([]string{"/usr/local/bin:/usr/bin:/bin"}),
	WithDelim(":"),
	WithReplaceItems(replacements),
)

fmt.Println(config.String())
Output:

/usr/local/bin:/opt/bin:/sbin

func WithSubject

func WithSubject(subjects []string) Option[Config]

WithSubject returns an option that sets all subject strings to be processed.

Example

ExampleWithSubject demonstrates setting the subject strings.

config := Make(
	WithSubject([]string{"/usr/local/bin:/usr/bin:/bin"}),
	WithDelim(":"),
)

fmt.Println(config.String())
Output:

/usr/local/bin:/usr/bin:/bin

func WithSubjectItems

func WithSubjectItems(subjects ...string) Option[Config]

WithSubjectItems returns an option that adds subject strings to be processed.

Example

ExampleWithSubjectItems demonstrates adding to subject strings.

config := Make(
	WithSubject([]string{"/usr/local/bin"}),
	WithDelim(":"),
	WithSubjectItems("/usr/bin", "/bin"),
)

fmt.Println(config.String())
Output:

/usr/local/bin:/usr/bin:/bin

func WithSuffix

func WithSuffix(suffixes []string) Option[Config]

WithSuffix returns an option that sets all strings to append after processing.

Strings are appended in the order they are given, meaning the trailing argument will trail the result; or, the leading argument is the first to be appended.

Example

ExampleWithSuffix demonstrates appending elements.

config := Make(
	WithSubject([]string{"/usr/local/bin:/usr/bin"}),
	WithDelim(":"),
	WithSuffix([]string{"/bin", "/opt/bin"}),
)

fmt.Println(config.String())
Output:

/usr/local/bin:/usr/bin:/bin:/opt/bin

func WithSuffixItems

func WithSuffixItems(suffixes ...string) Option[Config]

WithSuffixItems returns an option that adds strings to append after processing.

Example

ExampleWithSuffixItems demonstrates adding suffixes.

config := Make(
	WithSubject([]string{"/usr/local/bin:/usr/bin"}),
	WithDelim(":"),
	WithSuffix([]string{"/bin"}),
	WithSuffixItems("/opt/bin"),
)

fmt.Println(config.String())
Output:

/usr/local/bin:/usr/bin:/bin:/opt/bin

Directories

Path Synopsis
cmd
mung command
mung/run
Package run contains the full application logic for the mung CLI.
Package run contains the full application logic for the mung CLI.

Jump to

Keyboard shortcuts

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