This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (131 loc) · 2.67 KB
/
main.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"bufio"
"flag"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"strings"
)
const (
userField = "username"
passwordField = "password"
hostField = "host"
schemeField = "protocol"
pathField = "path"
)
func main() {
flag.Parse()
if flag.NArg() == 0 {
fmt.Printf("Usage: %s <get|store|erase>\n", os.Args[0])
os.Exit(2)
}
switch flag.Arg(0) {
case "store", "erase":
os.Exit(0)
case "get":
handleGet()
default:
fmt.Printf("Usage: %s <get|store|erase>\n", os.Args[0])
os.Exit(2)
}
}
func handleGet() {
home, err := os.UserHomeDir()
if err != nil {
fmt.Println("Home directory:", err)
os.Exit(1)
}
creds, err := loadCredentials(filepath.Join(home, ".git-credentials"))
if err != nil {
fmt.Println("Load credentials:", err)
os.Exit(1)
}
vals, err := readMap(os.Stdin)
if err != nil {
fmt.Println("Read values:", err)
os.Exit(1)
}
cred := lookupCredential(vals, creds)
if cred == nil {
os.Exit(1)
}
vals[schemeField] = cred.Scheme
vals[hostField] = cred.Host
if v := cred.User.Username(); v != "" {
vals[userField] = v
}
if v, ok := cred.User.Password(); ok && v != "" {
vals[passwordField] = v
}
if v := cred.Path; v != "" {
vals[pathField] = v
}
printMap(os.Stdout, vals)
}
func lookupCredential(m map[string]string, creds []*url.URL) *url.URL {
var potential *url.URL
for _, cred := range creds {
if cred.Scheme != m[schemeField] {
continue
}
if cred.Host != m[hostField] {
continue
}
wantUser := m[userField]
haveUser := cred.User.Username()
if wantUser != "" && haveUser != "" && wantUser != haveUser {
continue
}
wantPath := m[pathField]
if wantPath != "" && cred.Path != "" && !strings.HasPrefix(wantPath, cred.Path) {
continue
}
if wantPath == "" && cred.Path != "" {
potential = cred
continue
}
return cred
}
return potential
}
func loadCredentials(path string) ([]*url.URL, error) {
fd, err := os.Open(path)
if err != nil {
return nil, err
}
defer fd.Close()
br := bufio.NewScanner(fd)
var urls []*url.URL
for br.Scan() {
uri, err := url.Parse(br.Text())
if err != nil {
return nil, err
}
uri.Path = strings.TrimLeft(uri.Path, "/")
urls = append(urls, uri)
}
return urls, nil
}
func readMap(r io.Reader) (map[string]string, error) {
br := bufio.NewScanner(r)
m := make(map[string]string)
for br.Scan() {
if br.Text() == "" {
break
}
fields := strings.SplitN(br.Text(), "=", 2)
if len(fields) != 2 {
continue
}
m[strings.TrimSpace(fields[0])] = strings.TrimSpace(fields[1])
}
return m, br.Err()
}
func printMap(w io.Writer, m map[string]string) {
for key, val := range m {
fmt.Fprintf(w, "%s=%s\n", key, val)
}
}