Git Product home page Git Product logo

go-shell's Introduction

logo.png

go-shell

easy execute shell, better os/exec

Feature

  • simple api
  • add timeout
  • add stop()
  • add yum api
  • use channel to send stdout and stderr
  • merge stdout and stderr to new output
  • use sync.pool to reduce alloc buffer

Usage

😁 Look at the code for yourself

func TestCheckOutput(t *testing.T) {
	cmd := NewCommand("echo -n 123123")
	cmd.Run() // start and wait
	status := cmd.Status

	assert.Equal(t, status.Output, "123123")
	assert.Equal(t, status.Stdout, "123123")
	assert.Equal(t, status.Stderr, "")
}

func TestCheckStderr(t *testing.T) {
	cmd := NewCommand("echo -n \"123123\" >&2")
	cmd.Run() // start and wait
	status := cmd.Status

	assert.Equal(t, status.Stdout, "")
	assert.Equal(t, status.Output, "123123")
	assert.Equal(t, status.Stderr, "123123")
}

func TestDelayStop(t *testing.T) {
	start := time.Now()
	cmd := NewCommand("sleep 5;echo 123;sleep 123")
	cmd.Start() // async start

	go func() {
		time.Sleep(1 * time.Second)
		cmd.Stop()
	}()

	cmd.Wait() // wait
	end := time.Since(start)
	assert.Less(t, end.Seconds(), float64(2))
}

func TestRunTimeout(t *testing.T) {
	cmd := NewCommand("echo 123; sleep 5", WithTimeout(2))
	cmd.Start()
	cmd.Wait()
	status := cmd.Status

	assert.Equal(t, status.Error, ErrProcessTimeout)
	assert.Greater(t, status.CostTime.Seconds(), float64(2))
	assert.Less(t, status.CostTime.Seconds(), float64(3))
}

func TestCheckStderr(t *testing.T) {
	cmd := NewCommand("echo -n \"123123\" >&2") // echo stderr
	cmd.Run() // start and wait
	status := cmd.Status

	assert.Equal(t, status.Stdout, "")
	assert.Equal(t, status.Output, "123123")
	assert.Equal(t, status.Stderr, "123123")
}

func TestCheckStream(t *testing.T) {
	stdoutChan := make(chan string, 100)
	incr := 0
	go func() {
		for line := range stdoutChan {
			incr++
			fmt.Println(incr, line)
		}
	}()

	cmd := exec.Command("bash", "-c", "echo 123;sleep 1;echo 456; echo 789")
	stdout := NewOutputStream(stdoutChan)
	cmd.Stdout = stdout
	cmd.Run()

	assert.Equal(t, incr, 3)
}

func TestCheckBuffer(t *testing.T) {
	cmd := exec.Command("bash", "-c", "echo 123")
	stdout := NewOutputBuffer()
	cmd.Stdout = stdout
	cmd.Run()

	assert.Equal(t, stdout.buf.String(), "123\n")
	assert.Equal(t, stdout.Lines()[0], "123")
}

go-shell's People

Contributors

rfyiamcool avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

go-shell's Issues

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.