Reduce stable client coupling to syncer · Entire

Reduce stable client coupling to syncer

cafba1a→main·

Soph·3mo ago·2 files·+132 added/-40 removed

Sessions

2f33299e4045View transcript

Changes

2

4 unmodified lines

5
6
7
8
8
9
10
11
22 unmodified lines

34
35
36
37
37
38
39
40
9 unmodified lines

50
51
52
53
53
54
55
56
9 unmodified lines

66
67
68
69
69
70
71
72
73
74
75
76
76
77
78
79
80
81
82
83
84
85
86
79
80
81
82
83
91
84
85
93
86
87
88
89
90
91
92
93
94
95
96
95
97
98
99
100
101
102
103
104
105
106
107
108
98
109
110
111
101
112
113
114
115
105
116
117
107
108
109
110
111
112
113
114
115
116
117
118
119
120
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
3 unmodified lines

140
141
142
130
131
132
133
134
135
136
143
144
145

4 unmodified lines

"fmt"
    "net/http"

"github.com/soph/git-sync/internal/syncer"
    "github.com/soph/git-sync/pkg/gitsync/internalbridge"

// Options configures a Client. It is intentionally small in the first public cut.
22 unmodified lines

if err != nil {
        return ProbeResult{}, err
    }
    result, err := syncer.Probe(ctx, cfg)
    result, err := internalbridge.Probe(ctx, cfg)
    if err != nil {
        return ProbeResult{}, err
    }
9 unmodified lines

if err != nil {
        return PlanResult{}, err
    }
    result, err := syncer.Run(ctx, cfg)
    result, err := internalbridge.Run(ctx, cfg)
    if err != nil {
        return PlanResult{}, err
    }
9 unmodified lines

if err != nil {
        return SyncResult{}, err
    }
    result, err := syncer.Run(ctx, cfg)
    result, err := internalbridge.Run(ctx, cfg)
    if err != nil {
        return SyncResult{}, err
    }
    return fromSyncerResult(result), nil
}

func (c *Client) buildProbeConfig(ctx context.Context, req ProbeRequest) (syncer.Config, error) {
func (c *Client) buildProbeConfig(ctx context.Context, req ProbeRequest) (internalbridge.Config, error) {
    sourceAuth, err := c.authFor(ctx, req.Source, SourceRole)
    if err != nil {
        return syncer.Config{}, err
    }
    cfg := syncer.Config{
        Source:       syncer.Endpoint{URL: req.Source.URL, Username: sourceAuth.Username, Token: sourceAuth.Token, BearerToken: sourceAuth.BearerToken, SkipTLSVerify: sourceAuth.SkipTLSVerify},
        HTTPClient:   c.httpClient,
        IncludeTags:  req.IncludeTags,
        ShowStats:    req.CollectStats,
        ProtocolMode: string(req.Protocol),
    }
    return internalbridge.Config{}, err
    if req.Target != nil {
        targetAuth, err := c.authFor(ctx, *req.Target, TargetRole)
        if err != nil {
            return syncer.Config{}, err
            return internalbridge.Config{}, err
        }
        cfg.Target = syncer.Endpoint{URL: req.Target.URL, Username: targetAuth.Username, Token: targetAuth.Token, BearerToken: targetAuth.BearerToken, SkipTLSVerify: targetAuth.SkipTLSVerify}
        return internalbridge.ProbeConfig(
            internalbridge.Endpoint{URL: req.Source.URL},
            internalbridge.EndpointAuth(sourceAuth),
            &internalbridge.Endpoint{URL: req.Target.URL},
            internalbridge.EndpointAuth(targetAuth),
            internalbridge.ProtocolMode(req.Protocol),
            req.IncludeTags,
            req.CollectStats,
            c.httpClient,
        ), nil
    }
    return cfg, nil
    return internalbridge.ProbeConfig(
        internalbridge.Endpoint{URL: req.Source.URL},
        internalbridge.EndpointAuth(sourceAuth),
        nil,
        internalbridge.EndpointAuth{},
        internalbridge.ProtocolMode(req.Protocol),
        req.IncludeTags,
        req.CollectStats,
        c.httpClient,
        ), nil
}

func (c *Client) buildSyncConfig(ctx context.Context, source Endpoint, target Endpoint, scope RefScope, policy SyncPolicy, collectStats, dryRun bool) (syncer.Config, error) {
func (c *Client) buildSyncConfig(ctx context.Context, source Endpoint, target Endpoint, scope RefScope, policy SyncPolicy, collectStats, dryRun bool) (internalbridge.Config, error) {
    sourceAuth, err := c.authFor(ctx, source, SourceRole)
    if err != nil {
        return syncer.Config{}, err
        return internalbridge.Config{}, err
    }
    targetAuth, err := c.authFor(ctx, target, TargetRole)
    if err != nil {
        return syncer.Config{}, err
        return internalbridge.Config{}, err
    }
    return syncer.Config{
        Source:                 syncer.Endpoint{URL: source.URL, Username: sourceAuth.Username, Token: sourceAuth.Token, BearerToken: sourceAuth.BearerToken, SkipTLSVerify: sourceAuth.SkipTLSVerify},
        Target:                 syncer.Endpoint{URL: target.URL, Username: targetAuth.Username, Token: targetAuth.Token, BearerToken: targetAuth.BearerToken, SkipTLSVerify: targetAuth.SkipTLSVerify},
        HTTPClient:             c.httpClient,
        Branches:               append([]string(nil), scope.Branches...),
        Mappings:               append([]RefMapping(nil), scope.Mappings...),
        IncludeTags:            policy.IncludeTags,
        DryRun:                 dryRun,
        ShowStats:              collectStats,
        Force:                  policy.Force,
        Prune:                  policy.Prune,
        ProtocolMode:           protocolString(policy.Protocol),
        MaterializedMaxObjects: syncer.DefaultMaterializedMaxObjects,
    }, nil
    return internalbridge.SyncConfig(
        internalbridge.Endpoint{URL: source.URL},
        internalbridge.EndpointAuth(sourceAuth),
        internalbridge.Endpoint{URL: target.URL},
        internalbridge.EndpointAuth(targetAuth),
        internalbridge.RefScope{Branches: append([]string(nil), scope.Branches...), Mappings: append([]internalbridge.RefMapping(nil), scope.Mappings...)},
        internalbridge.SyncPolicy{
            IncludeTags: policy.IncludeTags,
            Force:       policy.Force,
            Prune:       policy.Prune,
            Protocol:    internalbridge.ProtocolMode(policy.Protocol),
        },
        collectStats,
        dryRun,
        c.httpClient,
    ), nil
}

func (c *Client) authFor(ctx context.Context, endpoint Endpoint, role EndpointRole) (EndpointAuth, error) {
3 unmodified lines

return c.auth.AuthFor(ctx, endpoint, role)
}

func protocolString(mode ProtocolMode) string {
    if mode == "" {
        return string(ProtocolAuto)
    }
    return string(mode)
}

func (r SyncRequest) Validate() error {
    if r.Source.URL == "" {
        return fmt.Errorf("source URL is required")
    }

Mpkg/gitsync/client.go+46/-40

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

package internalbridge

import (
    "context"
    "net/http"

"github.com/soph/git-sync/internal/syncer"
    "github.com/soph/git-sync/internal/validation"
)

type ProtocolMode string

type Config = syncer.Config

const ProtocolAuto ProtocolMode = validation.ProtocolAuto

type RefMapping = validation.RefMapping

type Endpoint struct {
    URL string
}

type EndpointAuth struct {
    Username      string
    Token         string
    BearerToken   string
    SkipTLSVerify bool
}

type RefScope struct {
    Branches []string
    Mappings []RefMapping
}

type SyncPolicy struct {
    IncludeTags bool
    Force       bool
    Prune       bool
    Protocol    ProtocolMode
}

func ProbeConfig(source Endpoint, sourceAuth EndpointAuth, target *Endpoint, targetAuth EndpointAuth, protocol ProtocolMode, includeTags, collectStats bool, httpClient *http.Client) syncer.Config {
    cfg := syncer.Config{
        Source:       syncer.Endpoint{URL: source.URL, Username: sourceAuth.Username, Token: sourceAuth.Token, BearerToken: sourceAuth.BearerToken, SkipTLSVerify: sourceAuth.SkipTLSVerify},
        HTTPClient:   httpClient,
        IncludeTags:  includeTags,
        ShowStats:    collectStats,
        ProtocolMode: protocolString(protocol),
    }
    if target != nil {
        cfg.Target = syncer.Endpoint{URL: target.URL, Username: targetAuth.Username, Token: targetAuth.Token, BearerToken: targetAuth.BearerToken, SkipTLSVerify: targetAuth.SkipTLSVerify}
    }
    return cfg
}

func SyncConfig(source Endpoint, sourceAuth EndpointAuth, target Endpoint, targetAuth EndpointAuth, scope RefScope, policy SyncPolicy, collectStats, dryRun bool, httpClient *http.Client) syncer.Config {
    return syncer.Config{
        Source:                 syncer.Endpoint{URL: source.URL, Username: sourceAuth.Username, Token: sourceAuth.Token, BearerToken: sourceAuth.BearerToken, SkipTLSVerify: sourceAuth.SkipTLSVerify},
        Target:                 syncer.Endpoint{URL: target.URL, Username: targetAuth.Username, Token: targetAuth.Token, BearerToken: targetAuth.BearerToken, SkipTLSVerify: targetAuth.SkipTLSVerify},
        HTTPClient:             httpClient,
        Branches:               append([]string(nil), scope.Branches...),
        Mappings:               append([]RefMapping(nil), scope.Mappings...),
        IncludeTags:            policy.IncludeTags,
        DryRun:                 dryRun,
        ShowStats:              collectStats,
        Force:                  policy.Force,
        Prune:                  policy.Prune,
        ProtocolMode:           protocolString(policy.Protocol),
        MaterializedMaxObjects: syncer.DefaultMaterializedMaxObjects,
    }, nil
}

func Probe(ctx context.Context, cfg syncer.Config) (syncer.ProbeResult, error) {
    return syncer.Probe(ctx, cfg)
}

func Run(ctx context.Context, cfg syncer.Config) (syncer.Result, error) {
    return syncer.Run(ctx, cfg)
}

func protocolString(mode ProtocolMode) string {
    if mode == "" {
        return string(ProtocolAuto)
    }
    return string(mode)
}