-
Notifications
You must be signed in to change notification settings - Fork 787
/
git_credential.go
125 lines (100 loc) · 3.15 KB
/
git_credential.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package credentialhelper
import (
"encoding/json"
"fmt"
"net/url"
"reflect"
"strings"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
)
// GitCredential represents the different parts of a git credential URL
// See also https://git-scm.com/docs/git-credential
type GitCredential struct {
Protocol string
Host string
Path string
Username string
Password string
}
// CreateGitCredential creates a CreateGitCredential instance from a slice of strings where each element is a key/value pair
// separated by '='.
func CreateGitCredential(lines []string) (GitCredential, error) {
var credential GitCredential
if lines == nil {
return credential, errors.New("no data lines provided")
}
fieldMap, err := util.ExtractKeyValuePairs(lines, "=")
if err != nil {
return credential, errors.Wrap(err, "unable to extract git credential parameters")
}
data, err := json.Marshal(fieldMap)
if err != nil {
return GitCredential{}, errors.Wrapf(err, "unable to marshal git credential data")
}
err = json.Unmarshal(data, &credential)
if err != nil {
return GitCredential{}, errors.Wrapf(err, "unable unmarshal git credential data")
}
return credential, nil
}
// CreateGitCredentialFromURL creates a CreateGitCredential instance from a URL and optional username and password.
func CreateGitCredentialFromURL(gitURL string, username string, password string) (GitCredential, error) {
var credential GitCredential
if gitURL == "" {
return credential, errors.New("url cannot be empty")
}
u, err := url.Parse(gitURL)
if err != nil {
return credential, errors.Wrapf(err, "unable to parse URL %s", gitURL)
}
credential.Protocol = u.Scheme
credential.Host = u.Host
credential.Path = u.Path
if username != "" {
credential.Username = username
}
if password != "" {
credential.Password = password
}
return credential, nil
}
// String returns a string representation of this instance according to the expected format of git credential helpers.
// See also https://git-scm.com/docs/git-credential
func (g *GitCredential) String() string {
answer := ""
value := reflect.ValueOf(g).Elem()
typeOfT := value.Type()
for i := 0; i < value.NumField(); i++ {
field := value.Field(i)
answer = answer + fmt.Sprintf("%s=%v\n", strings.ToLower(typeOfT.Field(i).Name), field.Interface())
}
answer = answer + "\n"
return answer
}
// Clone clones this GitCredential instance
func (g *GitCredential) Clone() GitCredential {
clone := GitCredential{}
value := reflect.ValueOf(g).Elem()
typeOfT := value.Type()
for i := 0; i < value.NumField(); i++ {
field := value.Field(i)
value := field.String()
v := reflect.ValueOf(&clone).Elem().FieldByName(typeOfT.Field(i).Name)
v.SetString(value)
}
return clone
}
// URL returns a URL from the data of this instance. If not enough information exist an error is returned
func (g *GitCredential) URL() (url.URL, error) {
urlAsString := g.Protocol + "://" + g.Host
if g.Path != "" {
urlAsString = urlAsString + "/" + g.Path
}
u, err := url.Parse(urlAsString)
if err != nil {
return url.URL{}, errors.Wrap(err, "unable to construct URL")
}
u.User = url.UserPassword(g.Username, g.Password)
return *u, nil
}