forked from x/icebergs
opt type
This commit is contained in:
parent
9f0b85bae3
commit
fe6226481e
124
type.go
124
type.go
@ -1,27 +1,29 @@
|
|||||||
package ice
|
package ice
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/shylinux/toolkits"
|
||||||
|
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Cache struct {
|
type Cache struct {
|
||||||
Value string
|
|
||||||
Name string
|
Name string
|
||||||
Help string
|
Help string
|
||||||
Hand func(m *Message, x *Cache, arg ...string) string
|
Value string
|
||||||
}
|
}
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Value interface{}
|
|
||||||
Name string
|
Name string
|
||||||
Help string
|
Help string
|
||||||
Hand func(m *Message, x *Config, arg ...string) string
|
Value interface{}
|
||||||
}
|
}
|
||||||
type Command struct {
|
type Command struct {
|
||||||
Form map[string]int
|
|
||||||
Name string
|
Name string
|
||||||
Help interface{}
|
Help interface{}
|
||||||
Auto func(m *Message, c *Context, key string, arg ...string) (ok bool)
|
Form map[string]int
|
||||||
Hand func(m *Message, c *Context, key string, arg ...string) (e error)
|
Hand func(m *Message, c *Context, key string, arg ...string)
|
||||||
}
|
}
|
||||||
type Context struct {
|
type Context struct {
|
||||||
Name string
|
Name string
|
||||||
@ -36,7 +38,7 @@ type Context struct {
|
|||||||
root *Context
|
root *Context
|
||||||
|
|
||||||
exit chan bool
|
exit chan bool
|
||||||
Server
|
server Server
|
||||||
}
|
}
|
||||||
type Server interface {
|
type Server interface {
|
||||||
Spawn(m *Message, c *Context, arg ...string) Server
|
Spawn(m *Message, c *Context, arg ...string) Server
|
||||||
@ -49,8 +51,8 @@ type Message struct {
|
|||||||
time time.Time
|
time time.Time
|
||||||
code int
|
code int
|
||||||
|
|
||||||
Meta map[string][]string
|
meta map[string][]string
|
||||||
Data map[string]interface{}
|
data map[string]interface{}
|
||||||
|
|
||||||
messages []*Message
|
messages []*Message
|
||||||
message *Message
|
message *Message
|
||||||
@ -60,3 +62,105 @@ type Message struct {
|
|||||||
target *Context
|
target *Context
|
||||||
Hand bool
|
Hand bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Message) Spawn(arg ...interface{}) *Message {
|
||||||
|
msg := &Message{
|
||||||
|
time: time.Now(),
|
||||||
|
code: -1,
|
||||||
|
|
||||||
|
meta: map[string][]string{},
|
||||||
|
data: map[string]interface{}{},
|
||||||
|
|
||||||
|
message: m,
|
||||||
|
root: m.root,
|
||||||
|
|
||||||
|
source: m.target,
|
||||||
|
target: m.target,
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(arg) > 0 {
|
||||||
|
switch val := arg[0].(type) {
|
||||||
|
case *Context:
|
||||||
|
msg.target = val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
func (m *Message) Log(level string, str string, arg ...interface{}) {
|
||||||
|
fmt.Fprintf(os.Stderr, "%s %s %s\n", time.Now().Format("2006-01-02 15:04:05"), level, fmt.Sprintf(str, arg...))
|
||||||
|
}
|
||||||
|
func (m *Message) Confv(arg ...interface{}) interface{} {
|
||||||
|
key := ""
|
||||||
|
switch val := arg[0].(type) {
|
||||||
|
case string:
|
||||||
|
key, arg = val, arg[1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range []*Context{m.target} {
|
||||||
|
for c := s; c != nil; c = c.context {
|
||||||
|
if conf, ok := c.Configs[key]; ok {
|
||||||
|
m.Log("conf", "%s.%s", c.Name, key)
|
||||||
|
return kit.Value(conf.Value, key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (m *Message) Conf(arg ...interface{}) string {
|
||||||
|
return kit.Format(m.Confv(arg...))
|
||||||
|
}
|
||||||
|
func (m *Message) Cmd(arg ...interface{}) *Message {
|
||||||
|
list := kit.Trans(arg...)
|
||||||
|
if len(list) == 0 {
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
for _, s := range []*Context{m.target} {
|
||||||
|
for c := s; c != nil; c = c.context {
|
||||||
|
if cmd, ok := c.Commands[list[0]]; ok {
|
||||||
|
m.Log("cmd", "%s.%s", c.Name, list[0])
|
||||||
|
cmd.Hand(m, c, list[0], list[1:]...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
func (m *Message) Echo(str string, arg ...interface{}) *Message {
|
||||||
|
m.meta["result"] = append(m.meta["result"], fmt.Sprintf(str, arg...))
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
func (m *Message) Result(arg ...interface{}) string {
|
||||||
|
return strings.Join(m.meta["result"], "")
|
||||||
|
}
|
||||||
|
|
||||||
|
var Pulse = &Message{
|
||||||
|
time: time.Now(), code: 0,
|
||||||
|
meta: map[string][]string{},
|
||||||
|
data: map[string]interface{}{},
|
||||||
|
|
||||||
|
messages: []*Message{}, message: nil, root: nil,
|
||||||
|
source: Index, target: Index, Hand: true,
|
||||||
|
}
|
||||||
|
var Index = &Context{Name: "root", Help: "元始模块",
|
||||||
|
Caches: map[string]*Cache{},
|
||||||
|
Configs: map[string]*Config{},
|
||||||
|
Commands: map[string]*Command{
|
||||||
|
"hi": {Name: "hi", Help: "hello", Hand: func(m *Message, c *Context, cmd string, arg ...string) {
|
||||||
|
m.Echo("hello world")
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Context) Register(s *Context, x Server) *Context {
|
||||||
|
if c.contexts == nil {
|
||||||
|
c.contexts = map[string]*Context{}
|
||||||
|
}
|
||||||
|
c.contexts[s.Name] = s
|
||||||
|
s.root = c.root
|
||||||
|
s.context = c
|
||||||
|
s.server = x
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
func init() {
|
||||||
|
Index.root = Index
|
||||||
|
Pulse.root = Pulse
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user