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