From a2b76ff34f93867281fa2c9df327c0ae2af3b774 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Ho=C3=9F?= Date: Thu, 16 Mar 2023 08:11:49 +0100 Subject: [PATCH] Start command from --stdin-from-command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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ß --- cmd/restic/cmd_backup.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index 56e3c7939..3f494a8cf 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "os" + "os/exec" "path" "path/filepath" "runtime" @@ -594,16 +595,37 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter defer localVss.DeleteSnapshots() targetFS = localVss } - if opts.Stdin { + + var command *exec.Cmd + var stderr io.ReadCloser + if opts.Stdin || opts.StdinCommand { if !gopts.JSON { progressPrinter.V("read data from stdin") } 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{ ModTime: timeStamp, Name: filename, Mode: 0644, - ReadCloser: os.Stdin, + ReadCloser: closer, } targets = []string{filename} }