From 4de93b9e1323e0558779dadf8c2134d537b3c2d8 Mon Sep 17 00:00:00 2001 From: nhh Date: Sat, 28 Sep 2024 14:10:47 +0200 Subject: [PATCH] Rename render to inspect and make install properly function --- cmd/{render.go => inspect.go} | 124 +++++----------------------------- cmd/install.go | 5 +- internal/k8s/helper.go | 111 ++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 107 deletions(-) rename cmd/{render.go => inspect.go} (57%) create mode 100644 internal/k8s/helper.go diff --git a/cmd/render.go b/cmd/inspect.go similarity index 57% rename from cmd/render.go rename to cmd/inspect.go index 3827ebd..e650370 100644 --- a/cmd/render.go +++ b/cmd/inspect.go @@ -6,14 +6,11 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/glamour" "github.com/charmbracelet/lipgloss" + "github.com/kubernetix/k8x/v1/internal/k8s" "github.com/kubernetix/k8x/v1/internal/ts" "github.com/spf13/cobra" - "gopkg.in/yaml.v3" - "log" "os" - "os/exec" "strings" - "time" ) // You generally won't need this unless you're processing stuff with @@ -129,18 +126,13 @@ func max(a, b int) int { return b } -var ( - Interactive bool -) - func init() { - render.PersistentFlags().BoolVarP(&Interactive, "interactive", "i", false, "Interactively show rendered json/yaml") - rootCmd.AddCommand(render) + rootCmd.AddCommand(inspect) } -var render = &cobra.Command{ - Use: "render", - Short: "Render a chart file as yaml or json", +var inspect = &cobra.Command{ + Use: "inspect", + Short: "Interactively inspect a chart file", Run: func(cmd *cobra.Command, args []string) { if len(args) == 0 { _ = cmd.Help() @@ -152,103 +144,23 @@ var render = &cobra.Command{ code := ts.Load(path, Verbose) export := ts.Run(code, path) - content := []string{""} - - for _, component := range export["components"].([]interface{}) { - if component == nil { - continue - } - bts, _ := yaml.Marshal(component) - content = append(content, string(bts)) - } - - namespace := export["namespace"] - - if Interactive { - // Append auto generated namespace, Todo handle if namespace is undefined/null/"" - if hasValidNamespace(namespace) { - nsyml, _ := yaml.Marshal(namespace) - content = append(content, string(nsyml)) - } - - md := fmt.Sprintf("```yml%s```", strings.Join(content, "---\n")) + kubectlYaml := k8s.PatchAndTransform(export) - out, _ := glamour.Render(md, "dark") + md := fmt.Sprintf("```yml%s```", kubectlYaml.Combined()) - p := tea.NewProgram( - model{content: out}, - tea.WithAltScreen(), // use the full size of the terminal in its "alternate screen buffer" - tea.WithMouseCellMotion(), // turn on mouse support so we can track the mouse wheel - ) + out, _ := glamour.Render(md, "dark") - if _, err := p.Run(); err != nil { - fmt.Println("could not run program:", err) - os.Exit(1) - } + p := tea.NewProgram( + model{content: out}, + tea.WithAltScreen(), // use the full size of the terminal in its "alternate screen buffer" + tea.WithMouseCellMotion(), // turn on mouse support so we can track the mouse wheel + ) - os.Exit(0) - } else { - // create and open a temporary file - f, err := os.CreateTemp("", "k8x-tmpfile-") // in Go version older than 1.17 you can use ioutil.TempFile - if err != nil { - log.Fatal(err) - } - // close and remove the temporary file at the end of the program - defer f.Close() - defer os.Remove(f.Name()) - - if hasValidNamespace(namespace) { - // Writing the namespace template to the file, apply it, wait 5 seconds - ns, err := yaml.Marshal(namespace) - if _, err := f.Write(ns); err != nil { - log.Fatal(err) - } - - //fileOutput, _ := os.ReadFile(f.Name()) - //fmt.Println(string(fileOutput)) - - grepCmd := exec.Command("kubectl", "apply", "-f", f.Name()) - - output, _ := grepCmd.Output() - fmt.Print(string(output)) - - if strings.Contains(string(output), "created") { - time.Sleep(1 * time.Second) - } - - // Reset file - err = f.Truncate(0) - if err != nil { - return - } - - _, err = f.Seek(0, 0) - } - - // Write chart - if _, err := f.Write([]byte(strings.Join(content, "---\n"))); err != nil { - log.Fatal(err) - } - - //fileOutput, _ = os.ReadFile(f.Name()) - //fmt.Println(string(fileOutput)) - - grepCmd := exec.Command("kubectl", "apply", "-f", f.Name()) - - output, _ := grepCmd.Output() - fmt.Println(string(output)) + if _, err := p.Run(); err != nil { + fmt.Println("could not run program:", err) + os.Exit(1) } - }, -} - -func hasValidNamespace(namespace interface{}) bool { - if namespace == nil { - return false - } - - if namespace == "" { - return false - } - return true + os.Exit(0) + }, } diff --git a/cmd/install.go b/cmd/install.go index 9cd98fc..0cece50 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/kubernetix/k8x/v1/internal/k8s" "github.com/kubernetix/k8x/v1/internal/ts" "github.com/spf13/cobra" "os" @@ -25,6 +26,8 @@ var install = &cobra.Command{ path := args[0] code := ts.Load(path, Verbose) - _ = ts.Run(code, path) + export := ts.Run(code, path) + chart := k8s.PatchAndTransform(export) + k8s.ApplyChart(chart) }, } diff --git a/internal/k8s/helper.go b/internal/k8s/helper.go new file mode 100644 index 0000000..caff072 --- /dev/null +++ b/internal/k8s/helper.go @@ -0,0 +1,111 @@ +package k8s + +import ( + "fmt" + "gopkg.in/yaml.v3" + "log" + "os" + "os/exec" + "strings" + "time" +) + +type Chart struct { + Namespace string + Content string +} + +func HasValidNamespace(namespace interface{}) bool { + if namespace == nil { + return false + } + + if namespace == "" { + return false + } + + return true +} + +func PatchAndTransform(export map[string]interface{}) Chart { + content := []string{""} + + chart := Chart{} + + for _, component := range export["components"].([]interface{}) { + if component == nil { + continue + } + bts, _ := yaml.Marshal(component) + content = append(content, string(bts)) + } + + chart.Content = strings.Join(content, "---\n") + + namespace := export["namespace"] + + if HasValidNamespace(namespace) { + nsyml, _ := yaml.Marshal(namespace) + chart.Namespace = string(nsyml) + } + + return chart +} + +// Todo add error handling +func ApplyChart(chart Chart) { + // create and open a temporary file + f, err := os.CreateTemp("", "k8x-tmpfile-") // in Go version older than 1.17 you can use ioutil.TempFile + if err != nil { + log.Fatal(err) + } + // close and remove the temporary file at the end of the program + defer f.Close() + defer os.Remove(f.Name()) + + if chart.Namespace == "" { + if _, err := f.Write([]byte(chart.Namespace)); err != nil { + log.Fatal(err) + } + + //fileOutput, _ := os.ReadFile(f.Name()) + //fmt.Println(string(fileOutput)) + + grepCmd := exec.Command("kubectl", "apply", "-f", f.Name()) + + output, _ := grepCmd.Output() + fmt.Print(string(output)) + + if strings.Contains(string(output), "created") { + time.Sleep(1 * time.Second) + } + + // Reset file + err = f.Truncate(0) + if err != nil { + return + } + + _, err = f.Seek(0, 0) + } + + // Write chart + if _, err := f.Write([]byte(chart.Content)); err != nil { + log.Fatal(err) + } + + //fileOutput, _ = os.ReadFile(f.Name()) + //fmt.Println(string(fileOutput)) + + grepCmd := exec.Command("kubectl", "apply", "-f", f.Name()) + + output, _ := grepCmd.Output() + fmt.Println(string(output)) +} + +func (chart *Chart) Combined() string { + arr := make([]string, 2) + arr[0] = chart.Namespace + arr[1] = chart.Content + return strings.Join(arr, "---\n") +}