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

Commit

Permalink
some nessasry strcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 4, 2022
1 parent db7350d commit b646864
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 8 deletions.
14 changes: 13 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"errors"
"github.com/timshannon/bolthold"
"os"
"path/filepath"
)
Expand All @@ -11,7 +12,10 @@ type Config struct {
DBFile string
}

var cfg *Config
var (
cfg *Config
store *bolthold.Store
)

func init() {
homeDir, err := os.UserHomeDir()
Expand Down Expand Up @@ -40,4 +44,12 @@ func init() {
BaseDir: baseDir,
DBFile: dbFile,
}

store, err = NewStore(&StoreOptions{
File: cfg.DBFile,
Options: &bolthold.Options{},
})
if err != nil {
panic(err)
}
}
7 changes: 7 additions & 0 deletions db_host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

type DBHost struct {
Name string
Content string
Group int
}
74 changes: 74 additions & 0 deletions group.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,81 @@
package main

import (
"errors"
"github.com/timshannon/bolthold"
)

var (
ErrGroupExist = errors.New("group is already existed")
)

type Group struct {
ID int
Parent int
Name string
}

type GroupNode struct {
*Group
Children []*GroupNode
}

func NewGroupNode(group *Group) *GroupNode {
return &GroupNode{
Group: group,
Children: make([]*GroupNode, 0),
}
}

type GroupService struct {
Groups map[int]*GroupNode
Tree []*GroupNode
}

func NewGroupService() *GroupService {
return &GroupService{
Groups: make(map[int]*GroupNode, 0),
Tree: make([]*GroupNode, 0),
}
}

func (gs *GroupService) LoadGroups() ([]*Group, error) {
var groups []*Group
if err := store.Find(&groups, &bolthold.Query{}); err != nil {
if errors.Is(bolthold.ErrNotFound, err) {
return nil, nil
} else {
return nil, err
}
}
return groups, nil
}

func (gs *GroupService) BuildTree(groups []*Group) {
if len(gs.Groups) == 0 {
return
}
for _, group := range groups {
gs.Groups[group.ID] = NewGroupNode(group)
}
for _, node := range gs.Groups {
p, exist := gs.Groups[node.Parent]
if !exist {
gs.Tree = append(gs.Tree, node)
continue
}
p.Children = append(p.Children, node)
}
}

func (gs *GroupService) Save(group Group) error {
if _, exist := gs.Groups[group.ID]; exist {
return ErrGroupExist
}
err := store.Insert(bolthold.NextSequence(), group)
if err != nil {
return err
}
gs.Groups[group.ID] = NewGroupNode(&group)
return nil
}
38 changes: 38 additions & 0 deletions group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"fmt"
"testing"
)

func TestGroupService_BuildTree(t *testing.T) {
//a := assert.New(t)
gs := NewGroupService()

groups := []*Group{
{
ID: 1,
Parent: 0,
Name: "g1",
},
{
ID: 2,
Parent: 0,
Name: "g2",
},
{
ID: 3,
Parent: 1,
Name: "g13",
},
{
ID: 4,
Parent: 3,
Name: "g34",
},
}
gs.BuildTree(groups)
for _, node := range gs.Tree {
fmt.Println(node)
}
}
7 changes: 7 additions & 0 deletions host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

type Host interface {
Name() string
Content() []byte
Labels() map[string]string
}
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package main

import (
"github.com/timshannon/bolthold"
"gohost/dal"
)

func main() {
//cmd.Execute()
store, err := dal.New(&dal.Options{
store, err := NewStore(&StoreOptions{
File: cfg.DBFile,
Options: &bolthold.Options{},
})
Expand Down
5 changes: 5 additions & 0 deletions remote_host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

type RemoteHost struct {

}
6 changes: 3 additions & 3 deletions dal/bbolt.go → store.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package dal
package main

import (
"github.com/timshannon/bolthold"
"os"
)

type Options struct {
type StoreOptions struct {
File string
*bolthold.Options
}

func New(opt *Options) (*bolthold.Store, error) {
func NewStore(opt *StoreOptions) (*bolthold.Store, error) {
return bolthold.Open(opt.File, os.ModePerm, opt.Options)
}
4 changes: 2 additions & 2 deletions dal/bbolt_test.go → store_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dal
package main

import (
"fmt"
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestBoltHold(t *testing.T) {
//err = store.Insert(bolthold.NextSequence(), "!23")
//a.NoError(err)

err = store.ForEach(bolthold.Where("Category").Eq("blue"), func(record *Item) error{
err = store.ForEach(bolthold.Where("Category").Eq("blue"), func(record *Item) error {
fmt.Println(record)
return nil
})
Expand Down
7 changes: 7 additions & 0 deletions sys_host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

type SysHost struct {
HostsEnable map[int]struct{}
}


0 comments on commit b646864

Please sign in to comment.