Skip to content

Commit fd23c3a

Browse files
committed
Added restic cmds that filter by host, tag & path
1 parent ce27e1f commit fd23c3a

File tree

3 files changed

+170
-62
lines changed

3 files changed

+170
-62
lines changed

config/profile.go

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ type Profile struct {
4545
Forget *OtherSectionWithSchedule `mapstructure:"forget"`
4646
Mount map[string]interface{} `mapstructure:"mount"`
4747
Copy *CopySection `mapstructure:"copy"`
48+
Dump map[string]interface{} `mapstructure:"dump"`
49+
Find map[string]interface{} `mapstructure:"find"`
50+
Ls map[string]interface{} `mapstructure:"ls"`
51+
Restore map[string]interface{} `mapstructure:"restore"`
52+
Stats map[string]interface{} `mapstructure:"stats"`
53+
Tag map[string]interface{} `mapstructure:"tag"`
4854
}
4955

5056
// BackupSection contains the specific configuration to the 'backup' command
@@ -156,25 +162,62 @@ func (p *Profile) SetRootPath(rootPath string) {
156162
p.Copy.PasswordFile = fixPath(p.Copy.PasswordFile, expandEnv, absolutePrefix(rootPath))
157163
p.Copy.RepositoryFile = fixPath(p.Copy.RepositoryFile, expandEnv, absolutePrefix(rootPath))
158164
}
165+
166+
// Handle dynamic flags dealing with paths
167+
pathFlags := []string{
168+
"cacert",
169+
"tls-client-cert",
170+
"cache-dir",
171+
"repository-file",
172+
"password-file",
173+
}
174+
for _, section := range p.allFlagsSections() {
175+
if section == nil {
176+
continue
177+
}
178+
for _, flag := range pathFlags {
179+
if paths, ok := stringifyValueOf(section[flag]); ok && len(paths) > 0 {
180+
for i, path := range paths {
181+
if len(path) > 0 {
182+
paths[i] = fixPath(path, expandEnv, absolutePrefix(rootPath))
183+
}
184+
}
185+
section[flag] = paths
186+
}
187+
}
188+
}
159189
}
160190

161191
// SetHost will replace any host value from a boolean to the hostname
162192
func (p *Profile) SetHost(hostname string) {
163-
if p.Backup != nil && p.Backup.OtherFlags != nil {
164-
replaceTrueValue(p.Backup.OtherFlags, constants.ParameterHost, hostname)
193+
for _, section := range p.allFlagsSections() {
194+
if section != nil {
195+
replaceTrueValue(section, constants.ParameterHost, hostname)
196+
}
165197
}
166-
if p.Retention != nil && p.Retention.OtherFlags != nil {
167-
replaceTrueValue(p.Retention.OtherFlags, constants.ParameterHost, hostname)
198+
}
199+
200+
func (p *Profile) allFlagsSections() (sections []map[string]interface{}) {
201+
sections = append(sections,
202+
p.Dump,
203+
p.Find,
204+
p.Ls,
205+
p.Mount,
206+
p.Restore,
207+
p.Snapshots,
208+
p.Stats,
209+
p.Tag,
210+
)
211+
if p.Backup != nil {
212+
sections = append(sections, p.Backup.OtherFlags)
168213
}
169-
if p.Snapshots != nil {
170-
replaceTrueValue(p.Snapshots, constants.ParameterHost, hostname)
214+
if p.Retention != nil {
215+
sections = append(sections, p.Retention.OtherFlags)
171216
}
172217
if p.Forget != nil {
173-
replaceTrueValue(p.Forget.OtherFlags, constants.ParameterHost, hostname)
174-
}
175-
if p.Mount != nil {
176-
replaceTrueValue(p.Mount, constants.ParameterHost, hostname)
218+
sections = append(sections, p.Forget.OtherFlags)
177219
}
220+
return
178221
}
179222

180223
// GetCommonFlags returns the flags common to all commands
@@ -201,17 +244,15 @@ func (p *Profile) GetCommandFlags(command string) *shell.Args {
201244
flags = addOtherArgs(flags, p.Backup.OtherFlags)
202245

203246
case constants.CommandSnapshots:
204-
if p.Snapshots != nil {
205-
flags = addOtherArgs(flags, p.Snapshots)
206-
}
247+
flags = addOtherArgs(flags, p.Snapshots)
207248

208249
case constants.CommandCheck:
209-
if p.Check != nil && p.Check.OtherFlags != nil {
250+
if p.Check != nil {
210251
flags = addOtherArgs(flags, p.Check.OtherFlags)
211252
}
212253

213254
case constants.CommandPrune:
214-
if p.Prune != nil && p.Prune.OtherFlags != nil {
255+
if p.Prune != nil {
215256
flags = addOtherArgs(flags, p.Prune.OtherFlags)
216257
}
217258

@@ -221,17 +262,31 @@ func (p *Profile) GetCommandFlags(command string) *shell.Args {
221262
}
222263

223264
case constants.CommandMount:
224-
if p.Mount != nil {
225-
flags = addOtherArgs(flags, p.Mount)
226-
}
265+
flags = addOtherArgs(flags, p.Mount)
227266

228267
case constants.CommandCopy:
229268
if p.Copy != nil {
230269
flags = convertStructToArgs(*p.Copy, flags)
231-
if p.Copy.OtherFlags != nil {
232-
flags = addOtherArgs(flags, p.Copy.OtherFlags)
233-
}
270+
flags = addOtherArgs(flags, p.Copy.OtherFlags)
234271
}
272+
273+
case constants.CommandDump:
274+
flags = addOtherArgs(flags, p.Dump)
275+
276+
case constants.CommandFind:
277+
flags = addOtherArgs(flags, p.Find)
278+
279+
case constants.CommandLs:
280+
flags = addOtherArgs(flags, p.Ls)
281+
282+
case constants.CommandRestore:
283+
flags = addOtherArgs(flags, p.Restore)
284+
285+
case constants.CommandStats:
286+
flags = addOtherArgs(flags, p.Stats)
287+
288+
case constants.CommandTag:
289+
flags = addOtherArgs(flags, p.Tag)
235290
}
236291

237292
return flags

config/profile_test.go

Lines changed: 88 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ exclude = "exclude"
225225
iexclude = "iexclude"
226226
[profile.copy]
227227
password-file = "key"
228+
[profile.dump]
229+
password-file = "key"
228230
`
229231
profile, err := getProfile("toml", testConfig, "profile", "")
230232
if err != nil {
@@ -243,6 +245,7 @@ password-file = "key"
243245
assert.ElementsMatch(t, []string{"exclude"}, profile.Backup.Exclude)
244246
assert.ElementsMatch(t, []string{"iexclude"}, profile.Backup.Iexclude)
245247
assert.Equal(t, "/wd/key", profile.Copy.PasswordFile)
248+
assert.Equal(t, []string{"/wd/key"}, profile.Dump["password-file"])
246249
}
247250

248251
func TestHostInProfile(t *testing.T) {
@@ -284,6 +287,10 @@ func TestHostInAllSupportedSections(t *testing.T) {
284287
constants.CommandSnapshots,
285288
constants.CommandMount,
286289
constants.SectionConfigurationRetention,
290+
constants.CommandFind,
291+
constants.CommandDump,
292+
constants.CommandLs,
293+
constants.CommandStats,
287294
}
288295

289296
assertHostIs := func(expectedHost []string, profile *Profile, section string) {
@@ -712,6 +719,18 @@ other-flag-prune = true
712719
other-flag-mount = true
713720
[profile.copy]
714721
other-flag-copy = true
722+
[profile.dump]
723+
other-flag-dump = true
724+
[profile.find]
725+
other-flag-find = true
726+
[profile.ls]
727+
other-flag-ls = true
728+
[profile.restore]
729+
other-flag-restore = true
730+
[profile.stats]
731+
other-flag-stats = true
732+
[profile.tag]
733+
other-flag-tag = true
715734
`},
716735
{"json", `
717736
{
@@ -724,7 +743,13 @@ other-flag-copy = true
724743
"forget": {"other-flag-forget": true},
725744
"prune": {"other-flag-prune": true},
726745
"mount": {"other-flag-mount": true},
727-
"copy": {"other-flag-copy": true}
746+
"copy": {"other-flag-copy": true},
747+
"dump": {"other-flag-dump": true},
748+
"find": {"other-flag-find": true},
749+
"ls": {"other-flag-ls": true},
750+
"restore": {"other-flag-restore": true},
751+
"stats": {"other-flag-stats": true},
752+
"tag": {"other-flag-tag": true}
728753
}
729754
}`},
730755
{"yaml", `---
@@ -746,6 +771,18 @@ profile:
746771
other-flag-mount: true
747772
copy:
748773
other-flag-copy: true
774+
dump:
775+
other-flag-dump: true
776+
find:
777+
other-flag-find: true
778+
ls:
779+
other-flag-ls: true
780+
restore:
781+
other-flag-restore: true
782+
stats:
783+
other-flag-stats: true
784+
tag:
785+
other-flag-tag: true
749786
`},
750787
{"hcl", `
751788
"profile" = {
@@ -774,6 +811,24 @@ profile:
774811
copy = {
775812
other-flag-copy = true
776813
}
814+
dump = {
815+
other-flag-dump = true
816+
}
817+
find = {
818+
other-flag-find = true
819+
}
820+
ls = {
821+
other-flag-ls = true
822+
}
823+
restore = {
824+
other-flag-restore = true
825+
}
826+
stats = {
827+
other-flag-stats = true
828+
}
829+
tag = {
830+
other-flag-tag = true
831+
}
777832
}
778833
`},
779834
}
@@ -794,57 +849,49 @@ profile:
794849
require.NotNil(t, profile.Prune)
795850
require.NotNil(t, profile.Snapshots)
796851
require.NotNil(t, profile.Copy)
852+
require.NotNil(t, profile.Dump)
853+
require.NotNil(t, profile.Find)
854+
require.NotNil(t, profile.Ls)
855+
require.NotNil(t, profile.Restore)
856+
require.NotNil(t, profile.Stats)
857+
require.NotNil(t, profile.Tag)
797858

798859
flags := profile.GetCommonFlags()
799860
assert.Equal(t, 1, len(flags.ToMap()))
800861
assert.ElementsMatch(t, []string{"1"}, flags.ToMap()["other-flag"])
801862

802-
flags = profile.GetCommandFlags("backup")
803-
assert.Equal(t, 2, len(flags.ToMap()))
804-
assert.ElementsMatch(t, []string{"1"}, flags.ToMap()["other-flag"])
805-
assert.ElementsMatch(t, []string{"backup"}, flags.ToMap()["other-flag-backup"])
806-
807863
flags = profile.GetRetentionFlags()
808864
assert.Equal(t, 2, len(flags.ToMap()))
809865
assert.ElementsMatch(t, []string{"1"}, flags.ToMap()["other-flag"])
810866
_, found := flags.ToMap()["other-flag-retention"]
811867
assert.True(t, found)
812868

813-
flags = profile.GetCommandFlags("snapshots")
814-
assert.Equal(t, 2, len(flags.ToMap()))
815-
assert.ElementsMatch(t, []string{"1"}, flags.ToMap()["other-flag"])
816-
_, found = flags.ToMap()["other-flag-snapshots"]
817-
assert.True(t, found)
818-
819-
flags = profile.GetCommandFlags("check")
820-
assert.Equal(t, 2, len(flags.ToMap()))
821-
assert.ElementsMatch(t, []string{"1"}, flags.ToMap()["other-flag"])
822-
_, found = flags.ToMap()["other-flag-check"]
823-
assert.True(t, found)
824-
825-
flags = profile.GetCommandFlags("forget")
826-
assert.Equal(t, 2, len(flags.ToMap()))
827-
assert.ElementsMatch(t, []string{"1"}, flags.ToMap()["other-flag"])
828-
_, found = flags.ToMap()["other-flag-forget"]
829-
assert.True(t, found)
830-
831-
flags = profile.GetCommandFlags("prune")
832-
assert.Equal(t, 2, len(flags.ToMap()))
833-
assert.ElementsMatch(t, []string{"1"}, flags.ToMap()["other-flag"])
834-
_, found = flags.ToMap()["other-flag-prune"]
835-
assert.True(t, found)
836-
837-
flags = profile.GetCommandFlags("mount")
838-
assert.Equal(t, 2, len(flags.ToMap()))
839-
assert.ElementsMatch(t, []string{"1"}, flags.ToMap()["other-flag"])
840-
_, found = flags.ToMap()["other-flag-mount"]
841-
assert.True(t, found)
842-
843-
flags = profile.GetCommandFlags("copy")
844-
assert.Equal(t, 2, len(flags.ToMap()))
845-
assert.ElementsMatch(t, []string{"1"}, flags.ToMap()["other-flag"])
846-
_, found = flags.ToMap()["other-flag-copy"]
847-
assert.True(t, found)
869+
commands := []string{
870+
"backup",
871+
"check",
872+
"copy",
873+
"dump",
874+
"find",
875+
"forget",
876+
"ls",
877+
"prune",
878+
"mount",
879+
"restore",
880+
"snapshots",
881+
"stats",
882+
"tag",
883+
}
884+
885+
for _, command := range commands {
886+
t.Run(command, func(t *testing.T) {
887+
flags = profile.GetCommandFlags(command)
888+
commandFlagName := "other-flag-" + command
889+
assert.Equal(t, 2, len(flags.ToMap()))
890+
assert.ElementsMatch(t, []string{"1"}, flags.ToMap()["other-flag"])
891+
_, found = flags.ToMap()[commandFlagName]
892+
assert.True(t, found, commandFlagName)
893+
})
894+
}
848895
})
849896
}
850897
}

constants/command.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ const (
1111
CommandUnlock = "unlock"
1212
CommandMount = "mount"
1313
CommandCopy = "copy"
14+
CommandDump = "dump"
15+
CommandFind = "find"
16+
CommandLs = "ls"
17+
CommandRestore = "restore"
18+
CommandStats = "stats"
19+
CommandTag = "tag"
1420
)

0 commit comments

Comments
 (0)