-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprovider.go
More file actions
83 lines (70 loc) · 2.07 KB
/
provider.go
File metadata and controls
83 lines (70 loc) · 2.07 KB
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
package gobookmarks
import (
"context"
"golang.org/x/oauth2"
"time"
)
type User struct {
Login string
}
type Branch struct {
Name string
}
type Tag struct {
Name string
}
type Commit struct {
SHA string
Message string
CommitterName string
CommitterEmail string
CommitterDate time.Time
}
type Provider interface {
Name() string
OAuth2Config(clientID, clientSecret, redirectURL string) *oauth2.Config
CurrentUser(ctx context.Context, token *oauth2.Token) (*User, error)
GetTags(ctx context.Context, user string, token *oauth2.Token) ([]*Tag, error)
GetBranches(ctx context.Context, user string, token *oauth2.Token) ([]*Branch, error)
GetCommits(ctx context.Context, user string, token *oauth2.Token) ([]*Commit, error)
GetBookmarks(ctx context.Context, user, ref string, token *oauth2.Token) (string, string, error)
UpdateBookmarks(ctx context.Context, user string, token *oauth2.Token, sourceRef, branch, text, expectSHA string) error
CreateBookmarks(ctx context.Context, user string, token *oauth2.Token, branch, text string) error
CreateRepo(ctx context.Context, user string, token *oauth2.Token, name string) error
DefaultServer() string
}
var providers = map[string]Provider{}
func RegisterProvider(p Provider) { providers[p.Name()] = p }
func GetProvider(name string) Provider { return providers[name] }
func ProviderNames() []string {
names := make([]string, 0, len(providers))
for n := range providers {
names = append(names, n)
}
return names
}
var ActiveProvider Provider
// SetProviderByName activates the named provider.
// It returns true if the provider exists.
func SetProviderByName(name string) bool {
if p, ok := providers[name]; ok {
prevDefault := ""
if ActiveProvider != nil {
prevDefault = ActiveProvider.DefaultServer()
}
ActiveProvider = p
if GitServer == "" || GitServer == prevDefault {
GitServer = p.DefaultServer()
}
return true
}
return false
}
func init() {
if p, ok := providers["github"]; ok {
ActiveProvider = p
if GitServer == "" {
GitServer = p.DefaultServer()
}
}
}