run

package module
v0.0.0-...-2659c38 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2015 License: MIT Imports: 15 Imported by: 0

README

go-run

Running system program with calls chained together, inspired by gulp.js.

Inspired by godo and created as a plugin for belt task runner

GoDoc

Quick Start

As a quick overview of usages, below shows an example of how to use run in general.

go-run runs independently, means that you can import it and use it anywhere you want

package main

import (
    "fmt"
    run "github.com/Fiery/go-run"
)

func main(){
    // set local env, use "::" as cross-platform path list separator
    run.Set(`GOPATH=./lib/::$GOPATH`)
    // simply executes shell command with local and global envs
    run.Shell("echo Hello $USER!").Run()


    // You could also specify local envs in command line strings, just as how you type in shell
    run.Call("GOOS=linux GOARCH=amd64 go build").Run()

    // In allows you to temporarily change to the specified folder and run
    run.Shell("bash","script.sh").In("project").Run()

    // Pipe the Stdout (and/or Stderr) to evaluate later
    var output = bytes.Buffer
    run.Call("echo Just run it!").Pipe(run.Stdout|run.Stderr, output).Run()
    fmt.Println(output.String())


    // Asynchronously runs the command
    run.Start("node server.js").Run()
}

API

run.Runnable

Runnable is the universal interface for all chainable runners available in run. Fill in your own type with below functions to implement run.Runnable.

  Run() error

  // Runnable functions can be chained in any order

  Call(string) Runnable
  Start(string) Runnable
  Shell(...string) Runnable

  With(...string) Runnable
  Pipe(int, *bytes.Buffer) Runnable

  At(string) Runnable
  In(string) Runnable
run.Shell

Shell returns an object implements Runnable, which allows you to chain another run function. It uses sh as default shell and can be specified with optional configuration.

run.Shell(`
    echo -n $USER
    echo some really long \
        command
`).Run()

above snippet will run multiline commands.

Environment variables are acceptable anywhere in command line If the binary itself is a env var, it will be expanded before execution

run.Env.Set("name=run")
run.Shell(`echo -n $name`).Run()

Can be chained by Pipe( ) to capture Stdout and Stderr or to bind Stdin.

var output bytes.Buffer
run.Shell(`bash`, `echo -n $USER`).Pipe(run.Stdout, &output).Run()
run.Call

Similarly, run.Call() returns Runnable which can be chained by Pipe( ), With( ), In( ), or another Call( ) if needed

  run.Call("echo Hello").Call("echo Hello again").Call("echo Hello again and again").Run()
  // Output:
  // Hello
  // Hello again
  // Hello again and again

run.Start

Start an async command. returns immediately.

run.Start("main").Run()
run.Runnable.In

Temporarily cd to specified path and run the Runnables

run.Call("...").In("path/to/run").Run()
run.Shell("...").In("path/to/run").Run()
run.Runnable.At

Runs the command with working directory set to be the input

run.Call("...").At("path/to/run").Run()
run.Shell("...").At("path/to/run").Run()
run.Runnable.Pipe

Any Runnable can use Pipe to direct its Stdin|Stdout|Stderr to a predefined io.Reader|io.Writer. Again it returns Runnable which can be chained with other functions.

var output = bytes.NewBuffer(nil)
run.Call(`GOOS=linux GOARCH=amd64 go build`).Pipe(run.Stdout|run.Stderr, output)
run.Runnable.With

Set command specific variables, only valid within the calling Runnable chain

run.Call("$c $t/txt").With("c=cat","t=test").Run()

Runtime Environment

Set from command line

Environment variables may be set via key-value assignments as prefix in command line.

Process environment will be set as the combination of available system environment variables and run.Env

  USER=run PATH=$PATH::./lib/ ./run/main
Set using run.Env

Use run.Env.Set To specify runtime environment for the command. Assignment expression to be separated with any type of spaces. All $VAR or ${VAR} will be expanded when loading at runtime

run.Env.Set(
    // Path list should use "::" as cross-platform path list separator.
    // On Windows "::" will be replaced with ";".
    // On Mac and linux "::" will be replaced with ":".
`
    PATH=./lib/::$PATH
    USER=user
`)
Use run.Runnable.With

More fine-grained environment configuration can be achieved by using With( )

// only valid in current Call
run.Call("$c $t/txt").With("c=cat","t=test").Run()

TIP: Set the Env when using a dependency manager like godep

wd, _ := os.Getwd()
run.Env.Set(fmt.Sprintf("GOPATH=%s::$GOPATH", path.Join(wd, "Godeps/_workspace")))

Tools

  • To get plain string user input

user := Prompt("user: ")


* To get hidden/masked user input

  ```go
password := PromptHidden("password: ")
password := PromptMasked("password: ")

Documentation

Overview

Package run is a command runner with deliberately designed chainable interface, inspired by gulp.js

Index

Constants

View Source
const (
	// Pipe bitmask options
	Stdin = 0x01 << iota
	Stdout
	Stderr

	// Shorthand version
	I = Stdin
	O = Stdout
	E = Stderr

	None = 0
)

Variables

View Source
var Env = makeEnvMap(flag.Args(), false)

Env is the default environment to use for all commands. os.exec.Cmd takes environment from the merged set of global environment and this Env set.

View Source
var PathListSeparator = "::"

PathListSeparator is a cross-platform path list separator template. Will be replaced according to different go runtim (Windows: ";", Unix/BSD: ":")

Functions

func Prompt

func Prompt(prompt string) string

Prompt prompts user for input with plain value.

func PromptHidden

func PromptHidden(prompt string) string

PromptHidden prompts user for hidden terminal input.

func PromptMasked

func PromptMasked(prompt string) string

PromptMasked prompts user for masked terminal input.

func Stop

func Stop(cmd string, spans ...[2]time.Time) error

kills processes with the corresponding command,

func Wait

func Wait()

Wait waits till all preceded async apps finish

Types

type Runnable

type Runnable interface {
	Run() error

	Call(string) Runnable
	Start(string) Runnable
	Shell(...string) Runnable

	With(...string) Runnable
	Pipe(int, *bytes.Buffer) Runnable

	At(string) Runnable
	In(string) Runnable
}

Runnable expose APIs for chainable call structure

func Call

func Call(c string) Runnable

Call defines a system call Runnable object, which calls the command with arguments

func Shell

func Shell(c ...string) Runnable

Shell defines a shell command Runnable object, which evaluates sh/bash command line. Use backquote for multiline commands. To specify a shell, insert shell name as the first string argument To run as shell script, use run.Call("sh/bash script.sh")

func Start

func Start(o string) Runnable

Start starts an async Runnable object, which does basically same as Call, returns immediately

Jump to

Keyboard shortcuts

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