Skip to content

Commit

Permalink
Update helm help snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
jlandowner committed Apr 29, 2024
1 parent 52375b5 commit d734532
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
8 changes: 4 additions & 4 deletions hack/helm-template-help-snapshot/helm-template.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Flags:
--insecure-skip-tls-verify skip tls certificate checks for the chart download
--is-upgrade set .Release.IsUpgrade instead of .Release.IsInstall
--key-file string identify HTTPS client using this SSL key file
--keyring string location of public keys used for verification (default \"/home/coder/.gnupg/pubring.gpg\")
--keyring string location of public keys used for verification (default \"###HOME_DIR###/.gnupg/pubring.gpg\")
--kube-version string Kubernetes version used for Capabilities.KubeVersion
-l, --labels stringToString Labels that would be added to release metadata. Should be divided by comma. (default [])
--name-template string specify template used to name the release
Expand Down Expand Up @@ -75,9 +75,9 @@ Global Flags:
--kubeconfig string path to the kubeconfig file
-n, --namespace string namespace scope for this request
--qps float32 queries per second used when communicating with the Kubernetes API, not including bursting
--registry-config string path to the registry config file (default \"/home/coder/.config/helm/registry/config.json\")
--repository-cache string path to the file containing cached repository indexes (default \"/home/coder/.cache/helm/repository\")
--repository-config string path to the file containing repository names and URLs (default \"/home/coder/.config/helm/repositories.yaml\")
--registry-config string path to the registry config file (default \"###HELM_REGISTRY_CONFIG###\")
--repository-cache string path to the file containing cached repository indexes (default \"###HELM_REPOSITORY_CACHE###\")
--repository-config string path to the file containing repository names and URLs (default \"###HELM_REPOSITORY_CONFIG###\")
"""
[helm-version]
Expand Down
40 changes: 39 additions & 1 deletion hack/helm-template-help-snapshot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log/slog"
"os"
"os/exec"
"regexp"

"github.com/jlandowner/helm-chartsnap/pkg/snap"
)
Expand All @@ -19,7 +20,29 @@ func execute(cmd ...string) string {
slog.Error("exec error", "err", err)
os.Exit(9)
}
return string(out)
return replaceHomeDir(replaceHelmEnv(string(out)))
}

func replaceHomeDir(bs string) string {
// Get os.UserHomeDir() and replace it with "###HOME_DIR###"
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
re := regexp.MustCompile(home)
return re.ReplaceAllString(bs, "###HOME_DIR###")
}

func replaceHelmEnv(bs string) string {
// Get helm env and replace HELM_REGISTRY_CONFIG, HELM_REPOSITORY_CACHE and HELM_REPOSITORY_CONFIG
envs := parseHelmEnvOutput()
re := regexp.MustCompile(envs["HELM_REGISTRY_CONFIG"])
bs = re.ReplaceAllString(bs, "###HELM_REGISTRY_CONFIG###")
re = regexp.MustCompile(envs["HELM_REPOSITORY_CACHE"])
bs = re.ReplaceAllString(bs, "###HELM_REPOSITORY_CACHE###")
re = regexp.MustCompile(envs["HELM_REPOSITORY_CONFIG"])
bs = re.ReplaceAllString(bs, "###HELM_REPOSITORY_CONFIG###")
return bs
}

func snapshot(id, data string) {
Expand All @@ -35,3 +58,18 @@ func snapshot(id, data string) {
os.Exit(1)
}
}

func parseHelmEnvOutput() map[string]string {
out, err := exec.Command("helm", "env").CombinedOutput()
if err != nil {
slog.Error("exec error", "err", err)
os.Exit(9)
}
re := regexp.MustCompile(`(.*)="(.*)"`)
matches := re.FindAllStringSubmatch(string(out), -1)
envs := make(map[string]string)
for _, match := range matches {
envs[match[1]] = match[2]
}
return envs
}

0 comments on commit d734532

Please sign in to comment.