Skip to content

Commit

Permalink
Added missing cmds that filter by host, tag & path
Browse files Browse the repository at this point in the history
  • Loading branch information
jkellerer committed Feb 26, 2022
1 parent ce27e1f commit e73d7b5
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 61 deletions.
98 changes: 78 additions & 20 deletions config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ type Profile struct {
Forget *OtherSectionWithSchedule `mapstructure:"forget"`
Mount map[string]interface{} `mapstructure:"mount"`
Copy *CopySection `mapstructure:"copy"`
Dump map[string]interface{} `mapstructure:"dump"`
Find map[string]interface{} `mapstructure:"find"`
Ls map[string]interface{} `mapstructure:"ls"`
Restore map[string]interface{} `mapstructure:"restore"`
Stats map[string]interface{} `mapstructure:"stats"`
Tag map[string]interface{} `mapstructure:"tag"`
}

// BackupSection contains the specific configuration to the 'backup' command
Expand Down Expand Up @@ -156,25 +162,65 @@ func (p *Profile) SetRootPath(rootPath string) {
p.Copy.PasswordFile = fixPath(p.Copy.PasswordFile, expandEnv, absolutePrefix(rootPath))
p.Copy.RepositoryFile = fixPath(p.Copy.RepositoryFile, expandEnv, absolutePrefix(rootPath))
}

// Handle dynamic flags dealing with paths
pathFlags := []string{
"cacert",
"tls-client-cert",
"cache-dir",
"repository-file",
"password-file",
}
for _, section := range p.allFlagsSections() {
if section == nil {
continue
}
for _, flag := range pathFlags {
if paths, ok := stringifyValueOf(section[flag]); ok && len(paths) > 0 {
for i, path := range paths {
if len(path) > 0 {
paths[i] = fixPath(path, expandEnv, absolutePrefix(rootPath))
}
}
section[flag] = paths
}
}
}
}

// SetHost will replace any host value from a boolean to the hostname
func (p *Profile) SetHost(hostname string) {
if p.Backup != nil && p.Backup.OtherFlags != nil {
replaceTrueValue(p.Backup.OtherFlags, constants.ParameterHost, hostname)
for _, section := range p.allFlagsSections() {
if section != nil {
replaceTrueValue(section, constants.ParameterHost, hostname)
}
}
if p.Retention != nil && p.Retention.OtherFlags != nil {
replaceTrueValue(p.Retention.OtherFlags, constants.ParameterHost, hostname)
}

func (p *Profile) allFlagsSections() (sections []map[string]interface{}) {
sections = append(sections,
p.Dump,
p.Find,
p.Ls,
p.Mount,
p.Restore,
p.Snapshots,
p.Stats,
p.Tag,
)
if p.Backup != nil {
sections = append(sections, p.Backup.OtherFlags)
}
if p.Snapshots != nil {
replaceTrueValue(p.Snapshots, constants.ParameterHost, hostname)
if p.Copy != nil {
sections = append(sections, p.Copy.OtherFlags)
}
if p.Forget != nil {
replaceTrueValue(p.Forget.OtherFlags, constants.ParameterHost, hostname)
sections = append(sections, p.Forget.OtherFlags)
}
if p.Mount != nil {
replaceTrueValue(p.Mount, constants.ParameterHost, hostname)
if p.Retention != nil {
sections = append(sections, p.Retention.OtherFlags)
}
return
}

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

case constants.CommandSnapshots:
if p.Snapshots != nil {
flags = addOtherArgs(flags, p.Snapshots)
}
flags = addOtherArgs(flags, p.Snapshots)

case constants.CommandCheck:
if p.Check != nil && p.Check.OtherFlags != nil {
if p.Check != nil {
flags = addOtherArgs(flags, p.Check.OtherFlags)
}

case constants.CommandPrune:
if p.Prune != nil && p.Prune.OtherFlags != nil {
if p.Prune != nil {
flags = addOtherArgs(flags, p.Prune.OtherFlags)
}

Expand All @@ -221,17 +265,31 @@ func (p *Profile) GetCommandFlags(command string) *shell.Args {
}

case constants.CommandMount:
if p.Mount != nil {
flags = addOtherArgs(flags, p.Mount)
}
flags = addOtherArgs(flags, p.Mount)

case constants.CommandCopy:
if p.Copy != nil {
flags = convertStructToArgs(*p.Copy, flags)
if p.Copy.OtherFlags != nil {
flags = addOtherArgs(flags, p.Copy.OtherFlags)
}
flags = addOtherArgs(flags, p.Copy.OtherFlags)
}

case constants.CommandDump:
flags = addOtherArgs(flags, p.Dump)

case constants.CommandFind:
flags = addOtherArgs(flags, p.Find)

case constants.CommandLs:
flags = addOtherArgs(flags, p.Ls)

case constants.CommandRestore:
flags = addOtherArgs(flags, p.Restore)

case constants.CommandStats:
flags = addOtherArgs(flags, p.Stats)

case constants.CommandTag:
flags = addOtherArgs(flags, p.Tag)
}

return flags
Expand Down
132 changes: 91 additions & 41 deletions config/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ exclude = "exclude"
iexclude = "iexclude"
[profile.copy]
password-file = "key"
[profile.dump]
password-file = "key"
`
profile, err := getProfile("toml", testConfig, "profile", "")
if err != nil {
Expand All @@ -243,6 +245,7 @@ password-file = "key"
assert.ElementsMatch(t, []string{"exclude"}, profile.Backup.Exclude)
assert.ElementsMatch(t, []string{"iexclude"}, profile.Backup.Iexclude)
assert.Equal(t, "/wd/key", profile.Copy.PasswordFile)
assert.Equal(t, []string{"/wd/key"}, profile.Dump["password-file"])
}

func TestHostInProfile(t *testing.T) {
Expand Down Expand Up @@ -284,6 +287,13 @@ func TestHostInAllSupportedSections(t *testing.T) {
constants.CommandSnapshots,
constants.CommandMount,
constants.SectionConfigurationRetention,
constants.CommandCopy,
constants.CommandDump,
constants.CommandFind,
constants.CommandLs,
constants.CommandRestore,
constants.CommandStats,
constants.CommandTag,
}

assertHostIs := func(expectedHost []string, profile *Profile, section string) {
Expand Down Expand Up @@ -712,6 +722,18 @@ other-flag-prune = true
other-flag-mount = true
[profile.copy]
other-flag-copy = true
[profile.dump]
other-flag-dump = true
[profile.find]
other-flag-find = true
[profile.ls]
other-flag-ls = true
[profile.restore]
other-flag-restore = true
[profile.stats]
other-flag-stats = true
[profile.tag]
other-flag-tag = true
`},
{"json", `
{
Expand All @@ -724,7 +746,13 @@ other-flag-copy = true
"forget": {"other-flag-forget": true},
"prune": {"other-flag-prune": true},
"mount": {"other-flag-mount": true},
"copy": {"other-flag-copy": true}
"copy": {"other-flag-copy": true},
"dump": {"other-flag-dump": true},
"find": {"other-flag-find": true},
"ls": {"other-flag-ls": true},
"restore": {"other-flag-restore": true},
"stats": {"other-flag-stats": true},
"tag": {"other-flag-tag": true}
}
}`},
{"yaml", `---
Expand All @@ -746,6 +774,18 @@ profile:
other-flag-mount: true
copy:
other-flag-copy: true
dump:
other-flag-dump: true
find:
other-flag-find: true
ls:
other-flag-ls: true
restore:
other-flag-restore: true
stats:
other-flag-stats: true
tag:
other-flag-tag: true
`},
{"hcl", `
"profile" = {
Expand Down Expand Up @@ -774,6 +814,24 @@ profile:
copy = {
other-flag-copy = true
}
dump = {
other-flag-dump = true
}
find = {
other-flag-find = true
}
ls = {
other-flag-ls = true
}
restore = {
other-flag-restore = true
}
stats = {
other-flag-stats = true
}
tag = {
other-flag-tag = true
}
}
`},
}
Expand All @@ -794,57 +852,49 @@ profile:
require.NotNil(t, profile.Prune)
require.NotNil(t, profile.Snapshots)
require.NotNil(t, profile.Copy)
require.NotNil(t, profile.Dump)
require.NotNil(t, profile.Find)
require.NotNil(t, profile.Ls)
require.NotNil(t, profile.Restore)
require.NotNil(t, profile.Stats)
require.NotNil(t, profile.Tag)

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

flags = profile.GetCommandFlags("backup")
assert.Equal(t, 2, len(flags.ToMap()))
assert.ElementsMatch(t, []string{"1"}, flags.ToMap()["other-flag"])
assert.ElementsMatch(t, []string{"backup"}, flags.ToMap()["other-flag-backup"])

flags = profile.GetRetentionFlags()
assert.Equal(t, 2, len(flags.ToMap()))
assert.ElementsMatch(t, []string{"1"}, flags.ToMap()["other-flag"])
_, found := flags.ToMap()["other-flag-retention"]
assert.True(t, found)

flags = profile.GetCommandFlags("snapshots")
assert.Equal(t, 2, len(flags.ToMap()))
assert.ElementsMatch(t, []string{"1"}, flags.ToMap()["other-flag"])
_, found = flags.ToMap()["other-flag-snapshots"]
assert.True(t, found)

flags = profile.GetCommandFlags("check")
assert.Equal(t, 2, len(flags.ToMap()))
assert.ElementsMatch(t, []string{"1"}, flags.ToMap()["other-flag"])
_, found = flags.ToMap()["other-flag-check"]
assert.True(t, found)

flags = profile.GetCommandFlags("forget")
assert.Equal(t, 2, len(flags.ToMap()))
assert.ElementsMatch(t, []string{"1"}, flags.ToMap()["other-flag"])
_, found = flags.ToMap()["other-flag-forget"]
assert.True(t, found)

flags = profile.GetCommandFlags("prune")
assert.Equal(t, 2, len(flags.ToMap()))
assert.ElementsMatch(t, []string{"1"}, flags.ToMap()["other-flag"])
_, found = flags.ToMap()["other-flag-prune"]
assert.True(t, found)

flags = profile.GetCommandFlags("mount")
assert.Equal(t, 2, len(flags.ToMap()))
assert.ElementsMatch(t, []string{"1"}, flags.ToMap()["other-flag"])
_, found = flags.ToMap()["other-flag-mount"]
assert.True(t, found)

flags = profile.GetCommandFlags("copy")
assert.Equal(t, 2, len(flags.ToMap()))
assert.ElementsMatch(t, []string{"1"}, flags.ToMap()["other-flag"])
_, found = flags.ToMap()["other-flag-copy"]
assert.True(t, found)
commands := []string{
"backup",
"check",
"copy",
"dump",
"find",
"forget",
"ls",
"prune",
"mount",
"restore",
"snapshots",
"stats",
"tag",
}

for _, command := range commands {
t.Run(command, func(t *testing.T) {
flags = profile.GetCommandFlags(command)
commandFlagName := "other-flag-" + command
assert.Equal(t, 2, len(flags.ToMap()))
assert.ElementsMatch(t, []string{"1"}, flags.ToMap()["other-flag"])
_, found = flags.ToMap()[commandFlagName]
assert.True(t, found, commandFlagName)
})
}
})
}
}
6 changes: 6 additions & 0 deletions constants/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ const (
CommandUnlock = "unlock"
CommandMount = "mount"
CommandCopy = "copy"
CommandDump = "dump"
CommandFind = "find"
CommandLs = "ls"
CommandRestore = "restore"
CommandStats = "stats"
CommandTag = "tag"
)

0 comments on commit e73d7b5

Please sign in to comment.