diff --git a/Formula/labeler.rb b/Formula/labeler.rb index ec6f555..31c893a 100644 --- a/Formula/labeler.rb +++ b/Formula/labeler.rb @@ -1,7 +1,7 @@ class Labeler < Formula desc "Utility that automates the labeling of resources output from kubectl, kustomize, and helm" homepage "https://github.com/clubanderson/labeler" - url "https://github.com/clubanderson/labeler/releases/download/v0.15.0/labeler" + url "https://github.com/clubanderson/labeler/releases/download/v0.16.0/labeler" # sha256 "26c5d47adbd0ed7d0a0d9f8a33a25bc242f7cdff2a661d8d6211f5279ca995d4" def install diff --git a/pkg/src/labeler-helpers.go b/pkg/src/labeler-helpers.go index 3d01060..334ad6c 100644 --- a/pkg/src/labeler-helpers.go +++ b/pkg/src/labeler-helpers.go @@ -8,6 +8,7 @@ import ( "log" "os" "os/exec" + "os/user" "path/filepath" "reflect" "regexp" @@ -197,7 +198,7 @@ func (p ParamsStruct) runHelmInTemplateMode(args []string) []byte { log.Printf("labeler.go: [debug] modified command components: %v\n", modifiedCommandComponents) } - output, err := p.runCmd("helm", modifiedCommandComponents) + output, err := p.runCmd("helm", modifiedCommandComponents, true) if err != nil { // log.Println("labeler.go: error (run helm):", err) os.Exit(1) @@ -395,7 +396,7 @@ func (p ParamsStruct) createCachedDiscoveryClient(restConfigCoreOrWds rest.Confi func (p ParamsStruct) useContext(contextName string) { setContext := []string{"config", "use-context", contextName} - _, err := p.runCmd("kubectl", setContext) + _, err := p.runCmd("kubectl", setContext, false) if err != nil { // log.Printf(" 🔴 error setting kubeconfig's current context: %v\n", err) } else { @@ -429,14 +430,36 @@ func DecodeYAML(yamlBytes []byte) (*unstructured.Unstructured, error) { return obj, nil } -func (p ParamsStruct) runCmd(cmdToRun string, cmdArgs []string) ([]byte, error) { +func expandTilde(args []string) []string { + for i, arg := range args { + if strings.Contains(arg, "~") { + usr, err := user.Current() + if err != nil { + log.Printf("Error getting current user: %v\n", err) + return args + } + args[i] = strings.ReplaceAll(args[i], "~", usr.HomeDir) + } + } + return args +} + +func (p ParamsStruct) runCmd(cmdToRun string, cmdArgs []string, suppressOutput bool) ([]byte, error) { + cmdArgs = expandTilde(cmdArgs) + cmd := exec.Command(cmdToRun, cmdArgs...) cmd.Env = append(cmd.Env, "PATH="+p.path) cmd.Env = append(cmd.Env, "HOME="+p.homeDir) + cmd.Env = append(cmd.Env, "KUBECONFIG="+os.Getenv("KUBECONFIG")) var outputBuf bytes.Buffer - cmd.Stdout = io.MultiWriter(&outputBuf) - cmd.Stderr = io.MultiWriter(&outputBuf) + if suppressOutput { + cmd.Stdout = io.MultiWriter(&outputBuf) + } else { + cmd.Stdout = io.MultiWriter(&outputBuf, os.Stdout) + } + cmd.Stderr = io.MultiWriter(&outputBuf, os.Stderr) + cmd.Stdin = os.Stdin err := cmd.Start() if err != nil { @@ -444,11 +467,11 @@ func (p ParamsStruct) runCmd(cmdToRun string, cmdArgs []string) ([]byte, error) return nil, err } - // err = cmd.Wait() - // if err != nil { - // // log.Println("labeler.go: error waiting for command to complete:", err) - // log.Printf(string(outputBuf.Bytes())) - // return nil, err - // } + err = cmd.Wait() + if err != nil { + // log.Println("labeler.go: error waiting for command to complete:", err) + // log.Printf(string(outputBuf.Bytes())) + return nil, err + } return outputBuf.Bytes(), nil } diff --git a/pkg/src/labeler-piped.go b/pkg/src/labeler-piped.go index 74ab786..a71a46d 100644 --- a/pkg/src/labeler-piped.go +++ b/pkg/src/labeler-piped.go @@ -99,7 +99,7 @@ func (p ParamsStruct) helmOrKubectl(r io.Reader, w io.Writer, input []string) er modifiedCommandComponents := append(strings.Split(modifiedCommand, " ")[1:]) // log.Printf("labeler.go: modified command: %q\n", modifiedCommand) // log.Printf("labeler.go: modified command components: %q\n", modifiedCommandComponents) - output, err := p.runCmd("helm", modifiedCommandComponents) + output, err := p.runCmd("helm", modifiedCommandComponents, true) if err != nil { // log.Println("labeler.go: error (running helm):", err) os.Exit(1) diff --git a/pkg/src/labeler-plugin-bp-creator.go b/pkg/src/labeler-plugin-bp-creator.go index 2da9563..e716cb8 100644 --- a/pkg/src/labeler-plugin-bp-creator.go +++ b/pkg/src/labeler-plugin-bp-creator.go @@ -114,7 +114,7 @@ func (p ParamsStruct) PluginCreateBP(reflect bool) []string { log.Printf(" 🚀 Attempting to create %v object %q in WDS namespace %q", k, n, p.params[nsArg]) p.createObjForPlugin(gvk, yamlData, n, r, p.params["namespaceArg"]) } else { - fmt.Println(string(yamlData)) + fmt.Printf("%v", string(yamlData)) } return []string{} } diff --git a/pkg/src/labeler-plugin-ocm-creator.go b/pkg/src/labeler-plugin-ocm-creator.go index 0abb30f..7cff332 100644 --- a/pkg/src/labeler-plugin-ocm-creator.go +++ b/pkg/src/labeler-plugin-ocm-creator.go @@ -90,7 +90,7 @@ func (p ParamsStruct) PluginCreateMW(reflect bool) []string { log.Printf(" 🚀 Attempting to create %v object %q in namespace %q", k, n, p.params["namespaceArg"]) p.createObjForPlugin(gvk, yamlData, n, r, p.params["namespaceArg"]) } else { - fmt.Println(string(yamlData)) + fmt.Printf("%v", string(yamlData)) } return []string{} } diff --git a/pkg/src/labeler-plugin-remote-deploy.go b/pkg/src/labeler-plugin-remote-deploy.go index dc8aa49..251ee91 100644 --- a/pkg/src/labeler-plugin-remote-deploy.go +++ b/pkg/src/labeler-plugin-remote-deploy.go @@ -39,7 +39,7 @@ func (p ParamsStruct) PluginRemoteDeployTo(reflect bool) []string { log.Printf("labeler.go: [debug] modified command components: %v\n", modifiedCommand) } - output, err := p.runCmd("kubectl", modifiedCommand[1:]) + output, err := p.runCmd("kubectl", modifiedCommand[1:], false) if err != nil { log.Println(err) } else { @@ -70,7 +70,7 @@ func (p ParamsStruct) PluginRemoteDeployTo(reflect bool) []string { log.Printf("labeler.go: [debug] modified command components: %v\n", modifiedCommand) } - output, err := p.runCmd("helm", modifiedCommand[1:]) + output, err := p.runCmd("helm", modifiedCommand[1:], false) if err != nil { log.Println(err) } else { diff --git a/pkg/src/labeler.go b/pkg/src/labeler.go index a31b39d..fff2cc5 100755 --- a/pkg/src/labeler.go +++ b/pkg/src/labeler.go @@ -4,7 +4,6 @@ import ( "fmt" "log" "os" - "os/exec" "os/user" "reflect" "strings" @@ -16,7 +15,7 @@ import ( "k8s.io/client-go/rest" ) -var version = "0.15.0" +var version = "0.16.0" type ResourceStruct struct { Group string @@ -30,6 +29,7 @@ type ParamsStruct struct { homeDir string path string originalCmd string + kubeconfig string ClientSet *kubernetes.Clientset RestConfig *rest.Config DynamicClient *dynamic.DynamicClient @@ -209,8 +209,8 @@ func (p ParamsStruct) aliasRun(args []string) error { if p.flags["l-debug"] { log.Println("labeler.go: [debug] before args: ", args) } - // Run the command with the parsed flags + // Run the command with the parsed flags if args[0] == "k" || args[0] == "kubectl" { if p.flags["l-debug"] { log.Println("labeler.go: [debug] after args: ", args) @@ -219,13 +219,12 @@ func (p ParamsStruct) aliasRun(args []string) error { originalCommand := strings.Join(args, " ") p.originalCmd = originalCommand - cmd := exec.Command(args[0], args[1:]...) - out, err := cmd.CombinedOutput() + // cmd := exec.Command(args[0], args[1:]...) + out, err := p.runCmd(args[0], args[1:], false) + // out, err := cmd.CombinedOutput() if err != nil { - fmt.Printf("%v", string(out)) + // fmt.Printf("%v", string(out)) os.Exit(1) - } else { - fmt.Printf("%v", string(out)) } p.ClientSet, p.RestConfig, p.DynamicClient = p.switchContext() @@ -235,12 +234,10 @@ func (p ParamsStruct) aliasRun(args []string) error { } else if args[0] == "helm" { // run the original helm command without the extra labeler flags - output, err := p.runCmd("helm", args[1:]) + _, err := p.runCmd("helm", args[1:], false) if err != nil { - log.Println(err) + // log.Println(err) os.Exit(1) - } else { - fmt.Printf("%v", string(output)) } // now run helm as template and label the output @@ -277,6 +274,7 @@ func (p ParamsStruct) aliasRun(args []string) error { v := strings.Split(vCSV, ",") if key == v[0] { if p.pluginPtrs[pkey].IsValid() { + log.Printf("\nlabeler plugin: %q:\n\n", pkey) p.pluginPtrs[pkey].Call(fnArgs) } }