2
2
mirror of https://github.com/octoleo/restic.git synced 2024-05-28 22:50:48 +00:00

Allow --files-from to take a dash for stdin (fixes #769)

This commit is contained in:
opennota 2017-02-06 18:43:44 +07:00
parent ec45cdba84
commit 24acb09a2a

View File

@ -3,6 +3,7 @@ package main
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"io"
"os" "os"
"path/filepath" "path/filepath"
"restic" "restic"
@ -28,6 +29,10 @@ The "backup" command creates a new snapshot and saves the files and directories
given as the arguments. given as the arguments.
`, `,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
if backupOptions.Stdin && backupOptions.FilesFrom == "-" {
return errors.Fatal("cannot use both `--stdin` and `--files-from -`")
}
if backupOptions.Stdin { if backupOptions.Stdin {
return readBackupFromStdin(backupOptions, globalOptions, args) return readBackupFromStdin(backupOptions, globalOptions, args)
} }
@ -266,21 +271,26 @@ func readBackupFromStdin(opts BackupOptions, gopts GlobalOptions, args []string)
// readFromFile will read all lines from the given filename and write them to a // readFromFile will read all lines from the given filename and write them to a
// string array, if filename is empty readFromFile returns and empty string // string array, if filename is empty readFromFile returns and empty string
// array // array. If filename is a dash (-), readFromFile will read the lines from
// the standard input.
func readLinesFromFile(filename string) ([]string, error) { func readLinesFromFile(filename string) ([]string, error) {
if filename == "" { if filename == "" {
return nil, nil return nil, nil
} }
file, err := os.Open(filename) var r io.Reader = os.Stdin
if err != nil { if filename != "-" {
return nil, err f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
r = f
} }
defer file.Close()
var lines []string var lines []string
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(r)
for scanner.Scan() { for scanner.Scan() {
lines = append(lines, scanner.Text()) lines = append(lines, scanner.Text())
} }
@ -293,6 +303,10 @@ func readLinesFromFile(filename string) ([]string, error) {
} }
func runBackup(opts BackupOptions, gopts GlobalOptions, args []string) error { func runBackup(opts BackupOptions, gopts GlobalOptions, args []string) error {
if opts.FilesFrom == "-" && gopts.password == "" && gopts.PasswordFile == "" {
return errors.Fatal("no password; either use `--password-file` option or put the password into the RESTIC_PASSWORD environment variable")
}
fromfile, err := readLinesFromFile(opts.FilesFrom) fromfile, err := readLinesFromFile(opts.FilesFrom)
if err != nil { if err != nil {
return err return err