shell

package module
v0.0.0-...-ccb53e0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2016 License: MIT Imports: 7 Imported by: 11 modules, 13 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

shell Build Status GoDoc

Generate Shell Commands in Go, sprintf Style. Inspired by libphutil.

Quick example:

// Generates shell command to find number of go files on a remote machine
host := "foo.com"
findCmd := shell.Sprintf("find . -iname %s | wc -l", "*.go")
remoteCmd := shell.Sprintf("ssh ubuntu@%s %s", host, findCmd)
fmt.Println(remoteCmd)
// Output: ssh ubuntu@foo.com 'find . -iname '\''*.go'\'' | wc -l'

Also slightly extends Sprintf syntax to allow easily passing in slices. This is a common use case when interacting with commands that take in a list of arguments:

// Support for passing in a slice of arguments
gitcmd := shell.Sprintf("git add %S", []string{"foo.go", "bar.go", "test data"})
fmt.Println(gitcmd)
// Output: git add foo.go bar.go 'test data'

See https://godoc.org/github.com/keegancsmith/shell for more information.

Documentation

Overview

Package shell provides utilities for building command strings to execute in sh. It also provides a wrapper around os/exec, but instead of an Args slice you can pass in a format string.

A common use case is generating safe strings for passing to commands that execute other shell commands (such as SSH). Otherwise you may find this a more convenient way to build up commands vs passing a slice to exec.Command

This way of interacting with the shell is inspired by the csprintf related functions from libphutil

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Commandf

func Commandf(format string, a ...interface{}) *exec.Cmd

Commandf runs a shell command based on the format string

Example
package main

import (
	"fmt"
	"log"

	"github.com/keegancsmith/shell"
)

func main() {
	out, err := shell.Commandf("echo %s", "hello world").Output()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(out))
}
Output:
hello world
Example (Redirect)
package main

import (
	"bytes"
	"fmt"
	"log"
	"strings"

	"github.com/keegancsmith/shell"
)

func main() {
	var stdout, stderr bytes.Buffer
	cmd := shell.Commandf("echo %s; echo %s 1>&2", "hello from stdout", "hello from stderr")
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	err := cmd.Run()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("stdout:", strings.TrimSpace(stdout.String()))
	fmt.Println("stderr:", strings.TrimSpace(stderr.String()))
}
Output:
stdout: hello from stdout
stderr: hello from stderr

func EscapeArg

func EscapeArg(arg string) string

EscapeArg escapes a string such that it is safe to pass to a shell. It is a re-implementation of PHP's escapeshellarg

func ReadableEscapeArg

func ReadableEscapeArg(arg string) string

ReadableEscapeArg will not escape strings that do not requiring escaping. Note that it is conservative, so may escape strings which do not require it.

func Sprintf

func Sprintf(format string, a ...interface{}) string

Sprintf generates a shell command with the format arguments escaped.

Example
package main

import (
	"fmt"

	"github.com/keegancsmith/shell"
)

func main() {
	// Generates shell command to find number of go files on a remote machine
	host := "foo.com"
	findCmd := shell.Sprintf("find . -iname %s | wc -l", "*.go")
	remoteCmd := shell.Sprintf("ssh ubuntu@%s %s", host, findCmd)
	fmt.Println(remoteCmd)
}
Output:
ssh ubuntu@foo.com 'find . -iname '\''*.go'\'' | wc -l'

Types

This section is empty.

Jump to

Keyboard shortcuts

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