2017-03-08 20:19:12 +01:00
package main
import (
"context"
2021-11-06 01:23:12 +01:00
"github.com/restic/restic/internal/backend"
2017-07-24 17:42:25 +02:00
"github.com/restic/restic/internal/restic"
2022-09-03 00:19:19 +02:00
"github.com/spf13/pflag"
2017-03-08 20:19:12 +01:00
)
2022-09-03 00:19:19 +02:00
type snapshotFilterOptions struct {
Hosts [ ] string
Tags restic . TagLists
Paths [ ] string
}
// initMultiSnapshotFilterOptions is used for commands that work on multiple snapshots
2022-10-03 14:50:21 +02:00
// MUST be combined with restic.FindFilteredSnapshots or FindFilteredSnapshots
2022-09-03 00:19:19 +02:00
func initMultiSnapshotFilterOptions ( flags * pflag . FlagSet , options * snapshotFilterOptions , addHostShorthand bool ) {
hostShorthand := "H"
if ! addHostShorthand {
hostShorthand = ""
}
flags . StringArrayVarP ( & options . Hosts , "host" , hostShorthand , nil , "only consider snapshots for this `host` (can be specified multiple times)" )
2022-09-10 23:34:46 +02:00
flags . Var ( & options . Tags , "tag" , "only consider snapshots including `tag[,tag,...]` (can be specified multiple times)" )
flags . StringArrayVar ( & options . Paths , "path" , nil , "only consider snapshots including this (absolute) `path` (can be specified multiple times)" )
2022-09-03 00:19:19 +02:00
}
// initSingleSnapshotFilterOptions is used for commands that work on a single snapshot
2022-10-03 14:50:21 +02:00
// MUST be combined with restic.FindFilteredSnapshot
2022-09-03 00:19:19 +02:00
func initSingleSnapshotFilterOptions ( flags * pflag . FlagSet , options * snapshotFilterOptions ) {
flags . StringArrayVarP ( & options . Hosts , "host" , "H" , nil , "only consider snapshots for this `host`, when snapshot ID \"latest\" is given (can be specified multiple times)" )
2022-09-10 23:34:46 +02:00
flags . Var ( & options . Tags , "tag" , "only consider snapshots including `tag[,tag,...]`, when snapshot ID \"latest\" is given (can be specified multiple times)" )
flags . StringArrayVar ( & options . Paths , "path" , nil , "only consider snapshots including this (absolute) `path`, when snapshot ID \"latest\" is given (can be specified multiple times)" )
2022-09-03 00:19:19 +02:00
}
2017-03-08 20:19:12 +01:00
// FindFilteredSnapshots yields Snapshots, either given explicitly by `snapshotIDs` or filtered from the list of all snapshots.
2022-06-12 14:38:19 +02:00
func FindFilteredSnapshots ( ctx context . Context , be restic . Lister , loader restic . LoaderUnpacked , hosts [ ] string , tags [ ] restic . TagList , paths [ ] string , snapshotIDs [ ] string ) <- chan * restic . Snapshot {
2017-03-08 20:19:12 +01:00
out := make ( chan * restic . Snapshot )
go func ( ) {
defer close ( out )
2022-10-03 13:51:41 +02:00
be , err := backend . MemorizeList ( ctx , be , restic . SnapshotFile )
if err != nil {
Warnf ( "could not load snapshots: %v\n" , err )
return
}
2017-03-08 20:19:12 +01:00
2022-10-03 13:51:41 +02:00
err = restic . FindFilteredSnapshots ( ctx , be , loader , hosts , tags , paths , snapshotIDs , func ( id string , sn * restic . Snapshot , err error ) error {
if err != nil {
Warnf ( "Ignoring %q: %v\n" , id , err )
} else {
2017-03-08 20:19:12 +01:00
select {
case <- ctx . Done ( ) :
2022-10-03 13:51:41 +02:00
return ctx . Err ( )
2017-03-08 20:19:12 +01:00
case out <- sn :
}
}
2022-10-03 13:51:41 +02:00
return nil
} )
2018-01-21 17:25:36 +01:00
if err != nil {
Warnf ( "could not load snapshots: %v\n" , err )
2017-03-08 20:19:12 +01:00
}
} ( )
return out
}