This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
157 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package main | ||
|
||
type RemoteHost struct { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
type SysHost struct { | ||
HostsEnable map[int]struct{} | ||
} | ||
|
||
|