zenizh / go-capturer Goto Github PK
View Code? Open in Web Editor NEWCapture stdout and/or stderr in Go.
Home Page: https://godoc.org/github.com/kami-zh/go-capturer
License: MIT License
Capture stdout and/or stderr in Go.
Home Page: https://godoc.org/github.com/kami-zh/go-capturer
License: MIT License
go.mod missing in this repo.
Since has shown in #7, we need to drain the buffer to do not block the A operation when full.
We should permit user to select another implementation to overcome this limitation.
This can maybe done with a new goroutine
which read and backup data from the pipe.
Made some minor changes in the package to made it more readable.
I have kept the changes in https://github.com/alok87/go-capture
capture.Stdout
instead of capturer.CaptureStdout
Let me know if you are open for contribution, then we can release it here, but those are backward incompatible. So may need a version up.
Since capture is implemented with a pipe, write are blocked when the buffer is full.
But this is not explicitly written inside the documentation.
It can be useful to record distinctly stdout and stderr.
Maybe, during unit-testing, you can separately check stdout and stderr output.
THis does not work, output is always "":
My struct and method:
type SDCInstanceOutput []scaleio.SdcInstance
// Output - filter results per cli options then output in the correct format
func (i *SDCInstanceOutput) Output(ctx *cli.Context) {
var filtered SDCInstanceOutput
for _, row := range *i {
if ctx.IsSet(FlagNameClientStatus) && strings.Compare(ctx.String(FlagNameClientStatus), "all") != 0 {
if strings.Compare(ctx.String(FlagNameClientStatus), strings.ToLower(row.MdmConnectionState)) != 0 {
continue
}
}
filtered = append(filtered, row)
}
switch strings.ToLower(ctx.String(FlagNameOutputMode)) {
case "csv":
filtered.CSV()
case "human":
filtered.Tabular()
case "json":
filtered.JSON(ctx)
default:
log.Fatalf("ERROR: invalid mode %q", ctx.String(FlagNameOutputMode))
}
}
Here is where Im trying to write a test, so using a test command like so. output is always "" even though the function is outputting 3 lines of text:
app.Commands = []*cli.Command{
&cli.Command{
Name: "test-cmd",
Flags: []cli.Flag{FlagClientStatus, FlagOutputMode},
Action: func(x *cli.Context) error {
output = capturer.CaptureOutput(func() {
c.givenResult.Output(x)
})
log.Infof("OUTPUT=%s", output)
return nil
},
},
}
if err := app.Run(c.givenArgs); err != nil {
t.Fatal(err)
}
resultCount := lineCount(output)
if c.wantedRows != resultCount {
t.Errorf("expecting %d results, got %d", c.wantedRows, resultCount)
}
% go test -v
=== RUN TestSDCInstanceOutput_Output
Name IP Active Version Installed Version OS Type Kernel Status
Connected
Connected
time="2021-09-10T16:57:24.084735-05:00" level=info file="commands_test.go" line=61 msg="OUTPUT="
commands_test.go:73: expecting 2 results, got 0
Hi,
When I try to capture stdout as a result of running an exec command, the output is empty. For example:
cmd := exec.Command("whoami")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
out := capturer.CaptureOutput(func() {
_ = cmd.Run()
})
fmt.Println(out) // empty
Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.