Files
kopia/cli/inproc.go
Jarek Kowalski b9be9632a2 feat(repository): added required features to the repository (#2220)
* feat(repository): added `required features` to the repository

This is intended for future compatibility to be able to reliably
stop old kopia client from being able to open a repository when
the old code does not understand new `required feature`.

Required features are checked on startup and periodically using the
same method as upgrade lock, where they will return errors during blob
operations.

* pr feedback
2022-07-29 09:31:17 -07:00

67 lines
1.5 KiB
Go

package cli
import (
"context"
"io"
"github.com/alecthomas/kingpin"
"github.com/kopia/kopia/internal/releasable"
"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, stdin io.Reader, argsAndFlags []string) (stdout, stderr io.Reader, wait func() error, kill func()) {
stdoutReader, stdoutWriter := io.Pipe()
stderrReader, stderrWriter := io.Pipe()
c.stdinReader = stdin
c.stdoutWriter = stdoutWriter
c.stderrWriter = stderrWriter
c.rootctx = logging.WithLogger(ctx, logging.ToWriter(stderrWriter))
c.simulatedCtrlC = make(chan bool, 1)
c.isInProcessTest = true
releasable.Created("simulated-ctrl-c", c.simulatedCtrlC)
c.Attach(kpapp)
var exitError error
resultErr := make(chan error, 1)
c.exitWithError = func(ec error) {
exitError = ec
}
go func() {
defer func() {
close(c.simulatedCtrlC)
releasable.Released("simulated-ctrl-c", c.simulatedCtrlC)
}()
defer close(resultErr)
defer stderrWriter.Close() //nolint:errcheck
defer stdoutWriter.Close() //nolint:errcheck
_, err := kpapp.Parse(argsAndFlags)
if err != nil {
resultErr <- err
return
}
if exitError != nil {
resultErr <- exitError
return
}
}()
return stdoutReader, stderrReader, func() error {
return <-resultErr
}, func() {
// deliver simulated Ctrl-C to the app.
c.simulatedCtrlC <- true
}
}