Skip to content

Commit e6f44e0

Browse files
authored
feat(tui): abstract away read view (#141)
1 parent 72c1b4c commit e6f44e0

File tree

6 files changed

+161
-341
lines changed

6 files changed

+161
-341
lines changed

cmd/interactive.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"github.com/opentdf/otdfctl/pkg/cli"
45
"github.com/opentdf/otdfctl/pkg/man"
56
"github.com/opentdf/otdfctl/tui"
67
"github.com/spf13/cobra"
@@ -9,7 +10,8 @@ import (
910
func init() {
1011
cmd := man.Docs.GetCommand("interactive",
1112
man.WithRun(func(cmd *cobra.Command, args []string) {
12-
tui.StartTea()
13+
h := cli.NewHandler(cmd)
14+
tui.StartTea(h)
1315
}),
1416
)
1517
RootCmd.AddCommand(&cmd.Command)

tui/appMenu.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tui
33
import (
44
"github.com/charmbracelet/bubbles/list"
55
tea "github.com/charmbracelet/bubbletea"
6+
"github.com/opentdf/otdfctl/pkg/handlers"
67
"github.com/opentdf/otdfctl/tui/constants"
78
)
89

@@ -38,20 +39,22 @@ func (m AppMenuItem) Description() string {
3839
type AppMenu struct {
3940
list list.Model
4041
view tea.Model
42+
sdk handlers.Handler
4143
}
4244

43-
func InitAppMenu() (AppMenu, tea.Cmd) {
45+
func InitAppMenu(h handlers.Handler) (AppMenu, tea.Cmd) {
4446
m := AppMenu{
4547
view: nil,
48+
sdk: h,
4649
}
4750
m.list = list.New([]list.Item{}, list.NewDefaultDelegate(), 8, 8)
4851
m.list.Title = "OpenTDF"
4952
m.list.SetItems([]list.Item{
50-
AppMenuItem{title: "Namespaces", description: "Manage namespaces", id: namespaceMenu},
53+
// AppMenuItem{title: "Namespaces", description: "Manage namespaces", id: namespaceMenu},
5154
AppMenuItem{title: "Attributes", description: "Manage attributes", id: attributeMenu},
52-
AppMenuItem{title: "Entitlements", description: "Manage entitlements", id: entitlementMenu},
53-
AppMenuItem{title: "Resource Encodings", description: "Manage resource encodings", id: resourceEncodingMenu},
54-
AppMenuItem{title: "Subject Encodings", description: "Manage subject encodings", id: subjectEncodingMenu},
55+
// AppMenuItem{title: "Entitlements", description: "Manage entitlements", id: entitlementMenu},
56+
// AppMenuItem{title: "Resource Encodings", description: "Manage resource encodings", id: resourceEncodingMenu},
57+
// AppMenuItem{title: "Subject Encodings", description: "Manage subject encodings", id: subjectEncodingMenu},
5558
})
5659
return m, func() tea.Msg { return nil }
5760
}
@@ -74,16 +77,13 @@ func (m AppMenu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
7477
return m, nil
7578
case "enter":
7679
switch m.list.SelectedItem().(AppMenuItem).id {
80+
// case namespaceMenu:
81+
// // get namespaces
82+
// nl, cmd := InitNamespaceList([]list.Item{}, 0)
83+
// return nl, cmd
7784
case attributeMenu:
78-
item := AttributeItem{
79-
id: "8a6755f2-efa8-4758-b893-af9a488e0bea",
80-
namespace: "demo.com",
81-
name: "relto",
82-
rule: "hierarchical",
83-
description: "The relto attribute is used to describe the relationship of the resource to the country of origin.",
84-
values: []string{"USA", "GBR"},
85-
}
86-
al, cmd := InitAttributeList([]list.Item{item}, 0)
85+
// list attributes
86+
al, cmd := InitAttributeList("", m.sdk)
8787
return al, cmd
8888
}
8989
}

tui/attributeList.go

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import (
44
"github.com/charmbracelet/bubbles/list"
55
tea "github.com/charmbracelet/bubbletea"
66
"github.com/charmbracelet/lipgloss"
7+
"github.com/opentdf/otdfctl/pkg/handlers"
78
"github.com/opentdf/otdfctl/tui/constants"
9+
"github.com/opentdf/platform/protocol/go/common"
810
)
911

1012
type AttributeList struct {
1113
list list.Model
1214
width int
15+
sdk handlers.Handler
1316
}
1417

1518
type AttributeItem struct {
@@ -19,6 +22,7 @@ type AttributeItem struct {
1922
description string
2023
rule string
2124
values []string
25+
title string
2226
}
2327

2428
func (m AttributeItem) FilterValue() string {
@@ -33,16 +37,35 @@ func (m AttributeItem) Description() string {
3337
return m.description
3438
}
3539

36-
func InitAttributeList(items []list.Item, selectIdx int) (tea.Model, tea.Cmd) {
37-
// TODO: fetch items from API
38-
39-
m := AttributeList{}
40+
func InitAttributeList(id string, sdk handlers.Handler) (tea.Model, tea.Cmd) {
41+
m := AttributeList{sdk: sdk}
4042
m.list = list.New([]list.Item{}, list.NewDefaultDelegate(), constants.WindowSize.Width, constants.WindowSize.Height)
41-
if selectIdx > 0 {
42-
m.list.Select(selectIdx)
43+
res, err := sdk.ListAttributes(common.ActiveStateEnum_ACTIVE_STATE_ENUM_ANY)
44+
if err != nil {
45+
return m, nil
46+
}
47+
var attrs []list.Item
48+
selectIdx := 0
49+
for i, attr := range res {
50+
var vals []string
51+
for _, val := range attr.Values {
52+
vals = append(vals, val.Value)
53+
}
54+
if attr.Id == id {
55+
selectIdx = i
56+
}
57+
item := AttributeItem{
58+
id: attr.Id,
59+
namespace: attr.Namespace.Name,
60+
name: attr.Name,
61+
rule: attr.Rule.String(),
62+
values: vals,
63+
}
64+
attrs = append(attrs, item)
4365
}
4466
m.list.Title = "Attributes"
45-
m.list.SetItems(items)
67+
m.list.SetItems(attrs)
68+
m.list.Select(selectIdx)
4669
return m.Update(WindowMsg())
4770
}
4871

@@ -76,8 +99,9 @@ func (m AttributeList) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
7699
case "ctrl+c", "q":
77100
return m, tea.Quit
78101
case "ctrl+[", "backspace":
79-
am, _ := InitAppMenu()
80-
am.list.Select(1)
102+
am, _ := InitAppMenu(m.sdk)
103+
// make enum for Attributes idx in AppMenu
104+
am.list.Select(0)
81105
return am.Update(WindowMsg())
82106
case "down", "j":
83107
if m.list.Index() < len(m.list.Items())-1 {
@@ -87,10 +111,11 @@ func (m AttributeList) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
87111
if m.list.Index() > 0 {
88112
m.list.Select(m.list.Index() - 1)
89113
}
90-
case "c":
91-
return InitAttributeView(m.list.Items(), len(m.list.Items()))
114+
// case "c":
115+
// create new attribute
116+
// return InitAttributeView(m.list.Items(), len(m.list.Items()))
92117
case "enter", "e":
93-
return InitAttributeView(m.list.Items(), m.list.Index())
118+
return InitAttributeView(m.list.Items()[m.list.Index()].(AttributeItem).id, m.sdk)
94119
case "ctrl+d":
95120
m.list.RemoveItem(m.list.Index())
96121
newIndex := m.list.Index() - 1

0 commit comments

Comments
 (0)