-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrurl.go
127 lines (97 loc) · 2.38 KB
/
grurl.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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"regexp"
"strings"
)
func main() {
listOption := flag.Bool("list", false, "list all remote names with URLs and exit")
remoteOption := flag.String("remote", "origin", "to set the remote name you for required url")
pathOption := flag.String("path", ".", "to set the git repository path")
flag.Parse()
path := *pathOption + "/.git/config"
config := NewRemoteConfig()
err := config.ParseFile(path)
if err != nil {
fmt.Println("Check path value.")
os.Exit(1)
}
if *remoteOption == "" {
fmt.Println("Remote value is required.")
os.Exit(1)
}
if *listOption {
for key, value := range config.data {
fmt.Printf("%s \t %s\n", key, value)
}
return
}
if *remoteOption != "" {
if url, err := config.data[*remoteOption]; err {
fmt.Println(url)
return
}
fmt.Println("This remote isn't exist.")
os.Exit(1)
}
}
type RemoteConfig struct {
configPath string
data map[string]string
}
func (c *RemoteConfig) ParseFile(path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
c.configPath = path
c.Parse(file)
return nil
}
func (c *RemoteConfig) Parse(file *os.File) {
const remoteRowStart = `[remote "`
const remoteUrlRowStart = `url = `
var remoteName string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.Index(line, remoteRowStart) != -1 {
remoteName = line[len(remoteRowStart) : len(line)-2]
c.data[remoteName] = ""
}
if strings.Index(line, remoteUrlRowStart) != -1 && remoteName != "" {
url := line[len(remoteUrlRowStart):]
url = SshUrlToHttpUrl(url)
c.data[remoteName] = url
remoteName = ""
}
}
}
//Checks if a url is a valid ssh url and converts it to http url or returns the same url
func SshUrlToHttpUrl(url string) string {
regex := regexp.MustCompile(
`^(?P<user>.+)@(?P<host>.+?):(?P<port>[0-9]*)(?P<path>.+)$`,
)
groups := regex.FindStringSubmatch(url)
if len(groups) == 5 {
url = ""
url = fmt.Sprintf("https://%s", groups[2])
if groups[3] != "" {
url += fmt.Sprintf(":%s", groups[3])
}
if strings.Index(groups[4], "/") == 0 {
url += fmt.Sprintf("%s", groups[4])
} else {
url += fmt.Sprintf("/%s", groups[4])
}
return url
}
return url
}
func NewRemoteConfig() *RemoteConfig {
return &RemoteConfig{data: map[string]string{}}
}