mirror of
https://github.com/kopia/kopia.git
synced 2026-01-23 22:07:54 -05:00
- removed a bunch of hacks and should improve the logging performance by avoiding interfaces and data translation. This will allow using of de-sugared loggers in performance-critical logging situations. - this will also allow using features of ZAP more directly without having to reimplement them. - moved logging.Printf() to testlogging - refactored `uitask` to store logs in a structural format and present them as JSON only in the UI - renamed printf_logger.go to printf.go so that fewer columns are used in the logs
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
|
|
"github.com/alecthomas/kingpin"
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/kopia/kopia/repo/logging"
|
|
)
|
|
|
|
// RunSubcommand executes the subcommand asynchronously in current process
|
|
// with flags in an isolated CLI environment and returns standard output and standard error.
|
|
func (c *App) RunSubcommand(ctx context.Context, kpapp *kingpin.Application, argsAndFlags []string) (stdout, stderr io.Reader, wait func() error, kill func()) {
|
|
stdoutReader, stdoutWriter := io.Pipe()
|
|
stderrReader, stderrWriter := io.Pipe()
|
|
|
|
c.stdoutWriter = stdoutWriter
|
|
c.stderrWriter = stderrWriter
|
|
c.rootctx = logging.WithLogger(ctx, logging.ToWriter(stderrWriter))
|
|
|
|
c.Attach(kpapp)
|
|
|
|
var exitCode int
|
|
|
|
resultErr := make(chan error, 1)
|
|
|
|
c.osExit = func(ec int) {
|
|
exitCode = ec
|
|
}
|
|
|
|
go func() {
|
|
defer close(resultErr)
|
|
defer stderrWriter.Close() //nolint:errcheck
|
|
defer stdoutWriter.Close() //nolint:errcheck
|
|
|
|
_, err := kpapp.Parse(argsAndFlags)
|
|
if err != nil {
|
|
resultErr <- err
|
|
return
|
|
}
|
|
|
|
if exitCode != 0 {
|
|
resultErr <- errors.Errorf("exit code %v", exitCode)
|
|
return
|
|
}
|
|
}()
|
|
|
|
return stdoutReader, stderrReader, func() error {
|
|
return <-resultErr
|
|
}, func() {}
|
|
}
|