diff --git a/README.md b/README.md index 35316bd..8611ab3 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,8 @@ kind create cluster The `test.json` file contains samples for each of the different panel types that `buoy` currently supports. As this is a hobby project very early in the development cycle there are some limitations and things are bound to not work as expected. +`buoy` uses https://github.com/tidwall/gjson for the path evaluation and extracting of values from resources. Please consult their documentation for valid path syntax + ## Controls - `ctrl+c`, `q`, `esc` will quit the program and exit the tui - `tab` will switch the active tab to the one to the right of the currently active tab diff --git a/go.mod b/go.mod index 6b695ff..a6fd072 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/charmbracelet/bubbletea v0.24.2 github.com/charmbracelet/lipgloss v0.7.1 github.com/spf13/cobra v1.7.0 + github.com/tidwall/gjson v1.17.0 k8s.io/api v0.28.1 k8s.io/apimachinery v0.28.1 k8s.io/client-go v0.28.1 @@ -15,7 +16,11 @@ require ( sigs.k8s.io/yaml v1.3.0 ) -require github.com/dlclark/regexp2 v1.4.0 // indirect +require ( + github.com/dlclark/regexp2 v1.4.0 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.0 // indirect +) require ( github.com/alecthomas/chroma v0.10.0 diff --git a/go.sum b/go.sum index 0579783..6304a98 100644 --- a/go.sum +++ b/go.sum @@ -132,6 +132,12 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM= +github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= diff --git a/pkg/paneler/helpers.go b/pkg/paneler/helpers.go index acf85c6..762c0de 100644 --- a/pkg/paneler/helpers.go +++ b/pkg/paneler/helpers.go @@ -1,20 +1,20 @@ package paneler import ( + "encoding/json" "fmt" - "strings" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "github.com/tidwall/gjson" ) func getDotNotationValue(item map[string]interface{}, dotPath string) (interface{}, error) { - keys := strings.Split(dotPath, ".") - val, exist, err := unstructured.NestedFieldNoCopy(item, keys...) + jsonBytes, err := json.Marshal(item) if err != nil { - return nil, fmt.Errorf("error fetching nested field %q: %w", dotPath, err) + return nil, fmt.Errorf("error marshalling item to json: %w", err) } - if !exist { + res := gjson.Get(string(jsonBytes), dotPath) + if !res.Exists() { return nil, fmt.Errorf("nested field %q not found", dotPath) } - return val, nil + return res.Value(), nil } diff --git a/pkg/paneler/table.go b/pkg/paneler/table.go index 2246107..018805e 100644 --- a/pkg/paneler/table.go +++ b/pkg/paneler/table.go @@ -96,19 +96,8 @@ func (t *Table) runInformerForTable(tablePanel buoytypes.Table, tw *panels.Table tw.SetError(err) break } - switch val := val.(type) { - case string: - row = append(row, val) - case map[string]interface{}: - data, err := json.Marshal(val) - if err != nil { - tw.SetError(err) - break - } - row = append(row, string(data)) - default: - row = append(row, fmt.Sprint(val)) - } + + row = append(row, fmt.Sprint(val)) } tw.AddOrUpdateRow(u.GetUID(), row) @@ -122,19 +111,7 @@ func (t *Table) runInformerForTable(tablePanel buoytypes.Table, tw *panels.Table tw.SetError(err) break } - switch val := val.(type) { - case string: - row = append(row, val) - case map[string]interface{}: - data, err := json.Marshal(val) - if err != nil { - tw.SetError(err) - break - } - row = append(row, string(data)) - default: - row = append(row, fmt.Sprint(val)) - } + row = append(row, fmt.Sprint(val)) } tw.AddOrUpdateRow(u.GetUID(), row) diff --git a/test.json b/test.json index 775de6d..b0d9d2c 100644 --- a/test.json +++ b/test.json @@ -16,20 +16,20 @@ "path": "metadata.name" }, { - "header": "Phase", - "path": "status.phase" + "header": "Labels", + "path": "metadata.labels" }, { - "header": "PodIP", - "path": "status.podIP" + "header": "Ready Condition", + "path": "status.conditions.#(type==Ready)" }, { - "header": "Start Time", - "path": "status.startTime" + "header": "Phase", + "path": "status.phase" }, { - "header": "UID", - "path": "metadata.uid" + "header": "Containers", + "path": "spec.containers.#.name" } ] },