Skip to content

Commit 3a3f852

Browse files
committed
feat: add recreate cmd and separate public to public-{source,target}
1 parent 57c46d6 commit 3a3f852

File tree

5 files changed

+145
-12
lines changed

5 files changed

+145
-12
lines changed

.goreleaser.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
version: 2
2+
before:
3+
hooks:
4+
- go mod tidy
5+
builds:
6+
- main: .
7+
binary: gitea-mirror
8+
env:
9+
- CGO_ENABLED=0
10+
mod_timestamp: "{{ .CommitTimestamp }}"
11+
flags:
12+
- -trimpath
13+
ldflags:
14+
- >-
15+
-s -w
16+
-X main.version={{.Version}}
17+
-X main.commit={{.ShortCommit}}
18+
-X main.date={{.Date}}
19+
goos:
20+
- windows
21+
- linux
22+
- darwin
23+
goarch:
24+
- amd64
25+
- arm64
26+
archives:
27+
- formats:
28+
- zip
29+
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
30+
snapshot:
31+
version_template: "{{ .Tag }}-next"
32+
checksum:
33+
name_template: "{{ .ProjectName }}_{{ .Version }}_SHA256SUMS"
34+
algorithm: sha256
35+
release:
36+
draft: false
37+
changelog:
38+
sort: asc
39+
filters:
40+
exclude:
41+
- "^docs:"
42+
- "^test:"
43+
brews:
44+
- repository:
45+
owner: isometry
46+
name: homebrew-tap
47+
token: "{{ .Env.HOMEBREW_TAP_GITHUB_TOKEN }}"
48+
directory: Formula
49+
description: Manage Gitea mirror lifecycle
50+
homepage: https://just.breathe.io/project/gitea-mirror/
51+
test: |
52+
system "#{bin}/gitea-mirror --help"
53+
install: |
54+
bin.install "gitea-mirror"

cmd/recreate.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
}

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func New() *cobra.Command {
3030
cmd.AddCommand(
3131
cmdConfig(),
3232
cmdCreate(),
33+
cmdRecreate(),
3334
cmdDelete(),
3435
cmdStatus(),
3536
cmdSync(),

pkg/config/config.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,18 @@ type Forge interface {
3333
}
3434

3535
type Defaults struct {
36-
Owner string `mapstructure:"owner"`
37-
Interval time.Duration `mapstructure:"interval"`
38-
Public bool `mapstructure:"public"`
36+
Owner string `mapstructure:"owner"`
37+
Interval time.Duration `mapstructure:"interval"`
38+
PublicSource bool `mapstructure:"public-source"`
39+
PublicTarget bool `mapstructure:"public-target"`
3940
}
4041

4142
type Repository struct {
42-
Owner string `mapstructure:"owner" yaml:"owner,omitempty"`
43-
Name string `mapstructure:"name"`
44-
Interval *time.Duration `mapstructure:"interval" yaml:"interval,omitempty"`
45-
Public *bool `mapstructure:"public" yaml:"public,omitempty"`
43+
Owner string `mapstructure:"owner" yaml:"owner,omitempty"`
44+
Name string `mapstructure:"name"`
45+
Interval *time.Duration `mapstructure:"interval" yaml:"interval,omitempty"`
46+
PublicSource *bool `mapstructure:"public-source" yaml:"public-source,omitempty"`
47+
PublicTarget *bool `mapstructure:"public-target" yaml:"public-target,omitempty"`
4648
}
4749

4850
type RepositorySet map[string]struct{}
@@ -88,8 +90,11 @@ func LoadConfig(names []string) (*Config, error) {
8890
if r.Interval == nil {
8991
config.Repositories[i].Interval = &config.Defaults.Interval
9092
}
91-
if r.Public == nil {
92-
config.Repositories[i].Public = &config.Defaults.Public
93+
if r.PublicSource == nil {
94+
config.Repositories[i].PublicSource = &config.Defaults.PublicSource
95+
}
96+
if r.PublicTarget == nil {
97+
config.Repositories[i].PublicTarget = &config.Defaults.PublicTarget
9398
}
9499
}
95100

pkg/gitea/mirror.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@ func (c *Controller) CreateMirror(source server.Server, r *config.Repository) (*
3030
return nil, err
3131
}
3232

33+
var authToken string
34+
if !*r.PublicSource {
35+
authToken = source.GetToken()
36+
}
37+
3338
options := gitea.MigrateRepoOption{
3439
RepoOwner: r.Owner,
3540
RepoName: r.Name,
36-
Private: !*r.Public,
41+
Private: !*r.PublicTarget,
3742
CloneAddr: cloneURL,
38-
AuthToken: source.GetToken(),
43+
AuthToken: authToken,
3944
Mirror: true,
4045
MirrorInterval: r.Interval.String(),
4146
}
@@ -46,7 +51,7 @@ func (c *Controller) CreateMirror(source server.Server, r *config.Repository) (*
4651
}
4752

4853
func (c *Controller) UpdateMirror(r *config.Repository) (*gitea.Repository, error) {
49-
private := !*r.Public
54+
private := !*r.PublicTarget
5055
interval := r.Interval.String()
5156
options := gitea.EditRepoOption{
5257
Private: &private,

0 commit comments

Comments
 (0)