Skip to content

Commit

Permalink
Rename render to inspect and make install properly function
Browse files Browse the repository at this point in the history
  • Loading branch information
nhh committed Sep 28, 2024
1 parent 8a63f81 commit 4de93b9
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 107 deletions.
124 changes: 18 additions & 106 deletions cmd/render.go → cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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)
},
}
5 changes: 4 additions & 1 deletion cmd/install.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
},
}
111 changes: 111 additions & 0 deletions internal/k8s/helper.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 4de93b9

Please sign in to comment.