Skip to content

feat: Dedicated exit code (2) on there's diff #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ LDFLAGS := -X $(PKG)/cmd.Version=$(VERSION)
LDFLAGS += -X $(PKG)/vendor/k8s.io/helm/pkg/version.BuildMetadata=
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')

.PHONY: format
format:
test -z "$$(find . -path ./vendor -prune -type f -o -name '*.go' -exec gofmt -d {} + | tee /dev/stderr)" || \
test -z "$$(find . -path ./vendor -prune -type f -o -name '*.go' -exec gofmt -w {} + | tee /dev/stderr)"

.PHONY: install
install: build
mkdir -p $(HELM_HOME)/plugins/helm-diff/bin
Expand Down
6 changes: 6 additions & 0 deletions cmd/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cmd

type Error struct {
error
Code int
}
20 changes: 14 additions & 6 deletions cmd/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (
)

type revision struct {
release string
client helm.Interface
suppressedKinds []string
revisions []string
outputContext int
release string
client helm.Interface
detailedExitCode bool
suppressedKinds []string
revisions []string
outputContext int
}

const revisionCmdLongUsage = `
Expand Down Expand Up @@ -121,13 +122,20 @@ func (d *revision) differentiate() error {
return prettyError(err)
}

diff.DiffManifests(
seenAnyChanges := diff.DiffManifests(
manifest.ParseRelease(revisionResponse1.Release),
manifest.ParseRelease(revisionResponse2.Release),
d.suppressedKinds,
d.outputContext,
os.Stdout)

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

default:
return errors.New("Invalid Arguments")
}
Expand Down
21 changes: 15 additions & 6 deletions cmd/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import (
"os"
"strconv"

"errors"
"github.com/databus23/helm-diff/diff"
"github.com/databus23/helm-diff/manifest"
"github.com/spf13/cobra"
"k8s.io/helm/pkg/helm"
)

type rollback struct {
release string
client helm.Interface
suppressedKinds []string
revisions []string
outputContext int
release string
client helm.Interface
detailedExitCode bool
suppressedKinds []string
revisions []string
outputContext int
}

const rollbackCmdLongUsage = `
Expand Down Expand Up @@ -88,12 +90,19 @@ func (d *rollback) backcast() error {
}

// create a diff between the current manifest and the version of the manifest that a user is intended to rollback
diff.DiffManifests(
seenAnyChanges := diff.DiffManifests(
manifest.ParseRelease(releaseResponse.Release),
manifest.ParseRelease(revisionResponse.Release),
d.suppressedKinds,
d.outputContext,
os.Stdout)

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

return nil
}
31 changes: 17 additions & 14 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ import (
"os"
"strings"

"errors"
"github.com/databus23/helm-diff/diff"
"github.com/databus23/helm-diff/manifest"
"github.com/spf13/cobra"
"k8s.io/helm/pkg/helm"
"errors"
)

type diffCmd struct {
release string
chart string
chartVersion string
client helm.Interface
detailedExitCode bool
valueFiles valueFiles
values []string
reuseValues bool
resetValues bool
allowUnreleased bool
suppressedKinds []string
outputContext int
release string
chart string
chartVersion string
client helm.Interface
detailedExitCode bool
valueFiles valueFiles
values []string
reuseValues bool
resetValues bool
allowUnreleased bool
suppressedKinds []string
outputContext int
}

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

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

return nil
Expand Down
2 changes: 1 addition & 1 deletion diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package diff
import (
"fmt"
"io"
"strings"
"math"
"strings"

"github.com/aryann/difflib"
"github.com/mgutz/ansi"
Expand Down
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package main

import (
"os"
"fmt"
"os"

"github.com/databus23/helm-diff/cmd"
)

func main() {
if err := cmd.New().Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
switch e := err.(type) {
case cmd.Error:
os.Exit(e.Code)
default:
os.Exit(1)
}
}
}