Skip to content

Commit 1bbac2a

Browse files
authored
Add "--stderr" to redirect console to stderr (for "cat" and "dump") (#353)
* added "--stderr" to redirect console to stderr also auto-enable it for "dump" and "cat" commands * updated documentation
1 parent 8297f50 commit 1bbac2a

File tree

8 files changed

+33
-3
lines changed

8 files changed

+33
-3
lines changed

constants/command.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const (
1010
CommandSnapshots = "snapshots"
1111
CommandUnlock = "unlock"
1212
CommandMount = "mount"
13+
CommandCat = "cat"
1314
CommandCopy = "copy"
1415
CommandDump = "dump"
1516
CommandFind = "find"

docs/content/usage/_index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ There are not many options on the command line, most of the options are in the c
8282
* **[-v | --verbose]**: Force resticprofile and restic to be verbose (override any configuration from the profile)
8383
* **[--trace]**: Display even more debugging information
8484
* **[--no-ansi]**: Disable console colouring (to save output into a log file)
85+
* **[--stderr]**: Send console output from resticprofile to stderr (is enabled for commands `cat` and `dump`)
8586
* **[--no-lock]**: Disable resticprofile locks, neither create nor fail on a lock. restic locks are unaffected by this option.
8687
* **[--theme]**: Can be `light`, `dark` or `none`. The colours will adjust to a
8788
light or dark terminal (none to disable colouring)
@@ -114,6 +115,7 @@ Most flags for resticprofile can be set using environment variables. If both are
114115
| `--dry-run` | `RESTICPROFILE_DRY_RUN` | `false` |
115116
| `--no-lock` | `RESTICPROFILE_NO_LOCK` | `false` |
116117
| `--lock-wait` | `RESTICPROFILE_LOCK_WAIT` | `0` |
118+
| `--stderr` | `RESTICPROFILE_STDERR` | `false` |
117119
| `--no-ansi` | `RESTICPROFILE_NO_ANSI` | `false` |
118120
| `--theme` | `RESTICPROFILE_THEME` | `"light"` |
119121
| `--no-priority` | `RESTICPROFILE_NO_PRIORITY` | `false` |

flags.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type commandLineFlags struct {
3333
resticArgs []string
3434
wait bool
3535
isChild bool
36+
stderr bool
3637
parentPort int
3738
noPriority bool
3839
ignoreOnBattery int
@@ -85,6 +86,7 @@ func loadFlags(args []string) (*pflag.FlagSet, commandLineFlags, error) {
8586
dryRun: envValueOverride(false, "RESTICPROFILE_DRY_RUN"),
8687
noLock: envValueOverride(false, "RESTICPROFILE_NO_LOCK"),
8788
lockWait: envValueOverride(time.Duration(0), "RESTICPROFILE_LOCK_WAIT"),
89+
stderr: envValueOverride(false, "RESTICPROFILE_STDERR"),
8890
noAnsi: envValueOverride(false, "RESTICPROFILE_NO_ANSI"),
8991
theme: envValueOverride(constants.DefaultTheme, "RESTICPROFILE_THEME"),
9092
noPriority: envValueOverride(false, "RESTICPROFILE_NO_PRIORITY"),
@@ -104,6 +106,7 @@ func loadFlags(args []string) (*pflag.FlagSet, commandLineFlags, error) {
104106
flagset.BoolVar(&flags.dryRun, "dry-run", flags.dryRun, "display the restic commands instead of running them")
105107
flagset.BoolVar(&flags.noLock, "no-lock", flags.noLock, "skip profile lock file")
106108
flagset.DurationVar(&flags.lockWait, "lock-wait", flags.lockWait, "wait up to duration to acquire a lock (syntax \"1h5m30s\")")
109+
flagset.BoolVar(&flags.stderr, "stderr", flags.noAnsi, "send console output to stderr (enabled for \"cat\" and \"dump\")")
107110
flagset.BoolVar(&flags.noAnsi, "no-ansi", flags.noAnsi, "disable ansi control characters (disable console colouring)")
108111
flagset.StringVar(&flags.theme, "theme", flags.theme, "console colouring theme (dark, light, none)")
109112
flagset.BoolVar(&flags.noPriority, "no-prio", flags.noPriority, "don't change the process priority: used when started from a service that has already set the priority")

flags_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func TestEnvOverrides(t *testing.T) {
6969
dryRun: setEnv(true, "RESTICPROFILE_DRY_RUN").(bool),
7070
noLock: setEnv(true, "RESTICPROFILE_NO_LOCK").(bool),
7171
lockWait: setEnv(time.Minute*5, "RESTICPROFILE_LOCK_WAIT").(time.Duration),
72+
stderr: setEnv(true, "RESTICPROFILE_STDERR").(bool),
7273
noAnsi: setEnv(true, "RESTICPROFILE_NO_ANSI").(bool),
7374
theme: setEnv("custom-theme", "RESTICPROFILE_THEME").(string),
7475
noPriority: setEnv(true, "RESTICPROFILE_NO_PRIORITY").(bool),

logger.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/creativeprojects/resticprofile/term"
1919
"github.com/creativeprojects/resticprofile/util"
2020
"github.com/creativeprojects/resticprofile/util/collect"
21+
"github.com/fatih/color"
2122
)
2223

2324
type LogCloser interface {
@@ -26,6 +27,12 @@ type LogCloser interface {
2627
}
2728

2829
func setupConsoleLogger(flags commandLineFlags) {
30+
if flags.stderr {
31+
out := color.Output
32+
color.Output = color.Error
33+
defer func() { color.Output = out }()
34+
}
35+
2936
consoleHandler := clog.NewConsoleHandler("", log.LstdFlags)
3037
if flags.theme != "" {
3138
consoleHandler.SetTheme(flags.theme)

main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func main() {
5252
args := os.Args[1:]
5353
_, flags, flagErr := loadFlags(args)
5454
if flagErr != nil && flagErr != pflag.ErrHelp {
55-
fmt.Println(flagErr)
55+
term.Println(flagErr)
5656
_ = displayHelpCommand(os.Stdout, commandContext{
5757
ownCommands: ownCommands,
5858
Context: Context{
@@ -70,7 +70,7 @@ func main() {
7070
// keep the console running at the end of the program
7171
// so we can see what's going on
7272
defer func() {
73-
fmt.Println("\n\nPress the Enter Key to continue...")
73+
term.Println("\n\nPress the Enter Key to continue...")
7474
fmt.Scanln()
7575
}()
7676
}
@@ -113,6 +113,12 @@ func main() {
113113
if ctx != nil {
114114
logTarget = ctx.logTarget
115115
commandOutput = ctx.commandOutput
116+
if ctx.request.command == constants.CommandCat ||
117+
ctx.request.command == constants.CommandDump {
118+
clog.Debugf("redirecting console to stderr for command %q", ctx.request.command)
119+
flags.stderr = true
120+
}
121+
term.PrintToError = flags.stderr
116122
}
117123
if logTarget != "" && logTarget != "-" {
118124
if closer, err := setupTargetLogger(flags, logTarget, commandOutput); err == nil {

term/term.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
var (
1616
terminalOutput io.Writer = os.Stdout
1717
errorOutput io.Writer = os.Stderr
18+
PrintToError = false
1819
)
1920

2021
// Flusher allows a Writer to declare it may buffer content that can be flushed
@@ -165,18 +166,27 @@ func FlushAllOutput() {
165166
// Spaces are added between operands when neither is a string.
166167
// It returns the number of bytes written and any write error encountered.
167168
func Print(a ...interface{}) (n int, err error) {
169+
if PrintToError {
170+
return fmt.Fprint(errorOutput, a...)
171+
}
168172
return fmt.Fprint(terminalOutput, a...)
169173
}
170174

171175
// Println formats using the default formats for its operands and writes to standard output.
172176
// Spaces are always added between operands and a newline is appended.
173177
// It returns the number of bytes written and any write error encountered.
174178
func Println(a ...interface{}) (n int, err error) {
179+
if PrintToError {
180+
return fmt.Fprintln(errorOutput, a...)
181+
}
175182
return fmt.Fprintln(terminalOutput, a...)
176183
}
177184

178185
// Printf formats according to a format specifier and writes to standard output.
179186
// It returns the number of bytes written and any write error encountered.
180187
func Printf(format string, a ...interface{}) (n int, err error) {
188+
if PrintToError {
189+
return fmt.Fprintf(errorOutput, format, a...)
190+
}
181191
return fmt.Fprintf(terminalOutput, format, a...)
182192
}

update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func confirmAndSelfUpdate(quiet, debug bool, version string, prerelease bool) er
6868

6969
// don't ask in quiet mode
7070
if !quiet && !term.AskYesNo(os.Stdin, fmt.Sprintf("Do you want to update to version %s", latest.Version()), true) {
71-
fmt.Println("Never mind")
71+
term.Println("Never mind")
7272
return nil
7373
}
7474

0 commit comments

Comments
 (0)