|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/spf13/cobra" |
| 7 | + |
| 8 | + "github.com/nexthink-oss/gitea-mirror/pkg/gitea" |
| 9 | + "github.com/nexthink-oss/gitea-mirror/pkg/github" |
| 10 | + "github.com/nexthink-oss/gitea-mirror/pkg/server" |
| 11 | + "github.com/nexthink-oss/gitea-mirror/pkg/util" |
| 12 | +) |
| 13 | + |
| 14 | +func cmdRecreate() *cobra.Command { |
| 15 | + cmd := &cobra.Command{ |
| 16 | + Use: "recreate [<repository> ...]", |
| 17 | + Short: "Recreate Gitea mirrors", |
| 18 | + RunE: RecreateMirrors, |
| 19 | + } |
| 20 | + |
| 21 | + return cmd |
| 22 | +} |
| 23 | + |
| 24 | +func RecreateMirrors(cmd *cobra.Command, args []string) (err error) { |
| 25 | + var ctx = cmd.Context() |
| 26 | + var source server.Server |
| 27 | + |
| 28 | + if config.Source.Token == "" { |
| 29 | + if err := util.PromptForToken("Source API token", &config.Source.Token); err != nil { |
| 30 | + return fmt.Errorf("Source API token: %w", err) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + if config.Target.Token == "" { |
| 35 | + if err := util.PromptForToken("Target API token", &config.Target.Token); err != nil { |
| 36 | + return fmt.Errorf("Target API token: %w", err) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + switch config.Source.Type { |
| 41 | + case "github": |
| 42 | + source = github.NewController(ctx, &config.Source) |
| 43 | + case "gitea": |
| 44 | + source, err = gitea.NewController(ctx, &config.Source) |
| 45 | + if err != nil { |
| 46 | + return fmt.Errorf("NewController(%s): %w", config.Source.Url, err) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + target, err := gitea.NewController(ctx, &config.Target) |
| 51 | + if err != nil { |
| 52 | + return fmt.Errorf("NewController(%s): %w", config.Target.Url, err) |
| 53 | + } |
| 54 | + |
| 55 | + for repo := range config.FilteredRepositories(args) { |
| 56 | + if err = target.DeleteMirror(&repo); err != nil { |
| 57 | + fmt.Println(repo.Failure(fmt.Errorf("deleting: %w", err))) |
| 58 | + continue |
| 59 | + } |
| 60 | + if _, err = target.CreateMirror(source, &repo); err != nil { |
| 61 | + fmt.Println(repo.Failure(fmt.Errorf("creating: %w", err))) |
| 62 | + } else { |
| 63 | + fmt.Println(repo.Success()) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return nil |
| 68 | +} |
0 commit comments