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

Commit

Permalink
fix node interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 7, 2022
1 parent 4392d6f commit f7c0304
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 72 deletions.
2 changes: 1 addition & 1 deletion config/unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func New() *config {
}
return &config{
BaseDir: baseDir,
DBFile: filepath.Join(baseDir, "gohost.db"),
DBFile: filepath.Join(baseDir, name+".db"),
SysHostFile: "/etc/hosts",
}
}
2 changes: 1 addition & 1 deletion config/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func New() *config {
}
return &config{
BaseDir: baseDir,
DBFile: filepath.Join(baseDir, "gohost.db"),
DBFile: filepath.Join(baseDir, name+".db"),
SysHostFile: "C:\\Windows\\System32\\drivers\\etc\\hosts",
}
}
8 changes: 4 additions & 4 deletions gohost/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ func (s *Service) loadGroups() []Group {
return groups
}

func (s *Service) loadGroupNodes() []*Node[TreeNode] {
func (s *Service) loadGroupNodes() []*TreeNode[Node] {
groups := s.loadGroups()

groupNodes := make([]*Node[TreeNode], 0, len(groups))
groupNodes := make([]*TreeNode[Node], 0, len(groups))
for _, group := range groups {
groupNodes = append(groupNodes, NewNode[TreeNode](group, 0))
groupNodes = append(groupNodes, NewTreeNode[Node](group, 0))
}
return groupNodes
}
Expand All @@ -61,6 +61,6 @@ func (s *Service) SaveGroup(group Group) error {
return err
}
// FIXME set correct depth
s.nodes[group.ID] = NewNode[TreeNode](&group, 0)
s.nodes[group.ID] = NewTreeNode[Node](&group, 0)
return nil
}
10 changes: 5 additions & 5 deletions gohost/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
)

type Host interface {
TreeNode
Node
GetID() string
GetName() string
GetContent() []byte
SetContent([]byte)
GetDesc() string
GetGroupID() string
IsEnabled() bool
IsEditable() bool
}

func (s *Service) SaveHost(host Host) error {
Expand All @@ -34,16 +34,16 @@ func (s *Service) LoadHosts(groupID string) []Host {
return s.loadLocalHosts(groupID)
}

func (s *Service) LoadHostNodes(groupID string) []*Node[TreeNode] {
func (s *Service) LoadHostNodes(groupID string) []*TreeNode[Node] {
groupNode := s.nodes[groupID]
if groupNode == nil {
return nil
}
hostNodeDepth := groupNode.Depth + 1
hosts := s.LoadHosts(groupID)
hostNodes := make([]*Node[TreeNode], 0, len(hosts))
hostNodes := make([]*TreeNode[Node], 0, len(hosts))
for _, host := range hosts {
node := NewNode[TreeNode](host, hostNodeDepth)
node := NewTreeNode[Node](host, hostNodeDepth)
hostNodes = append(hostNodes, node)
}
return hostNodes
Expand Down
7 changes: 5 additions & 2 deletions gohost/local_host.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gohost

var _ Host = (*LocalHost)(nil)

type LocalHost struct {
ID string `boltholdKey:"ID"`
Name string
Expand All @@ -9,8 +11,9 @@ type LocalHost struct {
Enabled bool
}

// Implement of Host
var _ Host = (*LocalHost)(nil)
func (h *LocalHost) IsEditable() bool {
return true
}

func (h *LocalHost) GetID() string {
return h.ID
Expand Down
28 changes: 14 additions & 14 deletions gohost/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,36 @@ package gohost

import "github.com/charmbracelet/bubbles/list"

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

type Node[T TreeNode] struct {
Data T
Children []*Node[T]
type TreeNode[T Node] struct {
Node T
Children []*TreeNode[T]
Depth int
IsFolded bool
}

func NewNode[T TreeNode](data T, depth int) *Node[T] {
return &Node[T]{
Data: data,
Children: make([]*Node[T], 0),
func NewTreeNode[T Node](data T, depth int) *TreeNode[T] {
return &TreeNode[T]{
Node: data,
Children: make([]*TreeNode[T], 0),
Depth: depth,
IsFolded: true,
}
}

func (n *Node[T]) FilterValue() string {
return n.Data.FilterValue()
func (n *TreeNode[T]) FilterValue() string {
return n.Node.FilterValue()
}

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

func (n *Node[T]) GetParentID() string {
return n.Data.GetParentID()
func (n *TreeNode[T]) GetParentID() string {
return n.Node.GetParentID()
}
26 changes: 14 additions & 12 deletions gohost/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,25 @@ func GetService() *Service {

func NewService() *Service {
return &Service{
store: db.Instance(),
nodes: make(map[string]*Node[TreeNode], 0),
tree: make([]*Node[TreeNode], 0),
store: db.Instance(),
nodes: make(map[string]*TreeNode[Node], 0),
tree: make([]*TreeNode[Node], 0),
SysHostNode: NewTreeNode[Node](SysHost(), 0),
}
}

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

func (s *Service) Tree() []*Node[TreeNode] {
func (s *Service) Tree() []*TreeNode[Node] {
return s.tree
}

func (s *Service) cacheNodes(nodes []*Node[TreeNode]) {
func (s *Service) cacheNodes(nodes []*TreeNode[Node]) {
for _, node := range nodes {
s.nodes[node.GetID()] = node
}
Expand All @@ -47,7 +49,7 @@ func (s *Service) cacheNodes(nodes []*Node[TreeNode]) {
func (s *Service) buildTree() {
// Build tree
for _, node := range s.nodes {
p, exist := s.nodes[node.Data.GetParentID()]
p, exist := s.nodes[node.Node.GetParentID()]
if !exist {
s.tree = append(s.tree, node)
continue
Expand All @@ -57,7 +59,7 @@ func (s *Service) buildTree() {
}
// Bfs to set depth
sort.Slice(s.tree, func(i, j int) bool {
return s.tree[i].Data.GetID() < s.tree[j].GetID()
return s.tree[i].Node.GetID() < s.tree[j].GetID()
})
nodes := s.tree
depth := 0
Expand All @@ -75,13 +77,13 @@ func (s *Service) buildTree() {
}

func (s *Service) Load() {
nodes := make([]*Node[TreeNode], 0)
nodes := []*TreeNode[Node]{s.SysHostNode}
nodes = append(nodes, s.loadGroupNodes()...)
s.cacheNodes(nodes)
s.buildTree()
}

func (s *Service) ChildNodes(nodeID string) []*Node[TreeNode] {
func (s *Service) ChildNodes(nodeID string) []*TreeNode[Node] {
return s.nodes[nodeID].Children
}

Expand Down
25 changes: 11 additions & 14 deletions gohost/sys_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ import (
"sync"
)

type sysHost struct {
name string
desc string
}

var (
_ Host = (*sysHost)(nil)
sysHostOnce sync.Once
sysHostInstance *sysHost
)
Expand All @@ -26,23 +22,28 @@ func SysHost() Host {
return sysHostInstance
}

// Implement of Host
var _ Host = (*sysHost)(nil)
type sysHost struct {
name string
desc string
}

func (s *sysHost) IsEditable() bool {
return false
}

func (s *sysHost) FilterValue() string {
return s.name
}

func (s *sysHost) GetParentID() string {
panic("implement me")
return ""
}

func (s *sysHost) GetID() string {
panic("implement me")
return "-1"
}

func (s *sysHost) GetName() string {
//TODO implement me
return s.name
}

Expand All @@ -62,10 +63,6 @@ func (s *sysHost) GetDesc() string {
return s.desc
}

func (s *sysHost) GetGroupID() string {
panic("implement me")
}

func (s *sysHost) IsEnabled() bool {
return true
}
16 changes: 10 additions & 6 deletions tui/editor_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,24 @@ func (v *EditorView) Update(msg tea.Msg) []tea.Cmd {
if v.model.state == editorViewState {
switch {
case key.Matches(m, keys.Save):
v.host.SetContent([]byte(v.hostEditor.Value()))
err := gohost.GetService().UpdateHost(v.host)
if err != nil {
v.model.Log(err.Error())
if v.host.IsEditable() {
v.host.SetContent([]byte(v.hostEditor.Value()))
err := gohost.GetService().UpdateHost(v.host)
if err != nil {
v.model.Log(err.Error())
} else {
v.SetSaved()
}
} else {
v.SetSaved()
v.statusLine = "Can not edit this host"
}
}
} else {
// Disable key
msg = nil
}
}
v.RefreshStatusLine()
//v.RefreshStatusLine()
v.hostEditor, cmd = v.hostEditor.Update(msg)
return append(cmds, cmd)
}
Expand Down
24 changes: 11 additions & 13 deletions tui/group_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type groupItemDelegate struct {
}

func (d groupItemDelegate) Render(w io.Writer, m list.Model, index int, item list.Item) {
node, ok := item.(*gohost.Node[gohost.TreeNode])
node, ok := item.(*gohost.TreeNode[gohost.Node])
if !ok {
return
}
Expand All @@ -27,11 +27,11 @@ func (d groupItemDelegate) Render(w io.Writer, m list.Model, index int, item lis
str = " "
}
spaces := strings.Repeat(" ", node.Depth)
switch node := node.Data.(type) {
switch node := node.Node.(type) {
case gohost.Group:
str += fmt.Sprintf("%s[G] %d. %s", spaces, index, node.Name)
case *gohost.LocalHost:
str += fmt.Sprintf("%s[L] %d. %s", spaces, index, node.Name)
case gohost.Host:
str += fmt.Sprintf("%s[L] %d. %s", spaces, index, node.GetName())
}
_, _ = fmt.Fprint(w, str)
}
Expand All @@ -52,7 +52,7 @@ func (d groupItemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd {
type GroupView struct {
model *Model
groupList list.Model
selectedNode *gohost.Node[gohost.TreeNode]
selectedNode *gohost.TreeNode[gohost.Node]
selectedIndex int
selectedGroup gohost.Group
selectedHost gohost.Host
Expand All @@ -75,7 +75,7 @@ func NewGroupView(model *Model) *GroupView {
return &GroupView{
model: model,
groupList: groupList,
selectedNode: nil,
selectedNode: service.SysHostNode,
service: service,
}
}
Expand All @@ -98,9 +98,9 @@ func (v *GroupView) Update(msg tea.Msg) []tea.Cmd {
case key.Matches(m, keys.Enter):
selectedItem := v.groupList.SelectedItem()
if selectedItem != nil {
v.selectedNode = selectedItem.(*gohost.Node[gohost.TreeNode])
v.selectedNode = selectedItem.(*gohost.TreeNode[gohost.Node])
v.selectedIndex = v.groupList.Index()
switch v.selectedNode.Data.(type) {
switch v.selectedNode.Node.(type) {
case gohost.Group:
v.onGroupNodeEnterClick(&cmds)
case gohost.Host:
Expand All @@ -122,7 +122,7 @@ func (v *GroupView) View() string {
}

func (v *GroupView) onGroupNodeEnterClick(cmds *[]tea.Cmd) {
v.selectedGroup = v.selectedNode.Data.(gohost.Group)
v.selectedGroup = v.selectedNode.Node.(gohost.Group)
if v.selectedNode.IsFolded {
v.unfoldSelectedGroup(cmds)
} else {
Expand Down Expand Up @@ -152,7 +152,7 @@ func (v *GroupView) foldSelectedGroup() {
if items[next] == nil {
break
}
node := items[next].(*gohost.Node[gohost.TreeNode])
node := items[next].(*gohost.TreeNode[gohost.Node])
if node.Depth > v.selectedNode.Depth {
node.IsFolded = true
v.groupList.RemoveItem(next)
Expand All @@ -163,10 +163,8 @@ func (v *GroupView) foldSelectedGroup() {
}

func (v *GroupView) onHostNodeSelected(cmds *[]tea.Cmd) {
// TODO display host content
v.selectedHost = v.selectedNode.Data.(gohost.Host)
v.selectedHost = v.selectedNode.Node.(gohost.Host)
v.model.Log("select host: " + v.selectedHost.GetName())
v.model.SwitchState(editorViewState)
v.model.editorView.SetHost(v.selectedHost)

}

0 comments on commit f7c0304

Please sign in to comment.