Skip to content

Commit db38964

Browse files
fix(git): properly log errors from git (#199)
1 parent 721ad2d commit db38964

2 files changed

Lines changed: 44 additions & 30 deletions

File tree

cmd/git.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ func (d DeisCmd) GitRemote(appID, remote string, force bool) error {
1818
if err != nil {
1919
//If git remote doesn't exist, create it without issue
2020
if err == git.ErrRemoteNotFound {
21-
git.CreateRemote(s.Client.ControllerURL.Host, remote, appID)
21+
err := git.CreateRemote(s.Client.ControllerURL.Host, remote, appID)
22+
if err != nil {
23+
return err
24+
}
2225
d.Printf(remoteCreationMsg, remote, appID)
2326
return nil
2427
}
@@ -35,8 +38,14 @@ func (d DeisCmd) GitRemote(appID, remote string, force bool) error {
3538

3639
if force {
3740
d.Printf("Deleting git remote %s.\n", remote)
38-
git.DeleteRemote(remote)
39-
git.CreateRemote(s.Client.ControllerURL.Host, remote, appID)
41+
err := git.DeleteRemote(remote)
42+
if err != nil {
43+
return err
44+
}
45+
err = git.CreateRemote(s.Client.ControllerURL.Host, remote, appID)
46+
if err != nil {
47+
return err
48+
}
4049
d.Printf(remoteCreationMsg, remote, appID)
4150
return nil
4251
}

pkg/git/git.go

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package git
33
import (
44
"errors"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
"os/exec"
98
"path/filepath"
@@ -14,23 +13,25 @@ import (
1413
// ErrRemoteNotFound is returned when the remote cannot be found in git
1514
var ErrRemoteNotFound = errors.New("Could not find remote matching app in 'git remote -v'")
1615

16+
func gitError(err *exec.ExitError, cmd []string) error {
17+
msg := fmt.Sprintf("Error when running '%s'\n", strings.Join(cmd, " "))
18+
out := string(err.Stderr)
19+
if out != "" {
20+
msg += strings.TrimSpace(out)
21+
}
22+
23+
return errors.New(msg)
24+
}
25+
1726
// CreateRemote adds a git remote in the current directory.
1827
func CreateRemote(host, remote, appID string) error {
19-
cmd := exec.Command("git", "remote", "add", remote, RemoteURL(host, appID))
20-
stderr, err := cmd.StderrPipe()
21-
28+
cmd := []string{"git", "remote", "add", remote, RemoteURL(host, appID)}
29+
_, err := exec.Command(cmd[0], cmd[1:]...).Output()
2230
if err != nil {
23-
return err
24-
}
25-
26-
if err = cmd.Start(); err != nil {
27-
return err
31+
return gitError(err.(*exec.ExitError), cmd)
2832
}
2933

30-
output, _ := ioutil.ReadAll(stderr)
31-
fmt.Print(string(output))
32-
33-
return cmd.Wait()
34+
return nil
3435
}
3536

3637
// DeleteAppRemotes removes all git remotes corresponding to an app in the repository.
@@ -52,23 +53,28 @@ func DeleteAppRemotes(host, appID string) error {
5253

5354
// DeleteRemote removes a remote from the repository
5455
func DeleteRemote(name string) error {
55-
_, err := exec.Command("git", "remote", "remove", name).Output()
56-
return err
56+
cmd := []string{"git", "remote", "remove", name}
57+
_, err := exec.Command(cmd[0], cmd[1:]...).Output()
58+
if err != nil {
59+
return gitError(err.(*exec.ExitError), cmd)
60+
}
61+
62+
return nil
5763
}
5864

5965
// remoteNamesFromAppID returns the git remote names for an app
6066
func remoteNamesFromAppID(host, appID string) ([]string, error) {
61-
out, err := exec.Command("git", "remote", "-v").Output()
67+
cmd := []string{"git", "remote", "-v"}
68+
out, err := exec.Command(cmd[0], cmd[1:]...).Output()
6269

6370
if err != nil {
64-
return []string{}, err
71+
return []string{}, gitError(err.(*exec.ExitError), cmd)
6572
}
6673

67-
cmd := string(out)
6874
remotes := []string{}
6975

7076
lines:
71-
for _, line := range strings.Split(cmd, "\n") {
77+
for _, line := range strings.Split(string(out), "\n") {
7278
if strings.Contains(line, RemoteURL(host, appID)) {
7379
name := strings.Split(line, "\t")[0]
7480
// git remote -v can show duplicate remotes, so don't add a remote if it already has been added
@@ -103,19 +109,17 @@ func DetectAppName(host string) (string, error) {
103109
}
104110

105111
func findRemote(host string) (string, error) {
106-
out, err := exec.Command("git", "remote", "-v").Output()
107-
112+
cmd := []string{"git", "remote", "-v"}
113+
out, err := exec.Command(cmd[0], cmd[1:]...).Output()
108114
if err != nil {
109-
return "", err
115+
return "", gitError(err.(*exec.ExitError), cmd)
110116
}
111117

112-
cmd := string(out)
113-
114118
// Strip off any trailing :port number after the host name.
115119
host = strings.Split(host, ":")[0]
116120
builderHost := getBuilderHostname(host)
117121

118-
for _, line := range strings.Split(cmd, "\n") {
122+
for _, line := range strings.Split(string(out), "\n") {
119123
for _, remote := range strings.Split(line, " ") {
120124
if strings.Contains(remote, host) || strings.Contains(remote, builderHost) {
121125
return strings.Split(remote, "\t")[1], nil
@@ -142,14 +146,15 @@ func getBuilderHostname(host string) string {
142146

143147
// RemoteValue gets the url that a git remote is set to.
144148
func RemoteValue(name string) (string, error) {
145-
out, err := exec.Command("git", "remote", "get-url", name).Output()
149+
cmd := []string{"git", "remote", "get-url", name}
150+
out, err := exec.Command(cmd[0], cmd[1:]...).Output()
146151

147152
if err != nil {
148153
// get the return code of the program and see if it equals not found
149154
if err.(*exec.ExitError).Sys().(syscall.WaitStatus).ExitStatus() == 128 {
150155
return "", ErrRemoteNotFound
151156
}
152-
return "", err
157+
return "", gitError(err.(*exec.ExitError), cmd)
153158
}
154159

155160
return strings.Trim(string(out), "\n"), nil

0 commit comments

Comments
 (0)