Skip to content

Commit 05029fe

Browse files
authored
feat: Dedicated exit code (2) on there's diff (databus23#78)
This feature is enabled only when the `--detailed-exitcode` flag (databus23#65) is provided. Supports `upgrade`, `revision` and `rollback` sub-commands. Resolves databus23#54, but only after helm/helm#4367 is released with helm v2.11.0, probably.
1 parent 7aa052c commit 05029fe

File tree

7 files changed

+65
-29
lines changed

7 files changed

+65
-29
lines changed

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ LDFLAGS := -X $(PKG)/cmd.Version=$(VERSION)
99
LDFLAGS += -X $(PKG)/vendor/k8s.io/helm/pkg/version.BuildMetadata=
1010
LDFLAGS += -X $(PKG)/vendor/k8s.io/helm/pkg/version.Version=$(shell grep -A1 "package: k8s.io/helm" glide.yaml | sed -n -e 's/[ ]*version:.*\(v[.0-9]*\).*/\1/p')
1111

12+
.PHONY: format
13+
format:
14+
test -z "$$(find . -path ./vendor -prune -type f -o -name '*.go' -exec gofmt -d {} + | tee /dev/stderr)" || \
15+
test -z "$$(find . -path ./vendor -prune -type f -o -name '*.go' -exec gofmt -w {} + | tee /dev/stderr)"
16+
1217
.PHONY: install
1318
install: build
1419
mkdir -p $(HELM_HOME)/plugins/helm-diff/bin

cmd/error.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package cmd
2+
3+
type Error struct {
4+
error
5+
Code int
6+
}

cmd/revision.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ import (
1313
)
1414

1515
type revision struct {
16-
release string
17-
client helm.Interface
18-
suppressedKinds []string
19-
revisions []string
20-
outputContext int
16+
release string
17+
client helm.Interface
18+
detailedExitCode bool
19+
suppressedKinds []string
20+
revisions []string
21+
outputContext int
2122
}
2223

2324
const revisionCmdLongUsage = `
@@ -121,13 +122,20 @@ func (d *revision) differentiate() error {
121122
return prettyError(err)
122123
}
123124

124-
diff.DiffManifests(
125+
seenAnyChanges := diff.DiffManifests(
125126
manifest.ParseRelease(revisionResponse1.Release),
126127
manifest.ParseRelease(revisionResponse2.Release),
127128
d.suppressedKinds,
128129
d.outputContext,
129130
os.Stdout)
130131

132+
if d.detailedExitCode && seenAnyChanges {
133+
return Error{
134+
error: errors.New("identified at least one change, exiting with non-zero exit code (detailed-exitcode parameter enabled)"),
135+
Code: 2,
136+
}
137+
}
138+
131139
default:
132140
return errors.New("Invalid Arguments")
133141
}

cmd/rollback.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@ import (
55
"os"
66
"strconv"
77

8+
"errors"
89
"github.com/databus23/helm-diff/diff"
910
"github.com/databus23/helm-diff/manifest"
1011
"github.com/spf13/cobra"
1112
"k8s.io/helm/pkg/helm"
1213
)
1314

1415
type rollback struct {
15-
release string
16-
client helm.Interface
17-
suppressedKinds []string
18-
revisions []string
19-
outputContext int
16+
release string
17+
client helm.Interface
18+
detailedExitCode bool
19+
suppressedKinds []string
20+
revisions []string
21+
outputContext int
2022
}
2123

2224
const rollbackCmdLongUsage = `
@@ -88,12 +90,19 @@ func (d *rollback) backcast() error {
8890
}
8991

9092
// create a diff between the current manifest and the version of the manifest that a user is intended to rollback
91-
diff.DiffManifests(
93+
seenAnyChanges := diff.DiffManifests(
9294
manifest.ParseRelease(releaseResponse.Release),
9395
manifest.ParseRelease(revisionResponse.Release),
9496
d.suppressedKinds,
9597
d.outputContext,
9698
os.Stdout)
9799

100+
if d.detailedExitCode && seenAnyChanges {
101+
return Error{
102+
error: errors.New("identified at least one change, exiting with non-zero exit code (detailed-exitcode parameter enabled)"),
103+
Code: 2,
104+
}
105+
}
106+
98107
return nil
99108
}

cmd/upgrade.go

+17-14
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ import (
55
"os"
66
"strings"
77

8+
"errors"
89
"github.com/databus23/helm-diff/diff"
910
"github.com/databus23/helm-diff/manifest"
1011
"github.com/spf13/cobra"
1112
"k8s.io/helm/pkg/helm"
12-
"errors"
1313
)
1414

1515
type diffCmd struct {
16-
release string
17-
chart string
18-
chartVersion string
19-
client helm.Interface
20-
detailedExitCode bool
21-
valueFiles valueFiles
22-
values []string
23-
reuseValues bool
24-
resetValues bool
25-
allowUnreleased bool
26-
suppressedKinds []string
27-
outputContext int
16+
release string
17+
chart string
18+
chartVersion string
19+
client helm.Interface
20+
detailedExitCode bool
21+
valueFiles valueFiles
22+
values []string
23+
reuseValues bool
24+
resetValues bool
25+
allowUnreleased bool
26+
suppressedKinds []string
27+
outputContext int
2828
}
2929

3030
const globalUsage = `Show a diff explaining what a helm upgrade would change.
@@ -150,7 +150,10 @@ func (d *diffCmd) run() error {
150150
seenAnyChanges := diff.DiffManifests(currentSpecs, newSpecs, d.suppressedKinds, d.outputContext, os.Stdout)
151151

152152
if d.detailedExitCode && seenAnyChanges {
153-
return errors.New("identified at least one change, exiting with non-zero exit code (detailed-exitcode parameter enabled)")
153+
return Error{
154+
error: errors.New("identified at least one change, exiting with non-zero exit code (detailed-exitcode parameter enabled)"),
155+
Code: 2,
156+
}
154157
}
155158

156159
return nil

diff/diff.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package diff
33
import (
44
"fmt"
55
"io"
6-
"strings"
76
"math"
7+
"strings"
88

99
"github.com/aryann/difflib"
1010
"github.com/mgutz/ansi"

main.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package main
22

33
import (
4-
"os"
54
"fmt"
5+
"os"
66

77
"github.com/databus23/helm-diff/cmd"
88
)
99

1010
func main() {
1111
if err := cmd.New().Execute(); err != nil {
1212
fmt.Println(err)
13-
os.Exit(1)
13+
switch e := err.(type) {
14+
case cmd.Error:
15+
os.Exit(e.Code)
16+
default:
17+
os.Exit(1)
18+
}
1419
}
1520
}

0 commit comments

Comments
 (0)