Skip to content

Commit

Permalink
add dry-run flag
Browse files Browse the repository at this point in the history
  • Loading branch information
creativeprojects committed Jul 17, 2020
1 parent ca9f7fe commit 67e24a4
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 46 deletions.
27 changes: 19 additions & 8 deletions README.md
Expand Up @@ -25,7 +25,7 @@ The configuration file accepts various formats:
* [TOML](https://github.com/toml-lang/toml) : configuration file with extension _.toml_ and _.conf_ to keep compatibility with versions before 0.6.0
* [JSON](https://en.wikipedia.org/wiki/JSON) : configuration file with extension _.json_
* [YAML](https://en.wikipedia.org/wiki/YAML) : configuration file with extension _.yaml_
* [HCL](https://github.com/hashicorp/hcl): **experimental support**, configuration file with extension _.hcl_
* [HCL](https://github.com/hashicorp/hcl): configuration file with extension _.hcl_

For the rest of the documentation, I'll be showing examples using the TOML file configuration format (because it was the only one supported before version 0.6.0) but you can pick your favourite: they all work with resticprofile :-)

Expand Down Expand Up @@ -277,7 +277,10 @@ exclude-file = [ "root-excludes", "excludes" ]
exclude-caches = true
one-file-system = false
tag = [ "test", "dev" ]
source = [ "." ]
source = [ "/" ]
# if scheduled, will run every dat at midnight
schedule = "daily"
schedule-permission = "system"

# retention policy for profile root
[root.retention]
Expand Down Expand Up @@ -316,6 +319,9 @@ check-before = true
# will only run these scripts before and after a backup
run-before = [ "echo Starting!", "ls -al ./src" ]
run-after = "echo All Done!"
# if scheduled, will run every 30 minutes
schedule = "*:0,30"
schedule-permission = "user"

# retention policy for profile src
[src.retention]
Expand All @@ -325,6 +331,11 @@ keep-within = "30d"
compact = false
prune = true

[src.check]
read-data = true
# if scheduled, will check the repository the first day of each month at 3am
schedule = "*-*-01 03:00"

```

And another simple example for Windows:
Expand Down Expand Up @@ -487,6 +498,7 @@ Usage of resticprofile:
resticprofile flags:
-c, --config string configuration file (default "profiles")
--dry-run display the restic commands instead of running them
-f, --format string file format of the configuration (default is to use the file extension)
-h, --help display this help
-l, --log string logs into a file instead of the console
Expand All @@ -507,9 +519,10 @@ resticprofile own commands:
```

A command is a restic command **except** for one command recognized by resticprofile only: `profiles`
A command is either a restic command or a resticprofile own command.


## Command line reference ##
Expand All @@ -520,6 +533,7 @@ There are not many options on the command line, most of the options are in the c
* **[-c | --config] configuration_file**: Specify a configuration file other than the default
* **[-f | --format] configuration_format**: Specify the configuration file format: `toml`, `yaml`, `json` or `hcl`
* **[-n | --name] profile_name**: Profile section to use from the configuration file
* **[--dry-run]**: Doesn't run the restic command but display the command line instead
* **[-q | --quiet]**: Force resticprofile and restic to be quiet (override any configuration from the profile)
* **[-v | --verbose]**: Force resticprofile and restic to be verbose (override any configuration from the profile)
* **[--no-ansi]**: Disable console colouring (to save output into a log file)
Expand Down Expand Up @@ -857,7 +871,7 @@ As an example, here's a similar configuration file in YAML:

```yaml
global:
default-command: version
default-command: snapshots
initialize: false
priority: low

Expand All @@ -869,14 +883,12 @@ groups:
default:
env:
tmp: /tmp
initialize: false
password-file: key
repository: /backup

documents:
backup:
source: ~/Documents
initialize: false
repository: ~/backup
snapshots:
tag:
Expand All @@ -890,7 +902,7 @@ root:
- excludes
one-file-system: false
source:
- .
- /
tag:
- test
- dev
Expand Down Expand Up @@ -918,7 +930,6 @@ root:
self:
backup:
source: ./
initialize: false
repository: ../backup
snapshots:
tag:
Expand Down
16 changes: 8 additions & 8 deletions commands.go
Expand Up @@ -43,14 +43,6 @@ var (
description: "show all the details of the current profile",
action: showProfile,
needConfiguration: true,
hide: false,
},
{
name: "panic",
description: "(debug only) simulates a panic",
action: panicCommand,
needConfiguration: false,
hide: true,
},
{
name: "schedule",
Expand All @@ -73,13 +65,21 @@ var (
needConfiguration: true,
hide: false,
},
// hidden commands
{
name: "elevation",
description: "test windows elevated mode",
action: testElevationCommand,
needConfiguration: true,
hide: true,
},
{
name: "panic",
description: "(debug only) simulates a panic",
action: panicCommand,
needConfiguration: false,
hide: true,
},
}
)

Expand Down
7 changes: 4 additions & 3 deletions examples/linux.yaml
Expand Up @@ -10,11 +10,12 @@ default:
documents:
inherit: default
initialize: true
schedule:
- "*:00,30" # every 15 minutes
- "*:15,45" # both combined together
backup:
tag: documents
source: ~/Documents
schedule:
- "*:00,30" # every 15 minutes
- "*:15,45" # both combined together
snapshots:
tag: documents

Expand Down
2 changes: 2 additions & 0 deletions flags.go
Expand Up @@ -17,6 +17,7 @@ type commandLineFlags struct {
format string
name string
logFile string
dryRun bool
noAnsi bool
theme string
resticArgs []string
Expand Down Expand Up @@ -49,6 +50,7 @@ func loadFlags() (*pflag.FlagSet, commandLineFlags) {
flagset.StringVarP(&flags.format, "format", "f", "", "file format of the configuration (default is to use the file extension)")
flagset.StringVarP(&flags.name, "name", "n", constants.DefaultProfileName, "profile name")
flagset.StringVarP(&flags.logFile, "log", "l", "", "logs into a file instead of the console")
flagset.BoolVar(&flags.dryRun, "dry-run", false, "display the restic commands instead of running them")

flagset.BoolVar(&flags.noAnsi, "no-ansi", false, "disable ansi control characters (disable console colouring)")
flagset.StringVar(&flags.theme, "theme", constants.DefaultTheme, "console colouring theme (dark, light, none)")
Expand Down
1 change: 1 addition & 0 deletions main.go
Expand Up @@ -302,6 +302,7 @@ func runProfile(
wrapper := newResticWrapper(
resticBinary,
global.Initialize || profile.Initialize,
flags.dryRun,
profile,
resticCommand,
resticArguments,
Expand Down
12 changes: 11 additions & 1 deletion shell_command.go
Expand Up @@ -3,7 +3,9 @@ package main
import (
"io"
"os"
"strings"

"github.com/creativeprojects/clog"
"github.com/creativeprojects/resticprofile/shell"
)

Expand All @@ -14,11 +16,12 @@ type shellCommandDefinition struct {
displayStderr bool
useStdin bool
stdout io.Writer
dryRun bool
sigChan chan os.Signal
}

// newShellCommand creates a new shell command definition
func newShellCommand(command string, args, env []string) shellCommandDefinition {
func newShellCommand(command string, args, env []string, dryRun bool, sigChan chan os.Signal) shellCommandDefinition {
if env == nil {
env = make([]string, 0)
}
Expand All @@ -29,13 +32,20 @@ func newShellCommand(command string, args, env []string) shellCommandDefinition
displayStderr: true,
useStdin: false,
stdout: os.Stdout,
dryRun: dryRun,
sigChan: sigChan,
}
}

// runShellCommand instantiates a shell.Command and sends the information to run the shell command
func runShellCommand(command shellCommandDefinition) error {
var err error

if command.dryRun {
clog.Infof("dry-run: %s %s", command.command, strings.Join(command.args, " "))
return nil
}

shellCmd := shell.NewSignalledCommand(command.command, command.args, command.sigChan)

shellCmd.Stdout = command.stdout
Expand Down
16 changes: 16 additions & 0 deletions term/term_test.go
Expand Up @@ -2,6 +2,7 @@ package term

import (
"bytes"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -67,3 +68,18 @@ func TestAskYesNo(t *testing.T) {
assert.Equalf(t, testItem.expected, result, "when input was %q", testItem.input)
}
}

func ExamplePrint() {
SetOutput(os.Stdout)
Print("ExamplePrint")
// Output: ExamplePrint
}

func TestCanRedirectTermOutput(t *testing.T) {
message := "TestCanRedirectTermOutput"
buffer := &bytes.Buffer{}
SetOutput(buffer)
_, err := Print(message)
assert.NoError(t, err)
assert.Equal(t, message, buffer.String())
}

0 comments on commit 67e24a4

Please sign in to comment.