mirror of
https://github.com/octoleo/restic.git
synced 2025-01-26 16:48:29 +00:00
733519d895
This commit is a followup to the addition of the --group-by flag for the snapshots command. Adding the grouping code there introduced duplicated code (the forget command also does grouping). This commit refactors boths sides to only use shared code.
77 lines
1.7 KiB
Go
77 lines
1.7 KiB
Go
package restic
|
|
|
|
import (
|
|
"encoding/json"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
)
|
|
|
|
// SnapshotGroupKey is the structure for identifying groups in a grouped
|
|
// snapshot list. This is used by GroupSnapshots()
|
|
type SnapshotGroupKey struct {
|
|
Hostname string `json:"hostname"`
|
|
Paths []string `json:"paths"`
|
|
Tags []string `json:"tags"`
|
|
}
|
|
|
|
// GroupSnapshots takes a list of snapshots and a grouping criteria and creates
|
|
// a group list of snapshots.
|
|
func GroupSnapshots(snapshots Snapshots, options string) (map[string]Snapshots, bool, error) {
|
|
// group by hostname and dirs
|
|
snapshotGroups := make(map[string]Snapshots)
|
|
|
|
var GroupByTag bool
|
|
var GroupByHost bool
|
|
var GroupByPath bool
|
|
var GroupOptionList []string
|
|
|
|
GroupOptionList = strings.Split(options, ",")
|
|
|
|
for _, option := range GroupOptionList {
|
|
switch option {
|
|
case "host":
|
|
GroupByHost = true
|
|
case "paths":
|
|
GroupByPath = true
|
|
case "tags":
|
|
GroupByTag = true
|
|
case "":
|
|
default:
|
|
return nil, false, errors.Fatal("unknown grouping option: '" + option + "'")
|
|
}
|
|
}
|
|
|
|
for _, sn := range snapshots {
|
|
// Determining grouping-keys
|
|
var tags []string
|
|
var hostname string
|
|
var paths []string
|
|
|
|
if GroupByTag {
|
|
tags = sn.Tags
|
|
sort.StringSlice(tags).Sort()
|
|
}
|
|
if GroupByHost {
|
|
hostname = sn.Hostname
|
|
}
|
|
if GroupByPath {
|
|
paths = sn.Paths
|
|
}
|
|
|
|
sort.StringSlice(sn.Paths).Sort()
|
|
var k []byte
|
|
var err error
|
|
|
|
k, err = json.Marshal(SnapshotGroupKey{Tags: tags, Hostname: hostname, Paths: paths})
|
|
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
snapshotGroups[string(k)] = append(snapshotGroups[string(k)], sn)
|
|
}
|
|
|
|
return snapshotGroups, GroupByTag || GroupByHost || GroupByPath, nil
|
|
}
|