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

Commit

Permalink
fix remove node bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 16, 2022
1 parent de5b63c commit cb46376
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 137 deletions.
27 changes: 0 additions & 27 deletions gohost/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,3 @@ func (s *Service) loadGroupNodesByParent(parent *TreeNode) []*TreeNode {
}
return nodes
}

func (s *Service) SaveGroup(group *Group) error {
return s.store.Insert(s.extractID(group), group)
}

func (s *Service) SaveGroupNode(groupNode *TreeNode) {
group := groupNode.Node.(*Group)
if err := s.SaveGroup(group); err != nil {
panic(err)
}
s.nodes[groupNode.GetID()] = groupNode
}

func (s *Service) UpdateGroup(group *Group) error {
return s.store.Update(group.GetID(), group)
}

func (s *Service) UpdateGroupNode(groupNode *TreeNode) {
group := groupNode.Node.(*Group)
if err := s.UpdateGroup(group); err != nil {
panic(err)
}
}

func (s *Service) DeleteGroup(id db.ID) error {
return s.store.Delete(id, &Group{})
}
6 changes: 1 addition & 5 deletions gohost/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ package gohost

import (
"github.com/stretchr/testify/assert"
"gohost/db"
"testing"
)

func TestGroupService_Save(t *testing.T) {
store := db.Instance()
defer store.Close()
a := assert.New(t)
service := GetService()
groups := []*Group{
{ID: 002, ParentID: 000, Name: "g-1", Desc: "desc1"},
{ID: 011, ParentID: 002, Name: "g-1-1", Desc: "desc11"},
Expand All @@ -27,7 +23,7 @@ func TestGroupService_Save(t *testing.T) {
{ID: 005, ParentID: 000, Name: "g-4", Desc: "desc4"},
}
for _, g := range groups {
if err := service.SaveGroup(g); err != nil {
if err := GetService().SaveNode(NewTreeNode(g)); err != nil {
a.NoError(err)
}
}
Expand Down
50 changes: 34 additions & 16 deletions gohost/host.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,49 @@
package gohost

import (
"bytes"
"os"
)

type Host interface {
Node
GetContent() []byte
SetContent([]byte)
IsEditable() bool
}

func (s *Service) SaveHost(host Host) error {
if err := s.store.Insert(s.extractID(host), host); err != nil {
return err
func (s *Service) ApplyHost(hostContent []byte) {
// Truncate system host file
sysHostFile, err := os.Create(cfg.SysHostFile)
if err != nil {
panic(err)
}
return nil
}

func (s *Service) SaveHostNode(hostNode *TreeNode) error {
host := hostNode.Node.(Host)
if err := s.SaveHost(host); err != nil {
return err
defer sysHostFile.Close()
// Write hosts to system host file
if _, err = sysHostFile.Write(hostContent); err != nil {
panic(err)
}
s.nodes[hostNode.GetID()] = hostNode
return nil
}

func (s *Service) UpdateHost(host Host) error {
if err := s.store.Update(host.GetID(), host); err != nil {
return err
func (s *Service) CombineEnabledHosts() []byte {
hosts := s.loadLocalHostsByFlag(MaskEnable)
// TODO load all enabled remote hosts
combinedHost := bytes.NewBuffer(nil)
for _, host := range hosts {
combinedHost.WriteString("# Content from ")
combinedHost.WriteString(host.Title())
if host.Description() != "" {
combinedHost.WriteString("( ")
combinedHost.WriteString(host.Description())
combinedHost.WriteString(" )")
}
combinedHost.WriteString(cfg.LineBreak)
combinedHost.Write(host.GetContent())
combinedHost.WriteString(cfg.LineBreak)
combinedHost.WriteString("# End of ")
combinedHost.WriteString(host.Title())
combinedHost.WriteString(cfg.LineBreak)
combinedHost.WriteString(cfg.LineBreak)
}
return nil
return combinedHost.Bytes()
}
4 changes: 2 additions & 2 deletions gohost/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ func TestService_SaveHost(t *testing.T) {
ID: 1001,
Name: "host-1",
Content: []byte("127.0.0.2 localhost"),
Desc: "host1000",
Desc: "host1001",
GroupID: 0,
},
}

for _, host := range hosts {
if err := GetService().SaveHost(host); err != nil {
if err := GetService().SaveNode(NewTreeNode(host)); err != nil {
a.NoError(err)
}
}
Expand Down
4 changes: 0 additions & 4 deletions gohost/local_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,3 @@ func (s *Service) loadLocalHostsByFlag(flag int) []Host {
}
return hosts
}

func (s *Service) DeleteLocalHost(id db.ID) error {
return s.store.Delete(id, &LocalHost{})
}
88 changes: 26 additions & 62 deletions gohost/service.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package gohost

import (
"bytes"
"github.com/charmbracelet/bubbles/list"
"gohost/config"
"gohost/db"
"os"
"sync"
)

Expand Down Expand Up @@ -34,6 +32,7 @@ func NewService() *Service {
children: make([]*TreeNode, 0),
depth: -1,
}

sysHostNode := NewTreeNode(SysHostInstance())
sysHostNode.SetParent(s.tree)
s.SysHostNode = sysHostNode
Expand Down Expand Up @@ -122,54 +121,42 @@ func (s *Service) RemoveNodesByParent(parent *TreeNode) {
parent.SetChildren(nil)
}

func (s *Service) extractID(node Node) db.ID {
if node.GetID() == 0 {
return s.store.NextID()
func (s *Service) DeleteNode(node *TreeNode) error {
if node == nil || node.Node == nil || node == s.tree || node == s.SysHostNode {
return nil
}
if err := s.store.Delete(node.GetID(), node.Node); err != nil {
return err
}
return node.GetID()
s.nodes[node.GetID()] = nil
if node.Parent() != nil {
node.Parent().RemoveChild(node)
}
return nil
}

func (s *Service) DeleteNode(node *TreeNode) {
if node == nil || node.Node == nil || node == s.SysHostNode {
return
func (s *Service) DeleteNodeRecursively(node *TreeNode) error {
if err := s.DeleteNode(node); err != nil {
return err
}
if node == s.tree {
panic("Can not delete dummy root node")
}
node.Parent().RemoveChild(node)
s.nodes[node.GetID()] = nil
switch node.Node.(type) {
case *Group:
if err := s.DeleteGroup(node.GetID()); err != nil {
panic(err)
}
for _, childNode := range node.Children() {
s.DeleteNode(childNode)
for len(node.Children()) > 0 {
if err := s.DeleteNodeRecursively(node.Children()[0]); err != nil {
return err
}
case *LocalHost:
if err := s.DeleteLocalHost(node.GetID()); err != nil {
panic(err)
}
case *RemoteHost:

}
return nil
}

func (s *Service) UpdateNode(node *TreeNode) {
if err := s.store.Update(node.GetID(), node.Node); err != nil {
panic(err)
func (s *Service) SaveNode(node *TreeNode) error {
id := node.GetID()
if id == 0 {
id = s.store.NextID()
}
return s.store.Insert(id, node.Node)
}

func (s *Service) ApplyHost(hostContent []byte) {
// Truncate system host file
sysHostFile, err := os.Create(cfg.SysHostFile)
if err != nil {
panic(err)
}
defer sysHostFile.Close()
// Write hosts to system host file
if _, err = sysHostFile.Write(hostContent); err != nil {
func (s *Service) UpdateNode(node *TreeNode) {
if err := s.store.Update(node.GetID(), node.Node); err != nil {
panic(err)
}
}
Expand Down Expand Up @@ -198,26 +185,3 @@ func (s *Service) UpdateEnabledOfNode(node *TreeNode, enabled bool) {
s.UpdateEnabledOfNode(child, enabled)
}
}

func (s *Service) CombineEnabledHosts() []byte {
hosts := s.loadLocalHostsByFlag(MaskEnable)
// TODO load all enabled remote hosts
combinedHost := bytes.NewBuffer(nil)
for _, host := range hosts {
combinedHost.WriteString("# Content from ")
combinedHost.WriteString(host.Title())
if host.Description() != "" {
combinedHost.WriteString("( ")
combinedHost.WriteString(host.Description())
combinedHost.WriteString(" )")
}
combinedHost.WriteString(cfg.LineBreak)
combinedHost.Write(host.GetContent())
combinedHost.WriteString(cfg.LineBreak)
combinedHost.WriteString("# End of ")
combinedHost.WriteString(host.Title())
combinedHost.WriteString(cfg.LineBreak)
combinedHost.WriteString(cfg.LineBreak)
}
return combinedHost.Bytes()
}
8 changes: 0 additions & 8 deletions tui/confirm_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,9 @@ func NewConfirmView(model *Model) *ConfirmView {
confirmForm.AddItem(tipLabel)

confirmButton := form.NewButton("Confirm")
confirmButton.OnClick = func() tea.Cmd {
confirmForm.AddItem(form.NewLabel("click confirm button"))
return nil
}
confirmForm.AddItem(confirmButton)

cancelButton := form.NewButton("Cancel")
cancelButton.OnClick = func() tea.Cmd {
confirmForm.AddItem(form.NewLabel("click cancle button"))
return nil
}
confirmForm.AddItem(cancelButton)

confirmForm.FocusAvailableFirstItem()
Expand Down
19 changes: 12 additions & 7 deletions tui/editor_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,8 @@ func (v *EditorView) Init() tea.Cmd {
{keys.Up, keys.Down, keys.Left, keys.Right, keys.Save},
{km.CharacterForward, km.CharacterBackward}, // TODO add all key map from textarea.KeyMap
})
return func() tea.Msg {
// Display system host on start up
v.SetHostNode(svc.SysHostNode)
return nil
}
v.SetHostNode(svc.SysHostNode)
return nil
}

func (v *EditorView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand All @@ -77,8 +74,9 @@ func (v *EditorView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(m, keys.Esc):
return v, nil
case key.Matches(m, keys.Save):
if v.hostNode.Node.(gohost.Host).IsEditable() {
v.hostNode.Node.(gohost.Host).SetContent([]byte(v.hostEditor.Value()))
host := v.Host()
if host.IsEditable() {
host.SetContent([]byte(v.hostEditor.Value()))
svc.UpdateNode(v.hostNode)
v.SetSaved()
} else {
Expand Down Expand Up @@ -114,6 +112,13 @@ func (v *EditorView) Blur() {
v.hostEditor.Blur()
}

func (v *EditorView) Host() gohost.Host {
if v.hostNode == nil {
return nil
}
return v.hostNode.Node.(gohost.Host)
}

func (v *EditorView) SetHostNode(hostNode *gohost.TreeNode) {
v.hostNode = hostNode
v.hostEditor.Reset()
Expand Down
2 changes: 1 addition & 1 deletion tui/form/choice.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (c *Choices) View() string {
b.WriteString(c.unfocusedStyle.Render(c.itemTitle(i)))
}
if i < len(c.items)-1 {
b.WriteString(strings.Repeat(cfg.LineBreak, c.Spacing+1))
b.WriteString(strings.Repeat("\n", c.Spacing+1))
}
}
return b.String()
Expand Down
6 changes: 2 additions & 4 deletions tui/node_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func NewNodeView(model *Model) *NodeView {
}
groupNode := gohost.NewTreeNode(group)
groupNode.SetParent(parent)
svc.SaveGroupNode(groupNode)
svc.SaveNode(groupNode)
case NodeLocalHost:
localHost := &gohost.LocalHost{
GroupID: parent.GetID(),
Expand All @@ -92,9 +92,7 @@ func NewNodeView(model *Model) *NodeView {
}
localHostNode := gohost.NewTreeNode(localHost)
localHostNode.SetParent(parent)
if err := svc.SaveHostNode(localHostNode); err != nil {
panic(err)
}
svc.SaveNode(localHostNode)
case NodeRemoteHost:
}

Expand Down
4 changes: 3 additions & 1 deletion tui/tree_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ func (v *TreeView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
case key.Matches(m, keys.Delete):
cmd = func() tea.Msg {
svc.DeleteNode(selectedNode)
if err := svc.DeleteNodeRecursively(selectedNode); err != nil {
panic(err)
}
return RefreshTreeViewMsg{}
}
case key.Matches(m, keys.Apply):
Expand Down

0 comments on commit cb46376

Please sign in to comment.