Skip to content

Commit

Permalink
- Fix errors while while displaying ec2 via generic table viewer
Browse files Browse the repository at this point in the history
- Add wide column toggling
  • Loading branch information
RamanaReddy0M committed Feb 1, 2023
1 parent 1bbb7ad commit ddba394
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 34 deletions.
4 changes: 2 additions & 2 deletions internal/dao/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ func (e *EC2) List(ctx context.Context) ([]Object, error) {
//TODO: make dynamic
//session := ctx.Value(internal.KeySession).(*session.Session)
ins, err := aws.GetInstances(*session)
log.Info().Msg(fmt.Sprintf("ins: %+v", ins))
log.Info().Msg(fmt.Sprintf("ins: %d", len(ins)))

objs := make([]Object, len(ins))
for i, obj := range ins {
ins[i] = obj
objs[i] = obj
}
return objs, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/model/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (t *Table) Watch(ctx context.Context) error {
if err := t.refresh(ctx); err != nil {
return err
}
go t.updater(ctx)
//go t.updater(ctx)

return nil
}
Expand Down
14 changes: 7 additions & 7 deletions internal/render/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,29 @@ func (e EC2) Header() Header {
HeaderColumn{Name: "Instance-Id"},
HeaderColumn{Name: "Instance-State"},
HeaderColumn{Name: "Instance-Type"},
HeaderColumn{Name: "Availability-Zone"},
HeaderColumn{Name: "Public-DNS"},
HeaderColumn{Name: "Monitoring-State"},
HeaderColumn{Name: "Launch-Time"},
HeaderColumn{Name: "Public-DNS", Wide: true},
HeaderColumn{Name: "Availability-Zone"},
}
}

func (e EC2) Render(o interface{}, ns string, row *Row) error {
ec2Resp, ok := o.(*aws.EC2Resp)
ec2Resp, ok := o.(aws.EC2Resp)

if !ok {
return fmt.Errorf("Expected PodWithMetrics, but got %T", o)
return fmt.Errorf("Expected EC2Resp, but got %T", o)
}

row.ID = ns
row.Fields = Fields{
ec2Resp.InstanceId,
ec2Resp.InstanceState,
ec2Resp.InstanceType,
ec2Resp.AvailabilityZone,
ec2Resp.PublicDNS,
ec2Resp.MonitoringState,
ec2Resp.LaunchTime,
ec2Resp.LaunchTime,
ec2Resp.PublicDNS,
ec2Resp.AvailabilityZone,
}

return nil
Expand Down
1 change: 0 additions & 1 deletion internal/ui/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func NewMenu() *Menu {
m := Menu{
Table: tview.NewTable(),
}
m.SetBorder(true)
return &m
}

Expand Down
2 changes: 1 addition & 1 deletion internal/view/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,6 @@ func (a *App) tempLayout(ctx context.Context) {
go flash.Watch(ctx, a.Flash().Channel())

main := tview.NewFlex().SetDirection(tview.FlexRow)
main.SetBorder(true)

main.AddItem(a.statusIndicator(), 1, 1, false)
main.AddItem(a.Content, 0, 10, true)
Expand All @@ -873,6 +872,7 @@ func (a *App) tempLayout(ctx context.Context) {

//Testing only
a.inject(NewHelp(a))
a.inject(NewEC2("ec2"))
}

// QueueUpdateDraw queues up a ui action and redraw the ui.
Expand Down
25 changes: 10 additions & 15 deletions internal/view/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,24 @@ package view

import (
"context"
"fmt"
"sync"

"github.com/gdamore/tcell/v2"
"github.com/one2nc/cloud-lens/internal"
"github.com/one2nc/cloud-lens/internal/ui"
"github.com/rs/zerolog/log"
)

// Browser represents a generic resource browser.
type Browser struct {
*Table
context context.Context
cancelFn context.CancelFunc
mx sync.RWMutex
mx sync.RWMutex``
}

// NewBrowser returns a new browser.
func NewBrowser(resource string, ctx context.Context) ResourceViewer {
return &Browser{
Table: NewTable(resource),
Table: NewTable(resource),
context: ctx,
}
}
Expand All @@ -49,21 +46,21 @@ func (b *Browser) Init(ctx context.Context) error {

func (b *Browser) bindKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{
tcell.KeyEscape: ui.NewSharedKeyAction("Filter Reset", b.resetCmd, false),
tcell.KeyHelp: ui.NewSharedKeyAction("Help", b.helpCmd, false),
ui.KeyR: ui.NewSharedKeyAction("Filter Reset", b.resetCmd, false),
tcell.KeyHelp: ui.NewSharedKeyAction("Help", b.helpCmd, false),
})
}

// Start initializes browser updates.
func (b *Browser) Start() {
log.Info().Msg(fmt.Sprintf("59b:watch ctx type: %T", b.context.Value(internal.KeySession)))

b.Stop()
//b.GetModel().AddListener(b)
b.Table.Start()
if err := b.GetModel().Watch(b.context); err != nil {
b.App().Flash().Err(fmt.Errorf("Watcher failed for %s -- %w", b.Resource(), err))
}
b.Table.GetModel().Refresh(b.context)
b.Refresh()
// if err := b.GetModel().Watch(b.context); err != nil {
// b.App().Flash().Err(fmt.Errorf("Watcher failed for %s -- %w", b.Resource(), err))
// }
}

// Stop terminates browser updates.
Expand All @@ -85,7 +82,6 @@ func (b *Browser) Name() string { return b.Table.Resource() }

// SetContextFn populates a custom context.
func (b *Browser) SetContext(ctx context.Context) {
log.Info().Msg(fmt.Sprintf("set ctx type: %T", ctx.Value(internal.KeySession)))
b.context = ctx
}

Expand All @@ -98,7 +94,6 @@ func (b *Browser) helpCmd(evt *tcell.EventKey) *tcell.EventKey {
}

func (b *Browser) resetCmd(evt *tcell.EventKey) *tcell.EventKey {
b.Table.GetModel().Refresh(b.context)
b.Refresh()
return nil
return evt
}
8 changes: 5 additions & 3 deletions internal/view/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/gdamore/tcell/v2"
"github.com/one2nc/cloud-lens/internal"
"github.com/one2nc/cloud-lens/internal/config"
"github.com/one2nc/cloud-lens/internal/ui"
Expand All @@ -19,7 +20,7 @@ func NewEC2(resource string) ResourceViewer {
cfg, _ := config.Get()
session, _ := config.GetSession(cfg.Profiles[0], "ap-south-1", cfg.AwsConfig)
ctx := context.WithValue(context.Background(), internal.KeySession, session)

var e EC2
e.ResourceViewer = NewBrowser(resource, ctx)
e.AddBindKeysFn(e.bindKeys)
Expand All @@ -30,8 +31,9 @@ func NewEC2(resource string) ResourceViewer {
func (e *EC2) bindKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{
//ui.KeyShiftT: ui.NewKeyAction("Sort Restart", e.GetTable().SortColCmd("RESTARTS", false), false),
ui.KeyShiftT: ui.NewKeyAction("Sort Type", nil, true),
ui.KeyShiftL: ui.NewKeyAction("Sort Launch-Time", nil, true),
ui.KeyShiftT: ui.NewKeyAction("Sort Type", nil, false),
ui.KeyShiftL: ui.NewKeyAction("Sort Launch-Time", nil, false),
tcell.KeyEscape: ui.NewKeyAction("Back", e.App().PrevCmd, true),
})
}

Expand Down
4 changes: 1 addition & 3 deletions internal/view/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/gdamore/tcell/v2"
"github.com/one2nc/cloud-lens/internal/model"
"github.com/one2nc/cloud-lens/internal/ui"
"github.com/rs/zerolog/log"
)

const (
Expand Down Expand Up @@ -46,7 +45,6 @@ func NewHelp(app *App) *Help {

// Init initializes the component.
func (h *Help) Init(ctx context.Context) error {
log.Info().Msg("Init called: help")
if err := h.Table.Init(ctx); err != nil {
return err
}
Expand All @@ -69,7 +67,7 @@ func (h *Help) bindKeys() {
func (h *Help) build() {
h.Clear()

sections := []string{"RESOURCE", "GENERAL" ,"NAVIGATION"}
sections := []string{"RESOURCE", "GENERAL", "NAVIGATION"}
h.maxRows = len(h.showGeneral())
ff := []HelpFunc{
h.hints,
Expand Down
8 changes: 7 additions & 1 deletion internal/view/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ func (t *Table) keyboard(evt *tcell.EventKey) *tcell.EventKey {

func (t *Table) bindKeys() {
t.Actions().Add(ui.KeyActions{
ui.KeyHelp: ui.NewKeyAction("Help", t.App().helpCmd, true),
tcell.KeyCtrlW: ui.NewKeyAction("Toggle Wide", t.toggleWideCmd, true),
ui.KeyHelp: ui.NewKeyAction("Help", t.App().helpCmd, true),
})
}

Expand All @@ -87,3 +88,8 @@ func (t *Table) Name() string { return t.Table.Resource() }
func (t *Table) AddBindKeysFn(f BindKeysFunc) {
t.bindKeysFn = append(t.bindKeysFn, f)
}

func (t *Table) toggleWideCmd(evt *tcell.EventKey) *tcell.EventKey {
t.ToggleWide()
return nil
}

0 comments on commit ddba394

Please sign in to comment.