2017-03-08 20:19:12 +01:00
package main
import (
"context"
2024-03-18 13:20:45 -07:00
"os"
2017-03-08 20:19:12 +01:00
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
)
2023-02-17 16:13:46 +01:00
// initMultiSnapshotFilter 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
2023-02-17 16:13:46 +01:00
func initMultiSnapshotFilter ( flags * pflag . FlagSet , filt * restic . SnapshotFilter , addHostShorthand bool ) {
2022-09-03 00:19:19 +02:00
hostShorthand := "H"
if ! addHostShorthand {
hostShorthand = ""
}
2024-03-18 13:20:45 -07:00
flags . StringArrayVarP ( & filt . Hosts , "host" , hostShorthand , nil , "only consider snapshots for this `host` (can be specified multiple times) (default: $RESTIC_HOST)" )
2023-02-17 16:13:46 +01:00
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)" )
2024-03-18 13:20:45 -07:00
2024-04-24 21:49:34 +02:00
// set default based on env if set
if host := os . Getenv ( "RESTIC_HOST" ) ; host != "" {
filt . Hosts = [ ] string { host }
2024-03-18 13:20:45 -07:00
}
2022-09-03 00:19:19 +02:00
}
2023-02-17 16:13:46 +01:00
// initSingleSnapshotFilter is used for commands that work on a single snapshot
2022-10-03 14:50:21 +02:00
// MUST be combined with restic.FindFilteredSnapshot
2023-02-17 16:13:46 +01:00
func initSingleSnapshotFilter ( flags * pflag . FlagSet , filt * restic . SnapshotFilter ) {
2024-03-18 13:20:45 -07:00
flags . StringArrayVarP ( & filt . Hosts , "host" , "H" , nil , "only consider snapshots for this `host`, when snapshot ID \"latest\" is given (can be specified multiple times) (default: $RESTIC_HOST)" )
2023-02-17 16:13:46 +01:00
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)" )
2024-03-18 13:20:45 -07:00
2024-04-24 21:49:34 +02:00
// set default based on env if set
if host := os . Getenv ( "RESTIC_HOST" ) ; host != "" {
filt . Hosts = [ ] string { host }
2024-03-18 13:20:45 -07:00
}
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.
2023-10-01 13:05:56 +02:00
func FindFilteredSnapshots ( ctx context . Context , be restic . Lister , loader restic . LoaderUnpacked , f * restic . SnapshotFilter , snapshotIDs [ ] string ) <- chan * restic . Snapshot {
2017-03-08 20:19:12 +01:00
out := make ( chan * restic . Snapshot )
go func ( ) {
defer close ( out )
2023-10-01 13:05:56 +02:00
be , err := restic . MemorizeList ( ctx , be , restic . SnapshotFile )
2022-10-03 13:51:41 +02:00
if err != nil {
Warnf ( "could not load snapshots: %v\n" , err )
return
}
2017-03-08 20:19:12 +01:00
2023-02-17 16:13:46 +01:00
err = f . FindAll ( ctx , be , loader , snapshotIDs , func ( id string , sn * restic . Snapshot , err error ) error {
2022-10-03 13:51:41 +02:00
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
}