1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2025-01-03 05:35:43 +00:00
restic/cmd/restic/cmd_repair_index.go

81 lines
2.1 KiB
Go
Raw Normal View History

2015-10-25 16:24:52 +00:00
package main
2016-09-17 10:36:05 +00:00
import (
"context"
2020-10-10 19:51:11 +00:00
"github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/ui/termstatus"
2016-09-17 10:36:05 +00:00
"github.com/spf13/cobra"
"github.com/spf13/pflag"
2016-09-17 10:36:05 +00:00
)
2015-10-25 16:24:52 +00:00
var cmdRepairIndex = &cobra.Command{
Use: "index [flags]",
2020-10-10 19:51:11 +00:00
Short: "Build a new index",
2016-09-17 10:36:05 +00:00
Long: `
The "repair index" command creates a new index based on the pack files in the
repository.
EXIT STATUS
===========
Exit status is 0 if the command was successful.
Exit status is 1 if there was any error.
Exit status is 10 if the repository does not exist.
Exit status is 11 if the repository is already locked.
Exit status is 12 if the password is incorrect.
2016-09-17 10:36:05 +00:00
`,
DisableAutoGenTag: true,
2024-02-10 21:58:10 +00:00
RunE: func(cmd *cobra.Command, _ []string) error {
term, cancel := setupTermstatus()
defer cancel()
return runRebuildIndex(cmd.Context(), repairIndexOptions, globalOptions, term)
2016-09-17 10:36:05 +00:00
},
2015-10-25 16:24:52 +00:00
}
var cmdRebuildIndex = &cobra.Command{
Use: "rebuild-index [flags]",
Short: cmdRepairIndex.Short,
Long: cmdRepairIndex.Long,
Deprecated: `Use "repair index" instead`,
DisableAutoGenTag: true,
RunE: cmdRepairIndex.RunE,
}
// RepairIndexOptions collects all options for the repair index command.
type RepairIndexOptions struct {
2020-10-10 19:51:11 +00:00
ReadAllPacks bool
}
var repairIndexOptions RepairIndexOptions
2020-10-10 19:51:11 +00:00
2015-10-25 16:24:52 +00:00
func init() {
cmdRepair.AddCommand(cmdRepairIndex)
// add alias for old name
2016-09-17 10:36:05 +00:00
cmdRoot.AddCommand(cmdRebuildIndex)
2020-10-10 19:51:11 +00:00
for _, f := range []*pflag.FlagSet{cmdRepairIndex.Flags(), cmdRebuildIndex.Flags()} {
f.BoolVar(&repairIndexOptions.ReadAllPacks, "read-all-packs", false, "read all pack files to generate new index from scratch")
}
2015-10-25 16:24:52 +00:00
}
func runRebuildIndex(ctx context.Context, opts RepairIndexOptions, gopts GlobalOptions, term *termstatus.Terminal) error {
ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, false)
2015-10-25 16:24:52 +00:00
if err != nil {
return err
}
defer unlock()
2015-10-25 16:24:52 +00:00
printer := newTerminalProgressPrinter(gopts.verbosity, term)
err = repository.RepairIndex(ctx, repo, repository.RepairIndexOptions{
ReadAllPacks: opts.ReadAllPacks,
}, printer)
2020-12-05 15:10:18 +00:00
if err != nil {
return err
}
printer.P("done\n")
return nil
2015-10-25 16:24:52 +00:00
}