Skip to content

Commit

Permalink
Merge pull request #62 from ewilliams0305/authorization
Browse files Browse the repository at this point in the history
Created API Token View
  • Loading branch information
ewilliams0305 committed Jan 7, 2024
2 parents 21e8dfa + a9669a7 commit 6dc5954
Show file tree
Hide file tree
Showing 14 changed files with 813 additions and 24 deletions.
11 changes: 11 additions & 0 deletions pkg/tui/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package tui

import "github.com/ewilliams0305/VC4-CLI/pkg/vc"

var (
server vc.VirtualControl
app *MainModel
programsView *ProgramsModel
roomsModel *RoomsTableModel
iptable *IpTableModel
)
3 changes: 0 additions & 3 deletions pkg/tui/iptable.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
vc "github.com/ewilliams0305/VC4-CLI/pkg/vc"
)

var iptable *IpTableModel

type IpTableModel struct {
roomId string
table table.Model
Expand Down Expand Up @@ -173,7 +171,6 @@ func getIpTableRows(width int, cursor int, entries []vc.IpTableEntry) []table.Ro
}
return rows
}

func IpTableQuery(id string) tea.Cmd {

return func() tea.Msg {
Expand Down
2 changes: 0 additions & 2 deletions pkg/tui/programs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
vc "github.com/ewilliams0305/VC4-CLI/pkg/vc"
)

var programsView *ProgramsModel

type ProgramsModel struct {
table table.Model
Programs vc.Programs
Expand Down
2 changes: 0 additions & 2 deletions pkg/tui/rooms.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
vc "github.com/ewilliams0305/VC4-CLI/pkg/vc"
)

var roomsModel *RoomsTableModel

type RoomsTableModel struct {
table table.Model
rooms vc.Rooms
Expand Down
18 changes: 13 additions & 5 deletions pkg/tui/rooms_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type roomsKeyMap struct {
// ShortHelp returns keybindings to be shown in the mini help view. It's part
// of the key.Map interface.
func (k roomsKeyMap) ShortHelp() []key.Binding {
return []key.Binding{k.Quit, k.Up, k.Down, k.Start, k.Restart, k.Debug, k.Delete, k.Table}
return []key.Binding{k.Quit, k.Up, k.Down, k.Start, k.Restart, k.Debug, k.Delete, k.Table, k.Create, k.Edit}
}

// FullHelp returns keybindings for the expanded help view. It's part of the
Expand Down Expand Up @@ -74,14 +74,22 @@ var roomKeys = roomsKeyMap{
key.WithKeys("ctrl+r"),
key.WithHelp("ctrl+r", "restart room"),
),
Delete: key.NewBinding(
key.WithKeys("delete"),
key.WithHelp("delete", "delete room"),
),
Table: key.NewBinding(
key.WithKeys("ctrl+t"),
key.WithHelp("ctrl+t", "view ip table"),
),
Create: key.NewBinding(
key.WithKeys("ctrl+n"),
key.WithHelp("ctrl+n", "create room"),
),
Edit: key.NewBinding(
key.WithKeys("ctrl+e", "enter"),
key.WithHelp("enter", "edit room"),
),
Delete: key.NewBinding(
key.WithKeys("delete"),
key.WithHelp("delete", "delete room"),
),
}

type RoomsHelpModel struct {
Expand Down
26 changes: 14 additions & 12 deletions pkg/tui/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
vc "github.com/ewilliams0305/VC4-CLI/pkg/vc"
)

var server vc.VirtualControl

type appState int

const (
Expand All @@ -27,17 +25,15 @@ const (
// INFO VIEW, displays all hardware and system information
info appState = 3
// DEVICES VIEW, displays all the device IP Tables and maps
devices appState = 4
//devices appState = 4
// AUTH VIEW, displays all auth and api tokens
auth appState = 5
auth appState = 4
// SYSTEM SERVICE VIEW, displays logs and service status
systemd appState = 6
systemd appState = 5
// HELP VIEW
helpState appState = 7
helpState appState = 6
)

var app *MainModel

type MainModel struct {
state appState
device vc.DeviceInfo
Expand All @@ -53,7 +49,7 @@ func InitialModel() MainModel {
w, h, _ := term.GetSize(int(os.Stdout.Fd()))
app = &MainModel{
device: vc.DeviceInfo{},
actions: []string{"Refresh", "Manage Programs", "Manage Rooms", "Device Information", "Devices", "Authorization", "System Service", "Help"},
actions: []string{"Refresh", "Manage Programs", "Manage Rooms", "Device Information", "API Tokens", "System Service", "Help"},
help: NewHelpModel(),
width: w,
height: h,
Expand Down Expand Up @@ -138,10 +134,13 @@ func (m MainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "s", "ctrl+s":
m.state = programs
return InitialSystemModel(), nil
}

case "a", "ctrl+a":
m.state = auth
authView := InitialTokensModel(m.width, m.height)
return authView, authView.Init()
}
}

// Return the updated model to the Bubble Tea runtime for processing.
// Note that we're not returning a command.
return m, nil
Expand Down Expand Up @@ -191,7 +190,10 @@ func arrowSelected(m *MainModel) (tea.Model, tea.Cmd) {
m.state = info
return NewDeviceInfo(m.width, m.height), DeviceInfoCommand
case int(auth):
case int(devices):
m.state = auth
authView := InitialTokensModel(m.width, m.height)
return authView, authView.Init()
//case int(devices):
case int(systemd):
return InitialSystemModel(), nil
case int(helpState):
Expand Down
9 changes: 9 additions & 0 deletions pkg/tui/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func RenderMessageBox(width int) lipgloss.Style {
Background(lipgloss.Color(PrimaryDark)).
PaddingTop(1).
PaddingLeft(1).
PaddingRight(1).
MarginBottom(1).
Width(width).Align(lipgloss.Top).
Height(3)
Expand Down Expand Up @@ -95,6 +96,14 @@ func GetOnlineIcon(status string) string {
return "❌"
}

func GetReadonlyIcon(status vc.TokenStatus) string {

if status == vc.ReadOnlyToken {
return "✅"
}
return "❌"
}

func CheckMark(status bool) string {
if status {
return " \u2713"
Expand Down
80 changes: 80 additions & 0 deletions pkg/tui/token_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package tui

import (
"fmt"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/ewilliams0305/VC4-CLI/pkg/vc"
)

type DeleteTokenForm struct {
form *huh.Form
token *vc.ApiToken
}

var (
tokenDeleteConfirm bool
)

func DeleteTokenFormModel(token *vc.ApiToken) DeleteTokenForm {
return DeleteTokenForm{
token: token,
form: huh.NewForm(
huh.NewGroup(
huh.NewConfirm().
Title(fmt.Sprintf("Are you sure you want to delete %s api?", token.Description)).
Value(&tokenDeleteConfirm).
Affirmative("Yes").
Negative("Cancel"),
),
).WithTheme(huh.ThemeDracula()),
}
}

func (m DeleteTokenForm) Init() tea.Cmd {
return m.form.Init()
}

func (m DeleteTokenForm) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {

case bool:
if msg {
return InitialTokensModel(app.width, app.height), tea.Batch(DeleteToken(m.token.Token), QueryTokens, tick)
}
return InitialTokensModel(app.width, app.height), tea.Batch(QueryTokens, tick)
case tea.KeyMsg:
switch msg.String() {
case "shift+tab":
return InitialTokensModel(app.width, app.height), tea.Batch(QueryTokens, tick)
}
}

form, cmd := m.form.Update(msg)
if f, ok := form.(*huh.Form); ok {
m.form = f

if m.form.State == huh.StateCompleted {

return m, SumbitDeleteTokenForm(&m)
}
}
return m, cmd
}

func (m DeleteTokenForm) View() string {
s := m.form.View()
return s
}

func SumbitDeleteTokenForm(m *DeleteTokenForm) tea.Cmd {
if m.form.State != huh.StateCompleted {
return nil
}
return deleteTokenConfirmation
}

func deleteTokenConfirmation() tea.Msg {
return tokenDeleteConfirm
}
Loading

0 comments on commit 6dc5954

Please sign in to comment.