Coder Social home page Coder Social logo

Comments (8)

bitfield avatar bitfield commented on July 22, 2024 1

Great! I look forward to seeing what interesting programs you come up with!

from script.

bitfield avatar bitfield commented on July 22, 2024

That's a good suggestion! How do you envisage this looking from the code point of view? What script pipeline would you write to get real-time output?

from script.

marco-m avatar marco-m commented on July 22, 2024

One possibility would be:

script.Exec("ping -c 100 www.google.com").Stream()

and the implementation could just use StdoutPipe() and StderrPipe() of package os.exec, something along the lines of (didn't check the source of script.Exec, this code doesn't follow script's design with Pipe, it is just to give an idea):

        cmd := exec.Command(...)

	stdout, err := cmd.StdoutPipe()
	if err != nil {
		return err
	}
	stderr, err := cmd.StderrPipe()
	if err != nil {
		return err
	}

	if err := cmd.Start(); err != nil {
		return err
	}

	scanOut := bufio.NewScanner(stdout)
	for scanOut.Scan() {
		line := scanOut.Text()
		if line != "" {
			fmt.Println(line)
		}
	}
	scanErr := bufio.NewScanner(stderr)
	for scanErr.Scan() {
		line := scanErr.Text()
		if line != "" {
			fmt.Println(line)
		}
	}

        // maybe here we need something smarter and collect the errors so that we always call cmd.Wait()
	if err := scanOut.Err(); err != nil {
		return err
	}
	if err := scanErr.Err(); err != nil {
		return err
	}

	if err := cmd.Wait(); err != nil {
		return err
	}

from script.

bitfield avatar bitfield commented on July 22, 2024

That sounds promising! Do you want to put a PR together along these lines so we can try it out?

from script.

bitfield avatar bitfield commented on July 22, 2024

Now implemented in v0.19.0.

from script.

madAndroid avatar madAndroid commented on July 22, 2024

Has this actually been implemented? I see that the PR was closed and not merged, and I don't see anything in the docs around streaming of output.. is this still something that would be implemented in time? It would be really helpful to have a Go variant of something like Python's subprocess

from script.

bitfield avatar bitfield commented on July 22, 2024

Yes, indeed. Here's what the README says:

Note that filters run concurrently, rather than producing nothing until each stage has fully read its input. This is convenient for executing long-running comands, for example. If you do need to wait for the pipeline to complete, call Wait.

The canonical example is something like OP's:

script.Exec("ping -c 100 www.google.com").Stdout()

But, as I say, all filters are now concurrent, including Exec. Try it!

from script.

madAndroid avatar madAndroid commented on July 22, 2024

Thank you for the reply - yes indeed, I can see the output is concurrent - I'm running a sonobuy test using Exec(), and the output is streamed to the console 👍

from script.

Related Issues (20)

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.