sh

package
v2.0.0-alpha.3 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package sh provides tools to build and manipulate shell commands.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Command

func Command(cmd string, args ...string) string

Command returns a shell escaped command string.

Example:

c.Exec(sh.Command("echo", "hello world"))
// resulting command: echo 'hello world'
Example
package main

import (
	"fmt"

	"github.com/k0sproject/rig/v2/sh"
)

func main() {
	fmt.Println(sh.Command("echo", "foo bar"))
}
Output:

echo 'foo bar'
Example (Builder)
package main

import (
	"fmt"

	"github.com/k0sproject/rig/v2/sh"
)

func main() {
	cmd := sh.CommandBuilder("echo").Arg("foo").ErrToNull().OutToFile("file").Pipe("grep").Args("-q", "foo bar")
	fmt.Println(cmd.String())
}
Output:

echo foo 2>/dev/null >file | grep -q 'foo bar'

func Quote

func Quote(s string) string

Quote returns a shell escaped string. This is a wrapper around shellescape.Quote and it is here to avoid importing shellescape separately.

Example
package main

import (
	"fmt"

	"github.com/k0sproject/rig/v2/sh"
)

func main() {
	fmt.Println(sh.Quote("foo bar"))
}
Output:

'foo bar'

Types

type CommandBuilder

type CommandBuilder string

CommandBuilder is a builder for shell commands. It is based on string and can be converted to one using string(CommandBuilder("foo")) or calling the String method.

func (CommandBuilder) AppendErrToFile

func (c CommandBuilder) AppendErrToFile(file string) CommandBuilder

AppendErrToFile appends the command's stderr to a file.

Example
package main

import (
	"fmt"

	"github.com/k0sproject/rig/v2/sh"
)

func main() {
	cmd := sh.CommandBuilder("echo").Arg("foo").AppendErrToFile("file")
	fmt.Println(cmd.String())
}
Output:

echo foo 2>>file

func (CommandBuilder) AppendOutToFile

func (c CommandBuilder) AppendOutToFile(file string) CommandBuilder

AppendOutToFile appends the command's stdout to a file.

Example
package main

import (
	"fmt"

	"github.com/k0sproject/rig/v2/sh"
)

func main() {
	cmd := sh.CommandBuilder("echo").Arg("foo").AppendOutToFile("file")
	fmt.Println(cmd.String())
}
Output:

echo foo >>file

func (CommandBuilder) Arg

Arg adds an argument to the command. The argument is shell escaped.

Example
package main

import (
	"fmt"

	"github.com/k0sproject/rig/v2/sh"
)

func main() {
	cmd := sh.CommandBuilder("echo").Arg("foo").Arg("bar baz")
	fmt.Println(cmd.String())
}
Output:

echo foo 'bar baz'

func (CommandBuilder) Args

func (c CommandBuilder) Args(args ...string) CommandBuilder

Args adds multiple arguments to the command. The arguments are shell escaped.

func (CommandBuilder) ErrToFile

func (c CommandBuilder) ErrToFile(file string) CommandBuilder

ErrToFile redirects the command's stderr to a file.

Example
package main

import (
	"fmt"

	"github.com/k0sproject/rig/v2/sh"
)

func main() {
	cmd := sh.CommandBuilder("echo").Arg("foo").ErrToFile("file")
	fmt.Println(cmd.String())
}
Output:

echo foo 2>file

func (CommandBuilder) ErrToNull

func (c CommandBuilder) ErrToNull() CommandBuilder

ErrToNull redirects the command's stderr to /dev/null.

Example
package main

import (
	"fmt"

	"github.com/k0sproject/rig/v2/sh"
)

func main() {
	cmd := sh.CommandBuilder("echo").Arg("foo").ErrToNull()
	fmt.Println(cmd.String())
}
Output:

echo foo 2>/dev/null

func (CommandBuilder) ErrToOut

func (c CommandBuilder) ErrToOut() CommandBuilder

ErrToOut redirects the command's stderr to stdout.

Example
package main

import (
	"fmt"

	"github.com/k0sproject/rig/v2/sh"
)

func main() {
	cmd := sh.CommandBuilder("echo").Arg("foo").ErrToOut()
	fmt.Println(cmd.String())
}
Output:

echo foo 2>&1

func (CommandBuilder) OutToFile

func (c CommandBuilder) OutToFile(file string) CommandBuilder

OutToFile redirects the command's stdout to a file.

Example
package main

import (
	"fmt"

	"github.com/k0sproject/rig/v2/sh"
)

func main() {
	cmd := sh.CommandBuilder("echo").Arg("foo").OutToFile("file")
	fmt.Println(cmd.String())
}
Output:

echo foo >file

func (CommandBuilder) OutToNull

func (c CommandBuilder) OutToNull() CommandBuilder

OutToNull redirects the command's stdout to /dev/null.

Example
package main

import (
	"fmt"

	"github.com/k0sproject/rig/v2/sh"
)

func main() {
	cmd := sh.CommandBuilder("echo").Arg("foo").OutToNull()
	fmt.Println(cmd.String())
}
Output:

echo foo >/dev/null

func (CommandBuilder) Pipe

func (c CommandBuilder) Pipe(cmd string, args ...string) CommandBuilder

Pipe the command to another command. The target command is shell escaped.

Example
package main

import (
	"fmt"

	"github.com/k0sproject/rig/v2/sh"
)

func main() {
	cmd := sh.CommandBuilder("echo").Arg("foo").Pipe("grep", "-q").Arg("foo")
	fmt.Println(cmd.String())
}
Output:

echo foo | grep -q foo

func (CommandBuilder) Raw

Raw adds a raw string to the command without shell escaping. This is needed when you want to use shell operators, globbing or variables.

Example
package main

import (
	"fmt"

	"github.com/k0sproject/rig/v2/sh"
)

func main() {
	cmd := sh.CommandBuilder("ls").Raw("**/*.go")
	fmt.Println(cmd.String())
}
Output:

ls **/*.go

func (CommandBuilder) String

func (c CommandBuilder) String() string

String returns the command as a string.

Directories

Path Synopsis
Package shellescape provides functions to escape strings for use in posix shell commands.
Package shellescape provides functions to escape strings for use in posix shell commands.

Jump to

Keyboard shortcuts

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