Skip to content

Commit

Permalink
(techdebt): make table report errors (#21)
Browse files Browse the repository at this point in the history
and other small improvements

Signed-off-by: Bryce Palmer <everettraven@gmail.com>
  • Loading branch information
everettraven committed Nov 14, 2023
1 parent 068eacc commit 46f80c7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
8 changes: 8 additions & 0 deletions pkg/charm/models/panels/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Table struct {
mutex *sync.Mutex
rows map[types.UID]tbl.Row
columns []buoytypes.Column
err error
}

func NewTable(name string, table tbl.Model, columns []buoytypes.Column) *Table {
Expand All @@ -42,6 +43,9 @@ func (m *Table) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m *Table) View() string {
if m.err != nil {
return m.err.Error()
}
return m.table.View()
}

Expand Down Expand Up @@ -74,3 +78,7 @@ func (m *Table) Columns() []buoytypes.Column {
func (m *Table) Name() string {
return m.name
}

func (m *Table) SetError(err error) {
m.err = err
}
25 changes: 8 additions & 17 deletions pkg/paneler/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"strings"
"sync"

"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -39,7 +38,7 @@ func modelWrapperForLogPanel(kc *kubernetes.Clientset, logsPanel types.Logs) *pa
return vpw
}

func streamLogs(kc *kubernetes.Clientset, logsPanel types.Logs, logItem *panels.Logs) error {
func streamLogs(kc *kubernetes.Clientset, logsPanel types.Logs, logItem *panels.Logs) {
//TODO: expand this beyond just a pod
req := kc.CoreV1().Pods(logsPanel.Key.Namespace).GetLogs(logsPanel.Key.Name, &v1.PodLogOptions{
Container: logsPanel.Container,
Expand All @@ -48,24 +47,16 @@ func streamLogs(kc *kubernetes.Clientset, logsPanel types.Logs, logItem *panels.

rc, err := req.Stream(context.Background())
if err != nil {
return fmt.Errorf("fetching logs for %s/%s: %w", logsPanel.Key.Namespace, logsPanel.Key.Name, err)
logItem.AddContent(fmt.Errorf("fetching logs for %s/%s: %w", logsPanel.Key.Namespace, logsPanel.Key.Name, err).Error())
return
}
defer rc.Close()

wg := sync.WaitGroup{}
wg.Add(1)

go func() {
defer wg.Done()
scanner := bufio.NewScanner(rc)
for scanner.Scan() {
logs := wrapLogs(scanner.Bytes())
logItem.AddContent(logs)
}
}()

wg.Wait()
return nil
scanner := bufio.NewScanner(rc)
for scanner.Scan() {
logs := wrapLogs(scanner.Bytes())
logItem.AddContent(logs)
}
}

func wrapLogs(logs []byte) string {
Expand Down
16 changes: 8 additions & 8 deletions pkg/paneler/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,17 @@ func (t *Table) runInformerForTable(tablePanel buoytypes.Table, tw *panels.Table
for _, column := range tw.Columns() {
val, err := getDotNotationValue(u.Object, column.Path)
if err != nil {
//TODO: Log some kind of info here
continue
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 {
//TODO: log some kind of info here
continue
tw.SetError(err)
break
}
row = append(row, string(data))
default:
Expand All @@ -123,17 +123,17 @@ func (t *Table) runInformerForTable(tablePanel buoytypes.Table, tw *panels.Table
for _, column := range tw.Columns() {
val, err := getDotNotationValue(u.Object, column.Path)
if err != nil {
//TODO: Log some kind of info here
continue
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 {
//TODO: log some kind of info here
continue
tw.SetError(err)
break
}
row = append(row, string(data))
default:
Expand Down

0 comments on commit 46f80c7

Please sign in to comment.