Skip to content

Commit 88473cd

Browse files
committed
Improve error output
* initial debug output when using `helm --debug` * output stderr from child commands in case of an error * Remove spurios output of error to stdout
1 parent 60f712d commit 88473cd

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

cmd/helm3.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"os/exec"
77
"strconv"
8+
"strings"
89
)
910

1011
func getRelease(release, namespace string) ([]byte, error) {
@@ -13,7 +14,7 @@ func getRelease(release, namespace string) ([]byte, error) {
1314
args = append(args, "--namespace", namespace)
1415
}
1516
cmd := exec.Command(os.Getenv("HELM_BIN"), args...)
16-
return cmd.Output()
17+
return outputWithRichError(cmd)
1718
}
1819

1920
func getHooks(release, namespace string) ([]byte, error) {
@@ -22,7 +23,7 @@ func getHooks(release, namespace string) ([]byte, error) {
2223
args = append(args, "--namespace", namespace)
2324
}
2425
cmd := exec.Command(os.Getenv("HELM_BIN"), args...)
25-
return cmd.Output()
26+
return outputWithRichError(cmd)
2627
}
2728

2829
func getRevision(release string, revision int, namespace string) ([]byte, error) {
@@ -31,7 +32,7 @@ func getRevision(release string, revision int, namespace string) ([]byte, error)
3132
args = append(args, "--namespace", namespace)
3233
}
3334
cmd := exec.Command(os.Getenv("HELM_BIN"), args...)
34-
return cmd.Output()
35+
return outputWithRichError(cmd)
3536
}
3637

3738
func getChart(release, namespace string) (string, error) {
@@ -40,7 +41,7 @@ func getChart(release, namespace string) (string, error) {
4041
args = append(args, "--namespace", namespace)
4142
}
4243
cmd := exec.Command(os.Getenv("HELM_BIN"), args...)
43-
out, err := cmd.Output()
44+
out, err := outputWithRichError(cmd)
4445
if err != nil {
4546
return "", err
4647
}
@@ -87,11 +88,12 @@ func (d *diffCmd) template() ([]byte, error) {
8788
args := []string{"template", d.release, d.chart}
8889
args = append(args, flags...)
8990
cmd := exec.Command(os.Getenv("HELM_BIN"), args...)
90-
return cmd.Output()
91+
return outputWithRichError(cmd)
9192
}
9293

9394
func (d *diffCmd) existingValues(f *os.File) error {
9495
cmd := exec.Command(os.Getenv("HELM_BIN"), "get", "values", d.release, "--all")
96+
DebugPrint("Executing %s", strings.Join(cmd.Args, " "))
9597
defer f.Close()
9698
cmd.Stdout = f
9799
return cmd.Run()

cmd/helpers.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package cmd
33
import (
44
"fmt"
55
"os"
6+
"os/exec"
67
"path/filepath"
8+
"strings"
79

810
flag "github.com/spf13/pflag"
911
"k8s.io/client-go/util/homedir"
@@ -31,6 +33,15 @@ func isHelm3() bool {
3133
return os.Getenv("TILLER_HOST") == ""
3234
}
3335

36+
func isDebug() bool {
37+
return os.Getenv("HELM_DEBUG") == "true"
38+
}
39+
func DebugPrint(format string, a ...interface{}) {
40+
if isDebug() {
41+
fmt.Printf(format+"\n", a...)
42+
}
43+
}
44+
3445
func addCommonCmdOptions(f *flag.FlagSet) {
3546
settings.AddFlagsTLS(f)
3647
settings.InitTLS(f)
@@ -71,3 +82,12 @@ func expandTLSPaths() {
7182
settings.TLSCertFile = os.ExpandEnv(settings.TLSCertFile)
7283
settings.TLSKeyFile = os.ExpandEnv(settings.TLSKeyFile)
7384
}
85+
86+
func outputWithRichError(cmd *exec.Cmd) ([]byte, error) {
87+
DebugPrint("Executing %s", strings.Join(cmd.Args, " "))
88+
output, err := cmd.Output()
89+
if exitError, ok := err.(*exec.ExitError); ok {
90+
return output, fmt.Errorf("%s: %s", exitError.Error(), string(exitError.Stderr))
91+
}
92+
return output, err
93+
}

cmd/upgrade.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"os"
7-
"os/exec"
87
"strings"
98

109
"github.com/spf13/cobra"
@@ -111,7 +110,7 @@ func (d *diffCmd) runHelm3() error {
111110
releaseManifest, err := getRelease(d.release, d.namespace)
112111

113112
var newInstall bool
114-
if err != nil && strings.Contains(string(err.(*exec.ExitError).Stderr), "release: not found") {
113+
if err != nil && strings.Contains(err.Error(), "release: not found") {
115114
if d.allowUnreleased {
116115
fmt.Printf("********************\n\n\tRelease was not present in Helm. Diff will show entire contents as new.\n\n********************\n")
117116
err = nil
@@ -122,12 +121,12 @@ func (d *diffCmd) runHelm3() error {
122121

123122
}
124123
if err != nil {
125-
return err
124+
return fmt.Errorf("Failed to get release %s in namespace %s: %s", d.release, d.namespace, err)
126125
}
127126

128127
installManifest, err := d.template()
129128
if err != nil {
130-
return err
129+
return fmt.Errorf("Failed to render chart: %s", err)
131130
}
132131

133132
currentSpecs := make(map[string]*manifest.MappingResult)

main.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package main
22

33
import (
4-
"fmt"
54
"os"
65

76
"github.com/databus23/helm-diff/cmd"
87
)
98

109
func main() {
1110
if err := cmd.New().Execute(); err != nil {
12-
fmt.Println(err)
1311
switch e := err.(type) {
1412
case cmd.Error:
1513
os.Exit(e.Code)

0 commit comments

Comments
 (0)