-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgit.go
More file actions
106 lines (79 loc) · 2.15 KB
/
git.go
File metadata and controls
106 lines (79 loc) · 2.15 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package client
import (
"errors"
"fmt"
"io/ioutil"
"os/exec"
"strings"
)
// CreateRemote adds a git remote in the current directory.
func (c Client) CreateRemote(remote, appID string) error {
cmd := exec.Command("git", "remote", "add", remote, c.RemoteURL(appID))
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
if err = cmd.Start(); err != nil {
return err
}
output, _ := ioutil.ReadAll(stderr)
fmt.Print(string(output))
if err := cmd.Wait(); err != nil {
return err
}
fmt.Printf("Git remote %s added\n", remote)
return nil
}
// DeleteRemote removes a git remote in the current directory.
func (c Client) DeleteRemote(appID string) error {
name, err := remoteNameFromAppID(appID)
if err != nil {
return err
}
if _, err = exec.Command("git", "remote", "remove", name).Output(); err != nil {
return err
}
fmt.Printf("Git remote %s removed\n", name)
return nil
}
func remoteNameFromAppID(appID string) (string, error) {
out, err := exec.Command("git", "remote", "-v").Output()
if err != nil {
return "", err
}
cmd := string(out)
for _, line := range strings.Split(cmd, "\n") {
if strings.Contains(line, appID) {
return strings.Split(line, "\t")[0], nil
}
}
return "", errors.New("Could not find remote matching app in 'git remote -v'")
}
// DetectApp detects if there is deis remote in git.
func (c Client) DetectApp() (string, error) {
remote, err := c.findRemote()
if err != nil {
return "", err
}
ss := strings.Split(remote, "/")
return strings.Split(ss[len(ss)-1], ".")[0], nil
}
func (c Client) findRemote() (string, error) {
out, err := exec.Command("git", "remote", "-v").Output()
if err != nil {
return "", err
}
cmd := string(out)
for _, line := range strings.Split(cmd, "\n") {
for _, remote := range strings.Split(line, " ") {
if strings.Contains(remote, c.ControllerURL.Host) {
return strings.Split(remote, "\t")[1], nil
}
}
}
return "", errors.New("Could not find deis remote in 'git remote -v'")
}
// RemoteURL returns the git URL of app.
func (c Client) RemoteURL(appID string) string {
return fmt.Sprintf("ssh://git@%s:2222/%s.git", c.ControllerURL.Host, appID)
}