Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
load and save host content
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 6, 2022
1 parent e11f846 commit c932b6b
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 55 deletions.
8 changes: 4 additions & 4 deletions gohost/group.go
Expand Up @@ -10,8 +10,8 @@ var (
)

type Group struct {
ID uint `boltholdKey:"ID"`
ParentID uint
ID string `boltholdKey:"ID"`
ParentID string
Name string
Desc string
}
Expand All @@ -26,11 +26,11 @@ func (g Group) FilterValue() string {
return g.Name
}

func (g Group) GetID() uint {
func (g Group) GetID() string {
return g.ID
}

func (g Group) GetParentID() uint {
func (g Group) GetParentID() string {
return g.ParentID
}

Expand Down
26 changes: 13 additions & 13 deletions gohost/group_test.go
Expand Up @@ -12,19 +12,19 @@ func TestGroupService_Save(t *testing.T) {
a := assert.New(t)
service := GetService()
groups := []Group{
{ID: 1, ParentID: 0, Name: "g-1", Desc: "desc1"},
{ID: 11, ParentID: 1, Name: "g-1-1", Desc: "desc11"},
{ID: 111, ParentID: 11, Name: "g-1-1-1", Desc: "desc111"},
{ID: 112, ParentID: 11, Name: "g-1-1-2", Desc: "desc112"},
{ID: 12, ParentID: 1, Name: "g-1-2", Desc: "desc12"},
{ID: 13, ParentID: 1, Name: "g-1-3", Desc: "desc13"},
{ID: 131, ParentID: 13, Name: "g-1-3-1", Desc: "desc131"},
{ID: 132, ParentID: 13, Name: "g-1-3-2", Desc: "desc132"},
{ID: 2, ParentID: 0, Name: "g-2", Desc: "desc2"},
{ID: 3, ParentID: 0, Name: "g-3", Desc: "desc3"},
{ID: 31, ParentID: 3, Name: "g-3-1", Desc: "desc31"},
{ID: 32, ParentID: 3, Name: "g-3-2", Desc: "desc32"},
{ID: 4, ParentID: 0, Name: "g-4", Desc: "desc4"},
{ID: "1", ParentID: "0", Name: "g-1", Desc: "desc1"},
{ID: "11", ParentID: "1", Name: "g-1-1", Desc: "desc11"},
{ID: "111", ParentID: "11", Name: "g-1-1-1", Desc: "desc111"},
{ID: "112", ParentID: "11", Name: "g-1-1-2", Desc: "desc112"},
{ID: "12", ParentID: "1", Name: "g-1-2", Desc: "desc12"},
{ID: "13", ParentID: "1", Name: "g-1-3", Desc: "desc13"},
{ID: "131", ParentID: "13", Name: "g-1-3-1", Desc: "desc131"},
{ID: "132", ParentID: "13", Name: "g-1-3-2", Desc: "desc132"},
{ID: "2", ParentID: "0", Name: "g-2", Desc: "desc2"},
{ID: "3", ParentID: "0", Name: "g-3", Desc: "desc3"},
{ID: "31", ParentID: "3", Name: "g-3-1", Desc: "desc31"},
{ID: "32", ParentID: "3", Name: "g-3-2", Desc: "desc32"},
{ID: "4", ParentID: "0", Name: "g-4", Desc: "desc4"},
}
for _, g := range groups {
if err := service.SaveGroup(g); err != nil {
Expand Down
20 changes: 14 additions & 6 deletions gohost/host.go
Expand Up @@ -7,25 +7,33 @@ import (

type Host interface {
TreeNode
GetID() uint
GetID() string
GetName() string
GetContent() []byte
SetContent([]byte)
GetDesc() string
GetGroupID() uint
GetGroupID() string
}

func (s *Service) SaveHost(host Host) error {
if err := s.store.Insert(host.GetDesc(), host); err != nil {
if err := s.store.Insert(host.GetID(), host); err != nil {
return err
}
return nil
}

func (s *Service) LoadHosts(groupID uint) []Host {
func (s *Service) UpdateHost(host Host) error {
if err := s.store.Update(host.GetID(), host); err != nil {
return err
}
return nil
}

func (s *Service) LoadHosts(groupID string) []Host {
return s.loadLocalHosts(groupID)
}

func (s *Service) LoadHostNodes(groupID uint) []*Node[TreeNode] {
func (s *Service) LoadHostNodes(groupID string) []*Node[TreeNode] {
groupNode := s.nodes[groupID]
if groupNode == nil {
return nil
Expand All @@ -40,7 +48,7 @@ func (s *Service) LoadHostNodes(groupID uint) []*Node[TreeNode] {
return hostNodes
}

func (s *Service) loadLocalHosts(groupID uint) []Host {
func (s *Service) loadLocalHosts(groupID string) []Host {
var hosts []*LocalHost
if err := s.store.FindNullable(&hosts, bolthold.Where("GroupID").Eq(groupID)); err != nil {
panic(err)
Expand Down
8 changes: 5 additions & 3 deletions gohost/host_test.go
Expand Up @@ -3,18 +3,20 @@ package gohost
import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/timshannon/bolthold"
"testing"
)

func TestService_SaveHost(t *testing.T) {
a := assert.New(t)
GetService().store.DeleteMatching(&LocalHost{}, &bolthold.Query{})
hosts := []Host{
&LocalHost{
ID: 1000,
ID: "1000",
Name: "host-1000",
Content: []byte("127.0.0.1 localhost"),
Desc: "host1000",
GroupID: 3,
GroupID: "3",
},
}

Expand All @@ -26,7 +28,7 @@ func TestService_SaveHost(t *testing.T) {
}

func TestService_LoadHost(t *testing.T) {
hosts := GetService().loadLocalHosts(3)
hosts := GetService().loadLocalHosts("3")
for _, host := range hosts {
fmt.Println(host)
}
Expand Down
16 changes: 11 additions & 5 deletions gohost/local_host.go
@@ -1,16 +1,17 @@
package gohost

type LocalHost struct {
ID uint
ID string `boltholdKey:"ID"`
Name string
Content []byte
Desc string
GroupID uint
GroupID string
}

// Implement of Host
var _ Host = (*LocalHost)(nil)

func (h *LocalHost) GetID() uint {
func (h *LocalHost) GetID() string {
return h.ID
}

Expand All @@ -22,20 +23,25 @@ func (h *LocalHost) GetContent() []byte {
return h.Content
}

func (h *LocalHost) SetContent(content []byte) {
h.Content = content
}

func (h *LocalHost) GetDesc() string {
return h.Desc
}

func (h *LocalHost) GetGroupID() uint {
func (h *LocalHost) GetGroupID() string {
return h.GroupID
}

// Implement of TreeNode
var _ TreeNode = (*LocalHost)(nil)

func (h *LocalHost) FilterValue() string {
return h.Name
}

func (h *LocalHost) GetParentID() uint {
func (h *LocalHost) GetParentID() string {
return h.GroupID
}
8 changes: 4 additions & 4 deletions gohost/node.go
Expand Up @@ -4,8 +4,8 @@ import "github.com/charmbracelet/bubbles/list"

type TreeNode interface {
list.Item
GetID() uint
GetParentID() uint
GetID() string
GetParentID() string
}

type Node[T TreeNode] struct {
Expand All @@ -28,10 +28,10 @@ func (n *Node[T]) FilterValue() string {
return n.Data.FilterValue()
}

func (n *Node[T]) GetID() uint {
func (n *Node[T]) GetID() string {
return n.Data.GetID()
}

func (n *Node[T]) GetParentID() uint {
func (n *Node[T]) GetParentID() string {
return n.Data.GetParentID()
}
6 changes: 3 additions & 3 deletions gohost/service.go
Expand Up @@ -21,14 +21,14 @@ func GetService() *Service {
func NewService() *Service {
return &Service{
store: db.Instance(),
nodes: make(map[uint]*Node[TreeNode], 0),
nodes: make(map[string]*Node[TreeNode], 0),
tree: make([]*Node[TreeNode], 0),
}
}

type Service struct {
store *db.Store
nodes map[uint]*Node[TreeNode]
nodes map[string]*Node[TreeNode]
tree []*Node[TreeNode]
}

Expand Down Expand Up @@ -79,6 +79,6 @@ func (s *Service) Load() {
s.buildTree()
}

func (s *Service) ChildNodes(nodeID uint) []*Node[TreeNode] {
func (s *Service) ChildNodes(nodeID string) []*Node[TreeNode] {
return s.nodes[nodeID].Children
}
26 changes: 22 additions & 4 deletions tui/editor_view.go
@@ -1,13 +1,16 @@
package tui

import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textarea"
tea "github.com/charmbracelet/bubbletea"
"gohost/gohost"
)

type EditorView struct {
model *Model
HostTextarea textarea.Model
host gohost.Host
}

func NewTextView(model *Model) *EditorView {
Expand All @@ -29,11 +32,21 @@ func (v *EditorView) Update(msg tea.Msg) []tea.Cmd {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
v.HostTextarea.SetHeight(msg.Height - v.model.helpView.MaxHeight())
v.HostTextarea.SetHeight(msg.Height - v.model.reservedHeight)
v.HostTextarea.SetWidth(msg.Width - v.model.groupView.groupList.Width())
case tea.KeyMsg:
if v.model.state == editorViewState {
v.HostTextarea, cmd = v.HostTextarea.Update(msg)
}
if v.model.state == editorViewState {
v.HostTextarea, cmd = v.HostTextarea.Update(msg)
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Save):
v.host.SetContent([]byte(v.HostTextarea.Value()))
err := gohost.GetService().UpdateHost(v.host)
if err != nil {
v.model.Log(err.Error())
}
}
}
}
return append(cmds, cmd)
Expand All @@ -46,3 +59,8 @@ func (v *EditorView) View() string {
func (v *EditorView) Focus() {
v.HostTextarea.Focus()
}

func (v *EditorView) SetHost(host gohost.Host) {
v.host = host
v.HostTextarea.SetValue(string(host.GetContent()))
}
5 changes: 3 additions & 2 deletions tui/group_view.go
Expand Up @@ -90,7 +90,7 @@ func (v *GroupView) Update(msg tea.Msg) []tea.Cmd {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
v.groupList.SetHeight(msg.Height - v.model.helpView.MaxHeight())
v.groupList.SetHeight(msg.Height - v.model.reservedHeight)
// FIXME not work
v.groupList.SetWidth(msg.Width / 3)
v.model.helpView.debug = fmt.Sprintf("w %d h %d, w %d h %d", msg.Width, msg.Height, v.groupList.Width(), v.groupList.Height())
Expand Down Expand Up @@ -167,5 +167,6 @@ func (v *GroupView) onHostNodeSelected(cmds *[]tea.Cmd) {
v.selectedHost = v.selectedNode.Data.(gohost.Host)
v.model.Log("select host: " + v.selectedHost.GetName())
v.model.SwitchState(editorViewState)
v.model.editorView.HostTextarea.Placeholder = "abc"
v.model.editorView.SetHost(v.selectedHost)

}
4 changes: 0 additions & 4 deletions tui/help_view.go
Expand Up @@ -45,10 +45,6 @@ func (h *HelpView) View() string {
return helper
}

func (h *HelpView) MaxHeight() int {
return 6
}

func (h *HelpView) Width() int {
return h.view.Width
}
5 changes: 5 additions & 0 deletions tui/keymap.go
Expand Up @@ -11,6 +11,7 @@ type keyMaps struct {
Quit key.Binding
Enter key.Binding
Switch key.Binding
Save key.Binding
}

func newKeys() keyMaps {
Expand Down Expand Up @@ -47,6 +48,10 @@ func newKeys() keyMaps {
key.WithKeys("tab"),
key.WithHelp("tab", "switch view"),
),
Save: key.NewBinding(
key.WithKeys("ctrl+s"),
key.WithHelp("ctrl+s", "save"),
),
}
}

Expand Down
17 changes: 10 additions & 7 deletions tui/model.go
Expand Up @@ -23,16 +23,18 @@ var (
)

type Model struct {
state sessionState
helpView *HelpView
groupView *GroupView
editorView *EditorView
quitting bool
state sessionState
helpView *HelpView
groupView *GroupView
editorView *EditorView
reservedHeight int
quitting bool
}

func NewModel() (*Model, error) {
model := &Model{
state: 0,
state: 0,
reservedHeight: 6,
}
model.helpView = NewHelpView(model)
model.groupView = NewGroupView(model)
Expand Down Expand Up @@ -75,7 +77,8 @@ func (m *Model) View() string {
modelStyle.Render(m.groupView.View()),
focusedModelStyle.Render(m.editorView.View()))
}
str = lipgloss.JoinVertical(lipgloss.Left, str, m.helpView.View())
//str = lipgloss.JoinVertical(lipgloss.Left, str, m.helpView.View())
str += "\n" + m.helpView.View()
return str
}

Expand Down

0 comments on commit c932b6b

Please sign in to comment.