#! /bin/sh

set -e

if [ ! -f $1 ];then
    echo "Usage $0 helm-release.yaml"
    exit 1
fi

JSON=$(kubectl apply -f $1 --dry-run=client -o json)

RELEASE=$(echo $JSON|jq -r '.spec.releaseName')
NAMESPACE=$(echo $JSON|jq -r '.metadata.namespace')
REPO=$(echo $JSON|jq -r '.spec.chart.repository')
CHART=$(echo $JSON|jq -r '.spec.chart.name')
VERSION=$(echo $JSON|jq -r '.spec.chart.version')
VALUES=$(echo $JSON|jq -r '.spec.values')

set -x

echo $VALUES | helm upgrade --install "$RELEASE" "$CHART" \
    --version "$VERSION" --repo "$REPO" \
    --namespace "$NAMESPACE" -f -
