Skip to content

Commit

Permalink
Merge branch 'main' into feat/auth-dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
jakedoublev committed May 3, 2024
2 parents fe378e5 + e6f44e0 commit a1c5a17
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 344 deletions.
4 changes: 3 additions & 1 deletion cmd/interactive.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/opentdf/otdfctl/pkg/cli"
"github.com/opentdf/otdfctl/pkg/man"
"github.com/opentdf/otdfctl/tui"
"github.com/spf13/cobra"
Expand All @@ -9,7 +10,8 @@ import (
func init() {
cmd := man.Docs.GetCommand("interactive",
man.WithRun(func(cmd *cobra.Command, args []string) {
tui.StartTea()
h := cli.NewHandler(cmd)
tui.StartTea(h)
}),
)
RootCmd.AddCommand(&cmd.Command)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22.2
require (
github.com/adrg/frontmatter v0.2.0
github.com/charmbracelet/bubbles v0.18.0
github.com/charmbracelet/bubbletea v0.26.0
github.com/charmbracelet/bubbletea v0.26.1
github.com/charmbracelet/glamour v0.7.0
github.com/charmbracelet/huh v0.3.0
github.com/charmbracelet/lipgloss v0.10.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqy
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/39KLfy0=
github.com/charmbracelet/bubbles v0.18.0/go.mod h1:08qhZhtIwzgrtBjAcJnij1t1H0ZRjwHyGsy6AL11PSw=
github.com/charmbracelet/bubbletea v0.26.0 h1:LRS2uBclVfqh3gWBmU8uso2fXBsroW2Nb6HtAHfzbJI=
github.com/charmbracelet/bubbletea v0.26.0/go.mod h1:FzKr7sKoO8iFVcdIBM9J0sJOcQv5nDQaYwsee3kpbgo=
github.com/charmbracelet/bubbletea v0.26.1 h1:xujcQeF73rh4jwu3+zhfQsvV18x+7zIjlw7/CYbzGJ0=
github.com/charmbracelet/bubbletea v0.26.1/go.mod h1:FzKr7sKoO8iFVcdIBM9J0sJOcQv5nDQaYwsee3kpbgo=
github.com/charmbracelet/glamour v0.7.0 h1:2BtKGZ4iVJCDfMF229EzbeR1QRKLWztO9dMtjmqZSng=
github.com/charmbracelet/glamour v0.7.0/go.mod h1:jUMh5MeihljJPQbJ/wf4ldw2+yBP59+ctV36jASy7ps=
github.com/charmbracelet/huh v0.3.0 h1:CxPplWkgW2yUTDDG0Z4S5HH8SJOosWHd4LxCvi0XsKE=
Expand Down
28 changes: 14 additions & 14 deletions tui/appMenu.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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"
)

Expand Down Expand Up @@ -38,20 +39,22 @@ func (m AppMenuItem) Description() string {
type AppMenu struct {
list list.Model
view tea.Model
sdk handlers.Handler
}

func InitAppMenu() (AppMenu, tea.Cmd) {
func InitAppMenu(h handlers.Handler) (AppMenu, tea.Cmd) {
m := AppMenu{
view: nil,
sdk: h,
}
m.list = list.New([]list.Item{}, list.NewDefaultDelegate(), 8, 8)
m.list.Title = "OpenTDF"
m.list.SetItems([]list.Item{
AppMenuItem{title: "Namespaces", description: "Manage namespaces", id: namespaceMenu},
// AppMenuItem{title: "Namespaces", description: "Manage namespaces", id: namespaceMenu},
AppMenuItem{title: "Attributes", description: "Manage attributes", id: attributeMenu},
AppMenuItem{title: "Entitlements", description: "Manage entitlements", id: entitlementMenu},
AppMenuItem{title: "Resource Encodings", description: "Manage resource encodings", id: resourceEncodingMenu},
AppMenuItem{title: "Subject Encodings", description: "Manage subject encodings", id: subjectEncodingMenu},
// AppMenuItem{title: "Entitlements", description: "Manage entitlements", id: entitlementMenu},
// AppMenuItem{title: "Resource Encodings", description: "Manage resource encodings", id: resourceEncodingMenu},
// AppMenuItem{title: "Subject Encodings", description: "Manage subject encodings", id: subjectEncodingMenu},
})
return m, func() tea.Msg { return nil }
}
Expand All @@ -74,16 +77,13 @@ func (m AppMenu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
case "enter":
switch m.list.SelectedItem().(AppMenuItem).id {
// case namespaceMenu:
// // get namespaces
// nl, cmd := InitNamespaceList([]list.Item{}, 0)
// return nl, cmd
case attributeMenu:
item := AttributeItem{
id: "8a6755f2-efa8-4758-b893-af9a488e0bea",
namespace: "demo.com",
name: "relto",
rule: "hierarchical",
description: "The relto attribute is used to describe the relationship of the resource to the country of origin.",
values: []string{"USA", "GBR"},
}
al, cmd := InitAttributeList([]list.Item{item}, 0)
// list attributes
al, cmd := InitAttributeList("", m.sdk)
return al, cmd
}
}
Expand Down
49 changes: 37 additions & 12 deletions tui/attributeList.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/opentdf/otdfctl/pkg/handlers"
"github.com/opentdf/otdfctl/tui/constants"
"github.com/opentdf/platform/protocol/go/common"
)

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

type AttributeItem struct {
Expand All @@ -19,6 +22,7 @@ type AttributeItem struct {
description string
rule string
values []string
title string
}

func (m AttributeItem) FilterValue() string {
Expand All @@ -33,16 +37,35 @@ func (m AttributeItem) Description() string {
return m.description
}

func InitAttributeList(items []list.Item, selectIdx int) (tea.Model, tea.Cmd) {
// TODO: fetch items from API

m := AttributeList{}
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)
if selectIdx > 0 {
m.list.Select(selectIdx)
res, err := sdk.ListAttributes(common.ActiveStateEnum_ACTIVE_STATE_ENUM_ANY)
if err != nil {
return m, nil
}
var attrs []list.Item
selectIdx := 0
for i, attr := range res {
var vals []string
for _, val := range attr.Values {
vals = append(vals, val.Value)
}
if attr.Id == id {
selectIdx = i
}
item := AttributeItem{
id: attr.Id,
namespace: attr.Namespace.Name,
name: attr.Name,
rule: attr.Rule.String(),
values: vals,
}
attrs = append(attrs, item)
}
m.list.Title = "Attributes"
m.list.SetItems(items)
m.list.SetItems(attrs)
m.list.Select(selectIdx)
return m.Update(WindowMsg())
}

Expand Down Expand Up @@ -76,8 +99,9 @@ func (m AttributeList) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "ctrl+c", "q":
return m, tea.Quit
case "ctrl+[", "backspace":
am, _ := InitAppMenu()
am.list.Select(1)
am, _ := InitAppMenu(m.sdk)
// 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 {
Expand All @@ -87,10 +111,11 @@ func (m AttributeList) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.list.Index() > 0 {
m.list.Select(m.list.Index() - 1)
}
case "c":
return InitAttributeView(m.list.Items(), len(m.list.Items()))
// 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())
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
Expand Down
Loading

0 comments on commit a1c5a17

Please sign in to comment.