This repository has been archived by the owner on Aug 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.go
87 lines (75 loc) · 1.71 KB
/
git.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
package main
import (
"fmt"
"github.com/vaughan0/go-ini"
"regexp"
"syscall"
)
type GitConfig struct {
file ini.File
user string
ip string
port string
home string
}
func (git *GitConfig) Parse() {
if git.file == nil {
git.Read()
}
file := git.file
url, _ := file.Get("remote \"webbynode\"", "url")
if isSshFormatted(url) {
re1, err := regexp.Compile(`^ssh://(\w+)@(.+)/(.+)$`)
if err != nil {
panic(nil)
}
res := re1.FindStringSubmatch(url)
git.user = res[1]
part := res[2]
re2, _ := regexp.Compile(`(.*):(\d*)\/(.*)$`)
if re2.MatchString(part) {
res := re2.FindStringSubmatch(part)
git.ip = res[1]
git.port = res[2]
git.home = res[3]
}
} else {
re1, err := regexp.Compile(`^(\w+)@(.+):(.+)$`)
if err != nil {
panic(nil)
}
res := re1.FindStringSubmatch(url)
git.user = res[1]
git.ip = res[2]
git.port = "22"
}
}
func (git *GitConfig) AddSshKey(passphrase string) {
sshKey := GetHomePath(".ssh/id_rsa.pub")
if !FileExists(sshKey) {
createSshKey(sshKey, passphrase)
fmt.Println("Doesn't exist")
}
}
func createSshKey(keyFile, passphrase string) {
// command := `/bin/bash -c "test -f %s"
}
var SshFormat = regexp.MustCompile(`^ssh://(\w+)@(.+)/(.+)$`)
func isSshFormatted(url string) bool {
return SshFormat.MatchString(url)
}
func (git *GitConfig) SshConsole() {
syscall.Exec("/usr/bin/ssh",
[]string{"ssh", "-p", git.port, git.user + "@" + git.ip},
[]string{})
}
func (git *GitConfig) ReadFromFile(fileName string) {
file, err := ini.LoadFile(fileName)
if err != nil {
panic(err)
}
git.file = file
}
func (git *GitConfig) Read() {
git.ReadFromFile(".git/config")
}