|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/github/git-bundle-server/cmd/utils" |
| 8 | + "github.com/github/git-bundle-server/internal/argparse" |
| 9 | + "github.com/github/git-bundle-server/internal/core" |
| 10 | + "github.com/github/git-bundle-server/internal/log" |
| 11 | + typeutils "github.com/github/git-bundle-server/internal/utils" |
| 12 | +) |
| 13 | + |
| 14 | +type repairCmd struct { |
| 15 | + logger log.TraceLogger |
| 16 | + container *utils.DependencyContainer |
| 17 | +} |
| 18 | + |
| 19 | +func NewRepairCommand(logger log.TraceLogger, container *utils.DependencyContainer) argparse.Subcommand { |
| 20 | + return &repairCmd{ |
| 21 | + logger: logger, |
| 22 | + container: container, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func (repairCmd) Name() string { |
| 27 | + return "repair" |
| 28 | +} |
| 29 | + |
| 30 | +func (repairCmd) Description() string { |
| 31 | + return ` |
| 32 | +Scan and correct inconsistencies in the bundle server's internal registries and |
| 33 | +storage.` |
| 34 | +} |
| 35 | + |
| 36 | +func (r *repairCmd) repairRoutes(ctx context.Context, args []string) error { |
| 37 | + parser := argparse.NewArgParser(r.logger, "git-bundle-server repair routes [--start-all] [--dry-run]") |
| 38 | + enable := parser.Bool("start-all", false, "turn on bundle computation for all repositories found") |
| 39 | + dryRun := parser.Bool("dry-run", false, "report the repairs needed, but do not perform them") |
| 40 | + // TODO: add a '--cleanup' option to delete non-repo contents inside repo root |
| 41 | + parser.Parse(ctx, args) |
| 42 | + |
| 43 | + repoProvider := utils.GetDependency[core.RepositoryProvider](ctx, r.container) |
| 44 | + |
| 45 | + // Read the routes file |
| 46 | + repos, err := repoProvider.GetRepositories(ctx) |
| 47 | + if err != nil { |
| 48 | + // If routes file cannot be read, start over |
| 49 | + fmt.Println("warning: cannot load routes file; rebuilding from scratch...") |
| 50 | + repos = make(map[string]core.Repository) |
| 51 | + } |
| 52 | + |
| 53 | + // Read the repositories as represented by internal storage |
| 54 | + storedRepos, err := repoProvider.ReadRepositoryStorage(ctx) |
| 55 | + if err != nil { |
| 56 | + return r.logger.Errorf(ctx, "could not read internal repository storage: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + _, missingOnDisk, notRegistered := typeutils.SegmentKeys(repos, storedRepos) |
| 60 | + |
| 61 | + // Print the updates to be made |
| 62 | + fmt.Print("\n") |
| 63 | + |
| 64 | + if *enable && len(notRegistered) > 0 { |
| 65 | + fmt.Println("Unregistered routes to add") |
| 66 | + fmt.Println("--------------------------") |
| 67 | + for _, route := range notRegistered { |
| 68 | + fmt.Printf("* %s\n", route) |
| 69 | + repos[route] = storedRepos[route] |
| 70 | + } |
| 71 | + fmt.Print("\n") |
| 72 | + } |
| 73 | + |
| 74 | + if len(missingOnDisk) > 0 { |
| 75 | + fmt.Println("Missing or invalid routes to remove") |
| 76 | + fmt.Println("-----------------------------------") |
| 77 | + for _, route := range missingOnDisk { |
| 78 | + fmt.Printf("* %s\n", route) |
| 79 | + delete(repos, route) |
| 80 | + } |
| 81 | + fmt.Print("\n") |
| 82 | + } |
| 83 | + |
| 84 | + if (!*enable || len(notRegistered) == 0) && len(missingOnDisk) == 0 { |
| 85 | + fmt.Println("No repairs needed.") |
| 86 | + return nil |
| 87 | + } |
| 88 | + |
| 89 | + if *dryRun { |
| 90 | + fmt.Println("Skipping updates (dry run)") |
| 91 | + } else { |
| 92 | + fmt.Println("Applying route repairs...") |
| 93 | + err := repoProvider.WriteAllRoutes(ctx, repos) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + // Start the global cron schedule (if it's not already running) |
| 99 | + cron := utils.GetDependency[utils.CronHelper](ctx, r.container) |
| 100 | + cron.SetCronSchedule(ctx) |
| 101 | + fmt.Println("Done") |
| 102 | + } |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +func (r *repairCmd) Run(ctx context.Context, args []string) error { |
| 108 | + parser := argparse.NewArgParser(r.logger, "git-bundle-server repair <subcommand> [<options>]") |
| 109 | + parser.Subcommand(argparse.NewSubcommand("routes", "Correct the contents of the internal route registry", r.repairRoutes)) |
| 110 | + parser.Parse(ctx, args) |
| 111 | + |
| 112 | + return parser.InvokeSubcommand(ctx) |
| 113 | +} |
0 commit comments