-
Notifications
You must be signed in to change notification settings - Fork 120
/
provider.go
78 lines (65 loc) · 1.58 KB
/
provider.go
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
package git
import (
"context"
"fmt"
"sort"
)
//go:generate mockery --name Provider --filename provider.go
type (
// Provider represents a git provider
Provider interface {
// CreateRepository creates the repository in the remote provider and returns a
// clone url
CreateRepository(ctx context.Context, opts *CreateRepoOptions) (string, error)
}
Auth struct {
Username string
Password string
}
// ProviderOptions for a new git provider
ProviderOptions struct {
Type string
Auth *Auth
Host string
}
CreateRepoOptions struct {
Owner string
Name string
Private bool
}
GetRepoOptions struct {
Owner string
Name string
}
)
// Errors
var (
ErrProviderNotSupported = func(providerType string) error {
return fmt.Errorf("git provider '%s' not supported", providerType)
}
ErrAuthenticationFailed = func(err error) error {
return fmt.Errorf("authentication failed, make sure credentials are correct: %w", err)
}
)
var supportedProviders = map[string]func(*ProviderOptions) (Provider, error){
"github": newGithub,
"gitea": newGitea,
"gitlab": newGitlab,
Azure: newAdo,
}
// New creates a new git provider
func newProvider(opts *ProviderOptions) (Provider, error) {
cons, exists := supportedProviders[opts.Type]
if !exists {
return nil, ErrProviderNotSupported(opts.Type)
}
return cons(opts)
}
func Providers() []string {
res := make([]string, 0, len(supportedProviders))
for p := range supportedProviders {
res = append(res, p)
}
sort.Strings(res) // must sort the providers by name, otherwise the codegen is not deterministic
return res
}