Start command from --stdin-from-command

It acts similar to --stdin but reads its data from the stdout of the given command instead of os.Stdin.

Signed-off-by: Sebastian Hoß <seb@xn--ho-hia.de>
This commit is contained in:
Sebastian Hoß 2023-03-16 08:11:49 +01:00 committed by Michael Eischer
parent 333fe1c3cf
commit a2b76ff34f
1 changed files with 24 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"os/exec"
"path" "path"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -594,16 +595,37 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter
defer localVss.DeleteSnapshots() defer localVss.DeleteSnapshots()
targetFS = localVss targetFS = localVss
} }
if opts.Stdin {
var command *exec.Cmd
var stderr io.ReadCloser
if opts.Stdin || opts.StdinCommand {
if !gopts.JSON { if !gopts.JSON {
progressPrinter.V("read data from stdin") progressPrinter.V("read data from stdin")
} }
filename := path.Join("/", opts.StdinFilename) filename := path.Join("/", opts.StdinFilename)
var closer io.ReadCloser
if opts.StdinCommand {
command = exec.CommandContext(ctx, args[0], args[1:]...)
stdout, err := command.StdoutPipe()
if err != nil {
return err
}
stderr, err = command.StderrPipe()
if err != nil {
return err
}
if err := command.Start(); err != nil {
return err
}
closer = stdout
} else {
closer = os.Stdin
}
targetFS = &fs.Reader{ targetFS = &fs.Reader{
ModTime: timeStamp, ModTime: timeStamp,
Name: filename, Name: filename,
Mode: 0644, Mode: 0644,
ReadCloser: os.Stdin, ReadCloser: closer,
} }
targets = []string{filename} targets = []string{filename}
} }