Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 23 additions & 39 deletions tui/attributeList.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,13 @@ import (
)

type AttributeList struct {
list list.Model
width int
sdk handlers.Handler
list list.Model
sdk handlers.Handler
}

type AttributeItem struct {
id string
namespace string
name string
description string
rule string
values []string
title string
id string
name string
}

func (m AttributeItem) FilterValue() string {
Expand All @@ -34,15 +28,14 @@ func (m AttributeItem) Title() string {
}

func (m AttributeItem) Description() string {
return m.description
return m.id
}

func InitAttributeList(id string, sdk handlers.Handler) (tea.Model, tea.Cmd) {
m := AttributeList{sdk: sdk}
m.list = list.New([]list.Item{}, list.NewDefaultDelegate(), constants.WindowSize.Width, constants.WindowSize.Height)
l := list.New([]list.Item{}, list.NewDefaultDelegate(), constants.WindowSize.Width, constants.WindowSize.Height)
res, err := sdk.ListAttributes(common.ActiveStateEnum_ACTIVE_STATE_ENUM_ANY)
if err != nil {
return m, nil
// return error view
}
var attrs []list.Item
selectIdx := 0
Expand All @@ -55,17 +48,15 @@ func InitAttributeList(id string, sdk handlers.Handler) (tea.Model, tea.Cmd) {
selectIdx = i
}
item := AttributeItem{
id: attr.Id,
namespace: attr.Namespace.Name,
name: attr.Name,
rule: attr.Rule.String(),
values: vals,
id: attr.Id,
name: attr.Name,
}
attrs = append(attrs, item)
}
m.list.Title = "Attributes"
m.list.SetItems(attrs)
m.list.Select(selectIdx)
l.Title = "Attributes"
l.SetItems(attrs)
l.Select(selectIdx)
m := AttributeList{sdk: sdk, list: l}
return m.Update(WindowMsg())
}

Expand All @@ -92,7 +83,6 @@ func (m AttributeList) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.WindowSizeMsg:
constants.WindowSize = msg
m.list.SetSize(msg.Width, msg.Height)
m.width = msg.Width
return m, nil
case tea.KeyMsg:
switch msg.String() {
Expand All @@ -103,29 +93,23 @@ func (m AttributeList) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// make enum for Attributes idx in AppMenu
am.list.Select(0)
return am.Update(WindowMsg())
case "down", "j":
if m.list.Index() < len(m.list.Items())-1 {
m.list.Select(m.list.Index() + 1)
}
case "up", "k":
if m.list.Index() > 0 {
m.list.Select(m.list.Index() - 1)
}
// case "c":
// create new attribute
// return InitAttributeView(m.list.Items(), len(m.list.Items()))
case "enter", "e":
return InitAttributeView(m.list.Items()[m.list.Index()].(AttributeItem).id, m.sdk)
case "ctrl+d":
m.list.RemoveItem(m.list.Index())
newIndex := m.list.Index() - 1
if newIndex < 0 {
newIndex = 0
}
m.list.Select(newIndex)
// case "ctrl+d":
// m.list.RemoveItem(m.list.Index())
// newIndex := m.list.Index() - 1
// if newIndex < 0 {
// newIndex = 0
// }
// m.list.Select(newIndex)
}
}
return m, nil
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}

func (m AttributeList) View() string {
Expand Down
36 changes: 21 additions & 15 deletions tui/attributeView.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/opentdf/otdfctl/pkg/cli"
"github.com/opentdf/otdfctl/pkg/handlers"
"github.com/opentdf/otdfctl/tui/constants"
"github.com/opentdf/platform/protocol/go/policy"
)

type AttributeSubItem struct {
Expand All @@ -26,31 +27,30 @@ func (m AttributeSubItem) Description() string {
}

type AttributeView struct {
attr *policy.Attribute
read Read
sdk handlers.Handler
}

func InitAttributeView(id string, h handlers.Handler) (AttributeView, tea.Cmd) {
m := AttributeView{}
attr, err := h.GetAttribute(id)
if err != nil {
return m, nil
}
var vals []string
for _, val := range attr.Values {
vals = append(vals, val.Value)
// return error view
}
sa := cli.GetSimpleAttribute(attr)
items := []list.Item{
AttributeSubItem{title: "ID", description: attr.Id},
AttributeSubItem{title: "Name", description: attr.Name},
AttributeSubItem{title: "Rule", description: attr.Rule.String()},
AttributeSubItem{title: "Values", description: cli.CommaSeparated(vals)},
AttributeSubItem{title: "Namespace", description: attr.Namespace.Name},
AttributeSubItem{title: "Created At", description: attr.Metadata.CreatedAt.String()},
AttributeSubItem{title: "Updated At", description: attr.Metadata.UpdatedAt.String()},
AttributeSubItem{title: "ID", description: sa.Id},
AttributeSubItem{title: "Name", description: sa.Name},
AttributeSubItem{title: "Rule", description: sa.Rule},
AttributeSubItem{title: "Values", description: cli.CommaSeparated(sa.Values)},
AttributeSubItem{title: "Namespace", description: sa.Namespace},
AttributeSubItem{title: "Active", description: sa.Active},
AttributeSubItem{title: "Labels", description: sa.Metadata["Labels"]},
AttributeSubItem{title: "Created At", description: sa.Metadata["Created At"]},
AttributeSubItem{title: "Updated At", description: sa.Metadata["Updated At"]},
}

model, _ := InitRead("Read Attribute", items)
m.read = model.(Read)
m := AttributeView{sdk: h, attr: attr, read: model.(Read)}
model, msg := m.Update(WindowMsg())
m = model.(AttributeView)
return m, msg
Expand All @@ -68,10 +68,16 @@ func (m AttributeView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
case tea.KeyMsg:
switch msg.String() {
case "backspace":
return InitAttributeList(m.attr.Id, m.sdk)
case "ctrl+c", "q":
return m, tea.Quit
case "ctrl+d":
return m, nil
case "enter":
if m.read.list.SelectedItem().(AttributeSubItem).title == "Labels" {
return InitLabelList(m.attr, m.sdk)
}
// case "enter":
// switch m.list.SelectedItem().(AttributeItem).id {
// // case namespaceMenu:
Expand Down
80 changes: 80 additions & 0 deletions tui/labelList.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package tui

import (
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/opentdf/otdfctl/pkg/handlers"
"github.com/opentdf/otdfctl/tui/constants"
"github.com/opentdf/platform/protocol/go/policy"
)

type LabelList struct {
attr *policy.Attribute
sdk handlers.Handler
read Read
}

type LabelItem struct {
title string
description string
}

func (m LabelItem) FilterValue() string {
return m.title
}

func (m LabelItem) Title() string {
return m.title
}

func (m LabelItem) Description() string {
return m.description
}

func InitLabelList(attr *policy.Attribute, sdk handlers.Handler) (tea.Model, tea.Cmd) {
labels := attr.Metadata.Labels
var items []list.Item
for k, v := range labels {
item := LabelItem{
title: k,
description: v,
}
items = append(items, item)
}
model, _ := InitRead("Read Labels", items)
return LabelList{attr: attr, sdk: sdk, read: model.(Read)}, nil
}

func (m LabelList) Init() tea.Cmd {
return nil
}

func (m LabelList) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
constants.WindowSize = msg
m.read.list.SetSize(msg.Width, msg.Height)
return m, nil
case tea.KeyMsg:
switch msg.String() {
case "backspace":
return InitAttributeView(m.attr.Id, m.sdk)
case "ctrl+c", "q", "esc":
return m, tea.Quit
case "enter", "e":
return InitLabelUpdate(m.read.list.Items()[m.read.list.Index()].(LabelItem), m.attr, m.sdk), nil
case "c":
return InitLabelUpdate(LabelItem{}, m.attr, m.sdk), nil
case "d":
// delete label
return m, nil
}
}
var cmd tea.Cmd
m.read.list, cmd = m.read.list.Update(msg)
return m, cmd
}

func (m LabelList) View() string {
return ViewList(m.read.list)
}
61 changes: 61 additions & 0 deletions tui/labelUpdate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package tui

import (
tea "github.com/charmbracelet/bubbletea"
"github.com/opentdf/otdfctl/pkg/handlers"
"github.com/opentdf/platform/protocol/go/common"
"github.com/opentdf/platform/protocol/go/policy"
)

type LabelUpdate struct {
label LabelItem
update Update
attr *policy.Attribute
sdk handlers.Handler
}

func InitLabelUpdate(label LabelItem, attr *policy.Attribute, sdk handlers.Handler) LabelUpdate {
return LabelUpdate{
label: label,
update: InitUpdate([]string{"Key", "Value"}, []string{label.title, label.description}),
attr: attr,
sdk: sdk,
}
}

func (m LabelUpdate) Init() tea.Cmd {
return nil
}

func (m LabelUpdate) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "enter":
if m.update.focusIndex == len(m.update.inputs) {
// update the label
metadata := &common.MetadataMutable{Labels: m.attr.Metadata.Labels}
oldKey := m.label.title
newKey := m.update.inputs[0].Value()
newVal := m.update.inputs[1].Value()
if oldKey != newKey {
delete(metadata.Labels, oldKey)
}
metadata.Labels[newKey] = newVal
behavior := common.MetadataUpdateEnum_METADATA_UPDATE_ENUM_REPLACE
attr, err := m.sdk.UpdateAttribute(m.attr.Id, metadata, behavior)
if err != nil {
// return error view
}
return InitLabelList(attr, m.sdk)
}
}
}
update, cmd := m.update.Update(msg)
m.update = update.(Update)
return m, cmd
}

func (m LabelUpdate) View() string {
return m.update.View()
}
Loading