Skip to content

xgfone/go-exec

Repository files navigation

Command Execution Build Status GoDoc License

The package supplies the common command execution supporting Go1.7+.

Install

$ go get -u github.com/xgfone/go-exec

Example

package main

import (
	"context"
	"fmt"

	"github.com/xgfone/go-exec"
)

const scripttmpl = `
ls %s
rm -rf %s
`

func main() {
	exec.Execute(context.Background(), "mkdir", "testdir")
	exec.ExecuteShellCmd(context.Background(), "echo abc > %s/%s", "testdir", "testfile")

	data, _ := exec.OutputShellCmd(context.Background(), "cat %s/%s", "testdir", "testfile")
	fmt.Println(data)

	_, _, err := exec.RunShellScript(context.Background(), scripttmpl, "testdir", "testdir")
	fmt.Println(err)

	// Output:
	// abc
	//
	// <nil>
}