GithubHelp home page GithubHelp logo

fiery / go-run Goto Github PK

View Code? Open in Web Editor NEW
2.0 4.0 0.0 0 B

Running system program with gulp-like calls chained together, designed for belt task runner

License: MIT License

Go 99.81% Batchfile 0.06% Shell 0.13%

go-run's Introduction

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: ")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.