forked from x/icebergs
opt base
This commit is contained in:
parent
efe0a13e06
commit
617b6df05a
@ -21,25 +21,11 @@ func _command_list(m *ice.Message, name string) *ice.Message {
|
||||
}
|
||||
return m.Sort(mdb.KEY)
|
||||
}
|
||||
if nfs.ExistsFile(m, path.Join(ice.SRC, name)) {
|
||||
switch kit.Ext(name) {
|
||||
case nfs.GO:
|
||||
name = GetFileCmd(name)
|
||||
case nfs.JS:
|
||||
m.Push(DISPLAY, FileURI(name))
|
||||
name = kit.Select(ice.CAN_PLUGIN, GetFileCmd(name))
|
||||
default:
|
||||
if msg := m.Cmd(mdb.RENDER, kit.Ext(name)); msg.Length() > 0 {
|
||||
m.Push(ARGS, kit.Format(kit.List(name)))
|
||||
name = kit.Keys(msg.Append(mdb.TEXT), msg.Append(mdb.NAME))
|
||||
}
|
||||
}
|
||||
}
|
||||
if strings.HasPrefix(name, "can.") {
|
||||
return m.Push(mdb.INDEX, name).Push(mdb.NAME, name).Push(mdb.HELP, "").Push(mdb.META, "").Push(mdb.LIST, "")
|
||||
}
|
||||
m.Spawn(m.Source()).Search(name, func(p *ice.Context, s *ice.Context, key string, cmd *ice.Command) {
|
||||
m.Push(mdb.INDEX, kit.Keys(s.Cap(ice.CTX_FOLLOW), key))
|
||||
m.Push(mdb.INDEX, kit.Keys(s.Prefix(), key))
|
||||
m.Push(mdb.NAME, kit.Format(cmd.Name))
|
||||
m.Push(mdb.HELP, kit.Format(cmd.Help))
|
||||
m.Push(mdb.META, kit.Format(cmd.Meta))
|
||||
@ -49,15 +35,11 @@ func _command_list(m *ice.Message, name string) *ice.Message {
|
||||
}
|
||||
func _command_search(m *ice.Message, kind, name, text string) {
|
||||
m.Travel(func(p *ice.Context, s *ice.Context, key string, cmd *ice.Command) {
|
||||
if IsOrderCmd(key) {
|
||||
if IsOrderCmd(key) || !strings.Contains(s.Prefix(key), name) {
|
||||
return
|
||||
}
|
||||
if name != "" && !strings.HasPrefix(key, name) && !strings.Contains(s.Name, name) {
|
||||
return
|
||||
}
|
||||
m.PushSearch(ice.CTX, kit.PathName(1), ice.CMD, kit.FileName(1), kit.SimpleKV("", s.Cap(ice.CTX_FOLLOW), cmd.Name, cmd.Help),
|
||||
CONTEXT, s.Cap(ice.CTX_FOLLOW), COMMAND, key, INDEX, kit.Keys(s.Cap(ice.CTX_FOLLOW), key),
|
||||
mdb.HELP, cmd.Help, nfs.FILE, FileURI(cmd.FileLine()),
|
||||
m.PushSearch(ice.CTX, kit.PathName(1), ice.CMD, kit.FileName(1), kit.SimpleKV("", s.Prefix(), cmd.Name, cmd.Help),
|
||||
CONTEXT, s.Prefix(), COMMAND, key, INDEX, kit.Keys(s.Prefix(), key), mdb.HELP, cmd.Help, nfs.FILE, FileURI(cmd.FileLine()),
|
||||
)
|
||||
})
|
||||
m.Sort(m.OptionFields())
|
||||
@ -69,7 +51,6 @@ const (
|
||||
STYLE = "style"
|
||||
DISPLAY = "display"
|
||||
ACTION = "action"
|
||||
TRANS = "trans"
|
||||
)
|
||||
const COMMAND = "command"
|
||||
|
||||
@ -78,71 +59,58 @@ func init() {
|
||||
COMMAND: {Name: "command key auto", Help: "命令", Actions: ice.MergeActions(ice.Actions{
|
||||
ice.CTX_INIT: {Hand: func(m *ice.Message, arg ...string) {
|
||||
TravelCmd(m, func(key, file, line string) {
|
||||
if strings.Contains(file, ice.ICEBERGS) {
|
||||
AddFileCmd(file, key)
|
||||
}
|
||||
kit.If(strings.Contains(file, ice.ICEBERGS), func() { AddFileCmd(file, key) })
|
||||
})
|
||||
}},
|
||||
mdb.SEARCH: {Hand: func(m *ice.Message, arg ...string) {
|
||||
if arg[0] == m.CommandKey() || len(arg) > 1 && arg[1] != "" {
|
||||
if arg[0] == m.CommandKey() || arg[1] != "" {
|
||||
_command_search(m, arg[0], kit.Select("", arg, 1), kit.Select("", arg, 2))
|
||||
}
|
||||
}},
|
||||
mdb.EXPORT: {Hand: func(m *ice.Message, arg ...string) {
|
||||
TravelCmd(m, func(key, file, line string) {
|
||||
m.Push(mdb.NAME, key).Push(nfs.FILE, file).Push(nfs.LINE, line)
|
||||
}).Sort(mdb.NAME).Table(func(value ice.Maps) {
|
||||
TravelCmd(m, func(key, file, line string) { m.Push(mdb.NAME, key).Push(nfs.FILE, file).Push(nfs.LINE, line) }).Sort(mdb.NAME).Table(func(value ice.Maps) {
|
||||
m.Echo(`%s %s %s;" f`+ice.NL, value[mdb.NAME], value[nfs.FILE], value[nfs.LINE])
|
||||
}).Cmd(nfs.SAVE, "tags", m.Result())
|
||||
}).Cmd(nfs.SAVE, nfs.TAGS, m.Result())
|
||||
}},
|
||||
}, aaa.RoleAction()), Hand: func(m *ice.Message, arg ...string) {
|
||||
if len(arg) == 0 {
|
||||
arg = append(arg, "")
|
||||
}
|
||||
for _, key := range arg {
|
||||
_command_list(m, key)
|
||||
}
|
||||
kit.If(len(arg) == 0, func() { arg = append(arg, "") })
|
||||
kit.For(arg, func(k string) { _command_list(m, k) })
|
||||
}},
|
||||
})
|
||||
}
|
||||
|
||||
func Run(m *ice.Message, arg ...string) {
|
||||
if !PodCmd(m, arg) && aaa.Right(m, arg) {
|
||||
m.Cmdy(arg)
|
||||
}
|
||||
}
|
||||
|
||||
var Upload = func(*ice.Message) []string { return nil }
|
||||
|
||||
func PodCmd(m *ice.Message, arg ...ice.Any) bool {
|
||||
Upload(m)
|
||||
if pod := m.Option(ice.POD); pod != "" {
|
||||
m.Option(ice.POD, "")
|
||||
m.Cmdy(append(kit.List(ice.SPACE, pod), arg...)...)
|
||||
m.Options(ice.POD, "").Cmdy(append(kit.List(ice.SPACE, pod), arg...)...)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
func Run(m *ice.Message, arg ...string) {
|
||||
kit.If(!PodCmd(m, arg) && aaa.Right(m, arg), func() { m.Cmdy(arg) })
|
||||
}
|
||||
func CmdHandler(args ...ice.Any) ice.Handler {
|
||||
return func(m *ice.Message, arg ...string) { m.Cmdy(args...) }
|
||||
}
|
||||
func CmdAction(args ...ice.Any) ice.Actions {
|
||||
return ice.Actions{ice.CTX_INIT: mdb.AutoConfig(args...),
|
||||
COMMAND: {Hand: func(m *ice.Message, arg ...string) {
|
||||
if !PodCmd(m, COMMAND, arg) {
|
||||
m.Cmdy(COMMAND, arg)
|
||||
}
|
||||
}}, ice.RUN: {Hand: Run},
|
||||
return ice.Actions{ice.CTX_INIT: mdb.AutoConfig(args...), ice.RUN: {Hand: Run},
|
||||
COMMAND: {Hand: func(m *ice.Message, arg ...string) { kit.If(!PodCmd(m, COMMAND, arg), func() { m.Cmdy(COMMAND, arg) }) }},
|
||||
}
|
||||
}
|
||||
func CmdList(m *ice.Message) *ice.Message {
|
||||
return m.Cmdy(COMMAND, mdb.SEARCH, COMMAND, ice.OptionFields(INDEX))
|
||||
}
|
||||
|
||||
func IsOrderCmd(key string) bool { return key[0] == '/' || key[0] == '_' }
|
||||
func FileURI(dir string) string {
|
||||
if dir == "" {
|
||||
return ""
|
||||
} else if strings.Contains(dir, "/go/pkg/mod/") {
|
||||
return path.Join(ice.PS, ice.REQUIRE, strings.Split(dir, "/go/pkg/mod/")[1])
|
||||
}
|
||||
if path.IsAbs(dir) {
|
||||
dir = strings.Split(dir, "/go/pkg/mod/")[1]
|
||||
} else if path.IsAbs(dir) {
|
||||
if strings.HasPrefix(dir, kit.Path("")+ice.PS) {
|
||||
dir = strings.TrimPrefix(dir, kit.Path("")+ice.PS)
|
||||
} else if ice.Info.Make.Path != "" && strings.HasPrefix(dir, ice.Info.Make.Path+ice.PS) {
|
||||
@ -156,12 +124,10 @@ func FileURI(dir string) string {
|
||||
func FileCmd(dir string) string { return FileURI(kit.ExtChange(strings.Split(dir, ice.DF)[0], nfs.GO)) }
|
||||
func AddFileCmd(dir, key string) {
|
||||
ice.Info.File[FileCmd(dir)] = key
|
||||
ls := strings.SplitN(path.Join(kit.Slice(strings.Split(FileCmd(dir), ice.PS), 2, 5)...), ice.AT, 2)
|
||||
if len(ls) > 1 {
|
||||
if ls := strings.SplitN(path.Join(kit.Slice(strings.Split(FileCmd(dir), ice.PS), 2, 5)...), ice.AT, 2); len(ls) > 1 {
|
||||
ice.Info.Gomod[ls[0]] = ls[1]
|
||||
}
|
||||
}
|
||||
func IsOrderCmd(key string) bool { return key[0] == '/' || key[0] == '_' }
|
||||
func GetFileCmd(dir string) string {
|
||||
if strings.HasPrefix(dir, ice.REQUIRE+ice.PS) {
|
||||
dir = ice.PS + dir
|
||||
@ -193,11 +159,8 @@ func TravelCmd(m *ice.Message, cb func(key, file, line string)) *ice.Message {
|
||||
return
|
||||
}
|
||||
if ls := kit.Split(cmd.FileLine(), ice.DF); len(ls) > 0 && cmd.Name != "" {
|
||||
cb(kit.Keys(s.Cap(ice.CTX_FOLLOW), key), strings.TrimPrefix(ls[0], kit.Path("")+ice.PS), kit.Select("1", ls, 1))
|
||||
cb(kit.Keys(s.Prefix(), key), strings.TrimPrefix(ls[0], kit.Path("")+ice.PS), kit.Select("1", ls, 1))
|
||||
}
|
||||
})
|
||||
return m
|
||||
}
|
||||
func CmdList(m *ice.Message) *ice.Message {
|
||||
return m.Cmdy(COMMAND, mdb.SEARCH, COMMAND, ice.OptionFields(INDEX))
|
||||
}
|
||||
|
@ -56,9 +56,7 @@ func _config_load(m *ice.Message, name string, arg ...string) {
|
||||
json.NewDecoder(f).Decode(&data)
|
||||
for k, v := range data {
|
||||
msg.Search(k, func(p *ice.Context, s *ice.Context, key string, conf *ice.Config) {
|
||||
if s.Configs[key] == nil {
|
||||
s.Configs[key] = &ice.Config{}
|
||||
}
|
||||
kit.If(s.Configs[key] == nil, func() { s.Configs[key] = &ice.Config{} })
|
||||
s.Configs[key].Value = v
|
||||
})
|
||||
}
|
||||
@ -67,9 +65,7 @@ func _config_load(m *ice.Message, name string, arg ...string) {
|
||||
func _config_make(m *ice.Message, key string, arg ...string) {
|
||||
msg := m.Spawn(m.Source())
|
||||
if len(arg) > 1 {
|
||||
if strings.HasPrefix(arg[1], ice.AT) {
|
||||
arg[1] = msg.Cmdx(nfs.CAT, arg[1][1:])
|
||||
}
|
||||
kit.If(strings.HasPrefix(arg[1], ice.AT), func() { arg[1] = msg.Cmdx(nfs.CAT, arg[1][1:]) })
|
||||
mdb.Confv(msg, key, arg[0], kit.Parse(nil, "", arg[1:]...))
|
||||
}
|
||||
if len(arg) > 0 {
|
||||
@ -116,7 +112,6 @@ func init() {
|
||||
_config_list(m)
|
||||
} else {
|
||||
_config_make(m, arg[0], arg[1:]...)
|
||||
// DisplayStoryJSON(m)
|
||||
}
|
||||
}},
|
||||
})
|
||||
@ -126,42 +121,24 @@ func init() {
|
||||
ice.Info.Load = Load
|
||||
}
|
||||
func Save(m *ice.Message, arg ...string) *ice.Message {
|
||||
if len(arg) == 0 {
|
||||
for k := range m.Target().Configs {
|
||||
arg = append(arg, k)
|
||||
}
|
||||
}
|
||||
for i, k := range arg {
|
||||
arg[i] = m.Prefix(k)
|
||||
}
|
||||
kit.If(len(arg) == 0, func() { arg = kit.SortedKey(m.Target().Configs) })
|
||||
kit.For(arg, func(i int, k string) { arg[i] = m.Prefix(k) })
|
||||
return m.Cmd(CONFIG, SAVE, m.Prefix(nfs.JSON), arg)
|
||||
}
|
||||
func Load(m *ice.Message, arg ...string) *ice.Message {
|
||||
if len(arg) == 0 {
|
||||
for k := range m.Target().Configs {
|
||||
arg = append(arg, k)
|
||||
}
|
||||
}
|
||||
for i, k := range arg {
|
||||
arg[i] = m.Prefix(k)
|
||||
}
|
||||
kit.If(len(arg) == 0, func() { arg = kit.SortedKey(m.Target().Configs) })
|
||||
kit.For(arg, func(i int, k string) { arg[i] = m.Prefix(k) })
|
||||
return m.Cmd(CONFIG, LOAD, m.Prefix(nfs.JSON), arg)
|
||||
}
|
||||
func ConfAction(arg ...ice.Any) ice.Actions {
|
||||
return ice.Actions{ice.CTX_INIT: mdb.AutoConfig(arg...)}
|
||||
}
|
||||
func ConfAction(arg ...ice.Any) ice.Actions { return ice.Actions{ice.CTX_INIT: mdb.AutoConfig(arg...)} }
|
||||
func ConfigSimple(m *ice.Message, key ...string) (res []string) {
|
||||
kit.For(kit.Split(kit.Join(key)), func(k string) { res = append(res, k, mdb.Config(m, k)) })
|
||||
return
|
||||
}
|
||||
func ConfigFromOption(m *ice.Message, arg ...string) {
|
||||
for _, k := range arg {
|
||||
mdb.Config(m, k, kit.Select(mdb.Config(m, k), m.Option(k)))
|
||||
}
|
||||
kit.For(arg, func(k string) { mdb.Config(m, k, kit.Select(mdb.Config(m, k), m.Option(k))) })
|
||||
}
|
||||
func OptionFromConfig(m *ice.Message, arg ...string) string {
|
||||
for _, key := range arg {
|
||||
m.Option(key, mdb.Config(m, key))
|
||||
}
|
||||
kit.For(arg, func(k string) { m.Option(k, mdb.Config(m, k)) })
|
||||
return m.Option(arg[0])
|
||||
}
|
||||
|
@ -10,11 +10,10 @@ import (
|
||||
|
||||
func _context_list(m *ice.Message, sub *ice.Context, name string) {
|
||||
m.Travel(func(p *ice.Context, s *ice.Context) {
|
||||
if name != "" && name != ice.ICE && !strings.HasPrefix(s.Cap(ice.CTX_FOLLOW), name+ice.PT) {
|
||||
if name != "" && name != ice.ICE && !strings.HasPrefix(s.Prefix(), name+ice.PT) {
|
||||
return
|
||||
}
|
||||
m.Push(mdb.NAME, s.Cap(ice.CTX_FOLLOW))
|
||||
m.Push(mdb.HELP, s.Help)
|
||||
m.Push(mdb.NAME, s.Prefix()).Push(mdb.HELP, s.Help)
|
||||
})
|
||||
}
|
||||
|
||||
@ -23,9 +22,7 @@ const CONTEXT = "context"
|
||||
func init() {
|
||||
Index.MergeCommands(ice.Commands{
|
||||
CONTEXT: {Name: "context name=web action=context,command,config key auto", Help: "模块", Hand: func(m *ice.Message, arg ...string) {
|
||||
if len(arg) == 0 {
|
||||
arg = append(arg, m.Source().Cap(ice.CTX_FOLLOW))
|
||||
}
|
||||
kit.If(len(arg) == 0, func() { arg = append(arg, m.Source().Prefix()) })
|
||||
m.Search(arg[0]+ice.PT, func(p *ice.Context, s *ice.Context) {
|
||||
msg := m.Spawn(s)
|
||||
defer m.Copy(msg)
|
||||
@ -41,17 +38,3 @@ func init() {
|
||||
}},
|
||||
})
|
||||
}
|
||||
func Inputs(m *ice.Message, field string) bool {
|
||||
switch strings.TrimPrefix(field, "extra.") {
|
||||
case ice.POD:
|
||||
m.Cmdy(ice.SPACE)
|
||||
case ice.CTX:
|
||||
m.Cmdy(CONTEXT)
|
||||
case ice.CMD:
|
||||
m.Cmdy(CONTEXT, kit.Select(m.Option(ice.CTX), m.Option(kit.Keys(mdb.EXTRA, ice.CTX))), COMMAND)
|
||||
case ice.ARG:
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
@ -16,13 +16,12 @@ type displayMessage interface {
|
||||
Action(arg ...ice.Any) *ice.Message
|
||||
}
|
||||
|
||||
func isLocalFile(p string) bool {
|
||||
return !strings.HasPrefix(p, ice.PS) && !strings.HasPrefix(p, ice.HTTP)
|
||||
}
|
||||
func Display(m displayMessage, file string, arg ...ice.Any) displayMessage {
|
||||
if file == "" {
|
||||
file = kit.Keys(kit.FileName(2), nfs.JS)
|
||||
}
|
||||
if !strings.HasPrefix(file, ice.PS) && !strings.HasPrefix(file, ice.HTTP) {
|
||||
file = path.Join(ice.PS, path.Join(path.Dir(FileURI(logs.FileLines(2))), file))
|
||||
}
|
||||
kit.If(file == "", func() { file = kit.Keys(kit.FileName(5), nfs.JS) })
|
||||
kit.If(isLocalFile(file), func() { file = path.Join(ice.PS, path.Join(path.Dir(FileURI(logs.FileLines(2))), file)) })
|
||||
return DisplayBase(m, file, arg...)
|
||||
}
|
||||
func DisplayTable(m displayMessage, arg ...ice.Any) displayMessage {
|
||||
@ -32,12 +31,8 @@ func DisplayTableCard(m displayMessage, arg ...ice.Any) displayMessage {
|
||||
return DisplayTable(m, "style", "card")
|
||||
}
|
||||
func DisplayStory(m displayMessage, file string, arg ...ice.Any) displayMessage {
|
||||
if file == "" {
|
||||
file = kit.ExtChange(kit.FileName(2), nfs.JS)
|
||||
}
|
||||
if !strings.HasPrefix(file, ice.PS) && !strings.HasPrefix(file, ice.HTTP) {
|
||||
file = path.Join(ice.PLUGIN_STORY, file)
|
||||
}
|
||||
kit.If(file == "", func() { file = kit.Keys(kit.FileName(5), nfs.JS) })
|
||||
kit.If(isLocalFile(file), func() { file = path.Join(ice.PLUGIN_STORY, file) })
|
||||
return DisplayBase(m, file, arg...)
|
||||
}
|
||||
func DisplayStoryJSON(m displayMessage, arg ...ice.Any) displayMessage {
|
||||
@ -47,16 +42,12 @@ func DisplayStorySpide(m displayMessage, arg ...ice.Any) displayMessage {
|
||||
return DisplayStory(m, "spide", arg...)
|
||||
}
|
||||
func DisplayLocal(m displayMessage, file string, arg ...ice.Any) displayMessage {
|
||||
if file == "" {
|
||||
file = path.Join(kit.PathName(2), kit.Keys(kit.FileName(2), ice.JS))
|
||||
}
|
||||
if !strings.HasPrefix(file, ice.PS) && !strings.HasPrefix(file, ice.HTTP) {
|
||||
file = path.Join(ice.PLUGIN_LOCAL, file)
|
||||
}
|
||||
kit.If(file == "", func() { file = path.Join(kit.PathName(5), kit.Keys(kit.FileName(5), nfs.JS)) })
|
||||
kit.If(isLocalFile(file), func() { file = path.Join(ice.PLUGIN_LOCAL, file) })
|
||||
return DisplayBase(m, file, arg...)
|
||||
}
|
||||
func DisplayBase(m displayMessage, file string, arg ...ice.Any) displayMessage {
|
||||
m.Option(ice.MSG_DISPLAY, kit.MergeURL(kit.Select(kit.ExtChange(file, nfs.JS), file, strings.Contains(file, "?")), arg...))
|
||||
m.Option(ice.MSG_DISPLAY, kit.MergeURL(kit.Select(kit.ExtChange(file, nfs.JS), file, strings.Contains(file, ice.QS)), arg...))
|
||||
return m
|
||||
}
|
||||
func Toolkit(m *ice.Message, arg ...string) {
|
||||
|
@ -29,32 +29,22 @@ func init() {
|
||||
m.Cmdy(kit.Split(value[ice.CMD]), arg[1], arg[2:], ice.OptionFields(""))
|
||||
})
|
||||
}},
|
||||
}, mdb.ZoneAction(mdb.SHORT, EVENT, mdb.FIELD, "time,id,cmd"))},
|
||||
}, mdb.ZoneAction(mdb.SHORT, EVENT, mdb.FIELD, "time,id,cmd"), mdb.ClearOnExitHashAction())},
|
||||
})
|
||||
}
|
||||
func EventAction(arg ...string) ice.Actions {
|
||||
return ice.Actions{
|
||||
ice.CTX_INIT: {Hand: func(m *ice.Message, _ ...string) {
|
||||
for _, v := range arg {
|
||||
Watch(m, v)
|
||||
}
|
||||
}},
|
||||
}
|
||||
return ice.Actions{ice.CTX_INIT: {Hand: func(m *ice.Message, _ ...string) { kit.For(arg, func(k string) { Watch(m, k) }) }}}
|
||||
}
|
||||
func EventsAction(arg ...string) ice.Actions {
|
||||
list := kit.DictList(arg...)
|
||||
return ice.Actions{ice.CTX_INIT: {Hand: func(m *ice.Message, arg ...string) {
|
||||
for sub := range m.Target().Commands[m.CommandKey()].Actions {
|
||||
if list[sub] == ice.TRUE {
|
||||
Watch(m, sub)
|
||||
}
|
||||
kit.If(list[sub] == ice.TRUE, func() { Watch(m, sub) })
|
||||
}
|
||||
}}}
|
||||
}
|
||||
func Watch(m *ice.Message, key string, arg ...string) *ice.Message {
|
||||
if len(arg) == 0 {
|
||||
arg = append(arg, m.PrefixKey())
|
||||
}
|
||||
kit.If(len(arg) == 0, func() { arg = append(arg, m.PrefixKey()) })
|
||||
return m.Cmd(EVENT, LISTEN, EVENT, key, ice.CMD, kit.Join(arg, ice.SP))
|
||||
}
|
||||
func Event(m *ice.Message, key string, arg ...ice.Any) *ice.Message {
|
||||
|
@ -15,29 +15,20 @@ import (
|
||||
|
||||
type Frame struct{ s chan os.Signal }
|
||||
|
||||
func (f *Frame) listen(m *ice.Message, s int, arg ...string) {
|
||||
signal.Notify(f.s, syscall.Signal(s))
|
||||
mdb.HashCreate(m, SIGNAL, s, arg)
|
||||
}
|
||||
func (f *Frame) Begin(m *ice.Message, arg ...string) {
|
||||
f.s = make(chan os.Signal, 10)
|
||||
}
|
||||
func (f *Frame) Begin(m *ice.Message, arg ...string) { f.s = make(chan os.Signal, 10) }
|
||||
func (f *Frame) Start(m *ice.Message, arg ...string) {
|
||||
kit.If(ice.Info.PidPath, func(p string) {
|
||||
if f, p, e := logs.CreateFile(p); e == nil {
|
||||
defer f.Close()
|
||||
m.Logs("save", "file", p, PID, os.Getpid())
|
||||
fmt.Fprint(f, os.Getpid())
|
||||
m.Logs("save", PID, p)
|
||||
}
|
||||
})
|
||||
t := kit.Duration(mdb.Conf(m, TIMER, kit.Keym(TICK)))
|
||||
enable := mdb.Conf(m, TIMER, kit.Keym("enable")) == ice.TRUE
|
||||
for {
|
||||
select {
|
||||
case <-time.Tick(t):
|
||||
if enable {
|
||||
m.Cmd(TIMER, HAPPEN)
|
||||
}
|
||||
case s, ok := <-f.s:
|
||||
if !ok {
|
||||
return
|
||||
@ -47,12 +38,13 @@ func (f *Frame) Start(m *ice.Message, arg ...string) {
|
||||
}
|
||||
}
|
||||
func (f *Frame) Close(m *ice.Message, arg ...string) { close(f.s) }
|
||||
func (f *Frame) listen(m *ice.Message, s int, arg ...string) {
|
||||
signal.Notify(f.s, syscall.Signal(s))
|
||||
mdb.HashCreate(m, SIGNAL, s, arg)
|
||||
}
|
||||
|
||||
const GDB = "gdb"
|
||||
|
||||
var Index = &ice.Context{Name: GDB, Help: "事件模块", Commands: ice.Commands{
|
||||
ice.CTX_INIT: {Hand: func(m *ice.Message, arg ...string) { ice.Info.Load(m, TIMER, ROUTINE) }},
|
||||
ice.CTX_EXIT: {Hand: func(m *ice.Message, arg ...string) { ice.Info.Save(m, TIMER, ROUTINE) }},
|
||||
}}
|
||||
var Index = &ice.Context{Name: GDB, Help: "事件模块"}
|
||||
|
||||
func init() { ice.Index.Register(Index, &Frame{}, SIGNAL, EVENT, TIMER, ROUTINE) }
|
||||
|
@ -12,10 +12,10 @@ const ROUTINE = "routine"
|
||||
func init() {
|
||||
Index.MergeCommands(ice.Commands{
|
||||
ROUTINE: {Name: "routine hash auto prunes", Help: "协程池", Actions: ice.MergeActions(ice.Actions{
|
||||
mdb.CREATE: {Name: "create name", Hand: func(m *ice.Message, arg ...string) {
|
||||
mdb.CREATE: {Name: "create name cmd", Hand: func(m *ice.Message, arg ...string) {
|
||||
m.Go(func() {
|
||||
cb := m.OptionCB("")
|
||||
h := mdb.HashCreate(m, m.OptionSimple(mdb.NAME), mdb.STATUS, START, ice.CMD, logs.FileLines(cb))
|
||||
h := mdb.HashCreate(m, m.OptionSimple(mdb.NAME, ice.CMD), mdb.STATUS, START)
|
||||
defer func() {
|
||||
if e := recover(); e == nil {
|
||||
mdb.HashModify(m, mdb.HASH, h, mdb.STATUS, STOP)
|
||||
@ -24,17 +24,22 @@ func init() {
|
||||
}
|
||||
}()
|
||||
switch cb := cb.(type) {
|
||||
case []string:
|
||||
m.Cmd(kit.Split(kit.Join(cb)))
|
||||
case string:
|
||||
m.Cmd(kit.Split(cb))
|
||||
case []string:
|
||||
m.Cmd(kit.Split(kit.Join(cb)))
|
||||
case func(*ice.Message):
|
||||
cb(m.Spawn(m.Source()))
|
||||
case func():
|
||||
cb()
|
||||
default:
|
||||
m.ErrorNotImplement(cb)
|
||||
}
|
||||
})
|
||||
}, m.Option(mdb.NAME))
|
||||
}},
|
||||
}, mdb.StatusHashAction(mdb.FIELD, "time,hash,status,name,cmd"))},
|
||||
}, mdb.StatusHashAction(mdb.FIELD, "time,hash,status,name,cmd"), mdb.ClearOnExitHashAction())},
|
||||
})
|
||||
}
|
||||
func Go(m *ice.Message, cb ice.Any, arg ...string) {
|
||||
m.Cmd(ROUTINE, mdb.CREATE, kit.Select(m.PrefixKey(), arg, 0), logs.FileLine(cb), cb)
|
||||
}
|
||||
|
@ -10,10 +10,7 @@ import (
|
||||
|
||||
func _timer_action(m *ice.Message, now time.Time, arg ...string) {
|
||||
mdb.HashSelects(m).Table(func(value ice.Maps) {
|
||||
if value[mdb.COUNT] == "0" {
|
||||
return
|
||||
}
|
||||
if value[mdb.TIME] > now.Format(ice.MOD_TIME) {
|
||||
if value[mdb.COUNT] == "0" || value[mdb.TIME] > now.Format(ice.MOD_TIME) {
|
||||
return
|
||||
}
|
||||
m.Cmd(ROUTINE, mdb.CREATE, mdb.NAME, value[mdb.NAME], kit.Keycb(ROUTINE), value[ice.CMD])
|
||||
@ -34,9 +31,7 @@ func init() {
|
||||
mdb.CREATE: {Name: "create name*=hi delay=10ms interval=10s count=3 cmd*=runtime"},
|
||||
mdb.PRUNES: {Hand: func(m *ice.Message, arg ...string) { mdb.HashPrunesValue(m, mdb.COUNT, "0") }},
|
||||
HAPPEN: {Hand: func(m *ice.Message, arg ...string) { _timer_action(m, time.Now(), arg...) }},
|
||||
RESTART: {Name: "restart count=3", Hand: func(m *ice.Message, arg ...string) {
|
||||
mdb.HashModify(m, m.OptionSimple(mdb.HashShort(m)), arg)
|
||||
}},
|
||||
RESTART: {Name: "restart count=3", Hand: func(m *ice.Message, arg ...string) { mdb.HashModify(m, m.OptionSimple(mdb.HashShort(m)), arg) }},
|
||||
}, mdb.HashAction(mdb.FIELD, "time,hash,name,delay,interval,count,cmd", TICK, "60s"))},
|
||||
})
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import (
|
||||
"shylinux.com/x/icebergs/base/nfs"
|
||||
"shylinux.com/x/icebergs/base/tcp"
|
||||
kit "shylinux.com/x/toolkits"
|
||||
"shylinux.com/x/toolkits/logs"
|
||||
)
|
||||
|
||||
func _broad_send(m *ice.Message, to_host, to_port string, host, port string, arg ...string) {
|
||||
@ -23,7 +24,7 @@ func _broad_serve(m *ice.Message) {
|
||||
m.GoSleep("10ms", tcp.HOST, func(value ice.Maps) {
|
||||
_broad_send(m, "", "", value[aaa.IP], m.Option(tcp.PORT), gdb.EVENT, tcp.LISTEN, mdb.NAME, ice.Info.NodeName, mdb.TYPE, ice.Info.NodeType)
|
||||
})
|
||||
m.Cmd(tcp.SERVER, tcp.LISTEN, mdb.TYPE, tcp.UDP4, m.OptionSimple(mdb.NAME, tcp.HOST, tcp.PORT), func(from *net.UDPAddr, buf []byte) {
|
||||
m.Cmd(tcp.SERVER, tcp.LISTEN, mdb.TYPE, tcp.UDP4, mdb.NAME, logs.FileLine(1), m.OptionSimple(tcp.HOST, tcp.PORT), func(from *net.UDPAddr, buf []byte) {
|
||||
msg := m.Spawn(buf).Logs(tcp.RECV, BROAD, string(buf), nfs.FROM, from)
|
||||
if mdb.HashCreate(m, msg.OptionSimple(kit.Simple(msg.Optionv(ice.MSG_OPTION))...)); msg.Option(gdb.EVENT) == tcp.LISTEN {
|
||||
m.Cmds("", func(value ice.Maps) {
|
||||
@ -51,8 +52,10 @@ func init() {
|
||||
})
|
||||
}
|
||||
}},
|
||||
SERVE_START: {Hand: func(m *ice.Message, arg ...string) { m.Go(func() { m.Cmd("", SERVE, m.OptionSimple(tcp.PORT)) }) }},
|
||||
SERVE: {Name: "serve port=9020", Hand: func(m *ice.Message, arg ...string) { _broad_serve(m) }},
|
||||
SERVE_START: {Hand: func(m *ice.Message, arg ...string) {
|
||||
gdb.Go(m, _broad_serve)
|
||||
}},
|
||||
SERVE: {Name: "serve port=9020 host", Hand: func(m *ice.Message, arg ...string) { gdb.Go(m, _broad_serve) }},
|
||||
OPEN: {Hand: func(m *ice.Message, arg ...string) {
|
||||
ctx.ProcessOpen(m, Domain(m.Option(tcp.HOST), m.Option(tcp.PORT)))
|
||||
}},
|
||||
|
@ -25,7 +25,8 @@ func _serve_start(m *ice.Message) {
|
||||
kit.If(m.Option(tcp.PORT) == tcp.RANDOM, func() { m.Option(tcp.PORT, m.Cmdx(tcp.PORT, aaa.RIGHT)) })
|
||||
kit.If(cli.IsWindows(), func() { m.Cmd(SPIDE, ice.OPS, _serve_address(m)+"/exit").Sleep30ms() })
|
||||
cli.NodeInfo(m, kit.Select(ice.Info.Hostname, m.Option(tcp.NODENAME)), SERVER)
|
||||
m.Target().Start(m, m.OptionSimple(tcp.HOST, tcp.PORT)...)
|
||||
m.Start("", m.OptionSimple(tcp.HOST, tcp.PORT)...)
|
||||
// m.Target().Start(m, m.OptionSimple(tcp.HOST, tcp.PORT)...)
|
||||
}
|
||||
func _serve_main(m *ice.Message, w http.ResponseWriter, r *http.Request) bool {
|
||||
const (
|
||||
|
@ -26,7 +26,7 @@ func _space_qrcode(m *ice.Message, dev string) {
|
||||
func _space_dial(m *ice.Message, dev, name string, arg ...string) {
|
||||
u := kit.ParseURL(kit.MergeURL2(strings.Replace(m.Cmdv(SPIDE, dev, CLIENT_ORIGIN), HTTP, "ws", 1), PP(SPACE), mdb.TYPE, ice.Info.NodeType, mdb.NAME, name, arg))
|
||||
args := kit.SimpleKV("type,name,host,port", u.Scheme, dev, u.Hostname(), u.Port())
|
||||
m.Go(func() {
|
||||
gdb.Go(m, func() {
|
||||
once := sync.Once{}
|
||||
redial := kit.Dict(mdb.Configv(m, REDIAL))
|
||||
a, b, c := kit.Int(redial["a"]), kit.Int(redial["b"]), kit.Int(redial["c"])
|
||||
@ -48,7 +48,7 @@ func _space_fork(m *ice.Message) {
|
||||
name := kit.ReplaceAll(kit.Select(addr, m.Option(mdb.NAME)), "[", "_", "]", "_", ice.DF, "_", ice.PT, "_")
|
||||
args := kit.Simple(mdb.TYPE, kit.Select(WORKER, m.Option(mdb.TYPE)), mdb.NAME, name, mdb.TEXT, kit.Select(addr, m.Option(mdb.TEXT)), m.OptionSimple(cli.DAEMON, ice.MSG_USERUA))
|
||||
if c, e := websocket.Upgrade(m.W, m.R); !m.Warn(e) {
|
||||
m.Go(func() {
|
||||
gdb.Go(m, func() {
|
||||
defer mdb.HashCreateDeferRemove(m, args, kit.Dict(mdb.TARGET, c))()
|
||||
switch m.Option(mdb.TYPE) {
|
||||
case WORKER:
|
||||
|
@ -12,6 +12,7 @@ import (
|
||||
"shylinux.com/x/icebergs/base/mdb"
|
||||
"shylinux.com/x/icebergs/base/tcp"
|
||||
kit "shylinux.com/x/toolkits"
|
||||
"shylinux.com/x/toolkits/logs"
|
||||
)
|
||||
|
||||
type Frame struct {
|
||||
@ -54,7 +55,7 @@ func (f *Frame) Start(m *ice.Message, arg ...string) {
|
||||
case func(http.Handler):
|
||||
cb(f)
|
||||
default:
|
||||
m.Cmd(tcp.SERVER, tcp.LISTEN, mdb.TYPE, HTTP, m.OptionSimple(mdb.NAME, tcp.HOST, tcp.PORT), func(l net.Listener) {
|
||||
m.Cmd(tcp.SERVER, tcp.LISTEN, mdb.TYPE, HTTP, mdb.NAME, logs.FileLine(1), m.OptionSimple(tcp.HOST, tcp.PORT), func(l net.Listener) {
|
||||
defer mdb.HashCreateDeferRemove(m, m.OptionSimple(mdb.NAME, tcp.PROTO), arg, cli.STATUS, tcp.START)()
|
||||
gdb.Event(m, SERVE_START, arg)
|
||||
m.Warn(f.Server.Serve(l))
|
||||
|
2
conf.go
2
conf.go
@ -289,6 +289,7 @@ const ( // ctx
|
||||
const ( // mdb
|
||||
SEARCH = "search"
|
||||
INPUTS = "inputs"
|
||||
CREATE = "create"
|
||||
SELECT = "select"
|
||||
|
||||
KEY = "key"
|
||||
@ -312,6 +313,7 @@ const ( // web
|
||||
)
|
||||
const ( // gdb
|
||||
EVENT = "event"
|
||||
ROUTINE = "routine"
|
||||
)
|
||||
const ( // nfs
|
||||
SOURCE = "source"
|
||||
|
@ -28,7 +28,7 @@ func _merge_url(m *ice.Message, domain, key string, arg ...ice.Any) string {
|
||||
if domain = strings.TrimSuffix(domain, ice.PS); strings.Contains(domain, "/chat/pod/") {
|
||||
domain += web.P(strings.TrimPrefix(m.Prefix(web.P(key)), "web.chat."))
|
||||
} else {
|
||||
domain += path.Join(strings.TrimPrefix(strings.Replace(m.Target().Cap(ice.CTX_FOLLOW), ice.PT, ice.PS, -1), "web"), path.Join(key))
|
||||
domain += path.Join(strings.TrimPrefix(strings.Replace(m.Target().Prefix(), ice.PT, ice.PS, -1), "web"), path.Join(key))
|
||||
}
|
||||
return kit.MergeURL(domain, arg...)
|
||||
}
|
||||
|
2
init.go
2
init.go
@ -97,7 +97,7 @@ func Run(arg ...string) string {
|
||||
Pulse.time = time.Now()
|
||||
switch Index.Merge(Index).Begin(Pulse, arg...); kit.Select("", arg, 0) {
|
||||
case SERVE, SPACE:
|
||||
Index.Start(Pulse, arg...)
|
||||
Pulse.Go(func() { Index.Start(Pulse, arg...) })
|
||||
conf.Wait()
|
||||
os.Exit(kit.Int(Pulse.Option(EXIT)))
|
||||
default:
|
||||
|
14
type.go
14
type.go
@ -182,17 +182,17 @@ func (c *Context) Merge(s *Context) *Context {
|
||||
}
|
||||
func (c *Context) Begin(m *Message, arg ...string) *Context {
|
||||
kit.If(c.Caches == nil, func() { c.Caches = Caches{} })
|
||||
c.Caches[CTX_FOLLOW] = &Cache{Name: CTX_FOLLOW, Value: c.Name}
|
||||
kit.If(c.context != nil && c.context != Index, func() { c.Cap(CTX_FOLLOW, kit.Keys(c.context.Cap(CTX_FOLLOW), c.Name)) })
|
||||
c.Caches[CTX_FOLLOW] = &Cache{Value: c.Name}
|
||||
kit.If(c.context != nil && c.context != Index, func() { c.Cap(CTX_FOLLOW, c.context.Prefix(c.Name)) })
|
||||
kit.If(c.server != nil, func() { c.server.Begin(m, arg...) })
|
||||
return c.Merge(c)
|
||||
}
|
||||
func (c *Context) Start(m *Message, arg ...string) {
|
||||
m.Log(CTX_START, c.Cap(CTX_FOLLOW))
|
||||
kit.If(c.server != nil, func() { m.Go(func() { c.server.Start(m, arg...) }, m.Prefix()) })
|
||||
m.Log(CTX_START, c.Prefix())
|
||||
kit.If(c.server != nil, func() { c.server.Start(m, arg...) })
|
||||
}
|
||||
func (c *Context) Close(m *Message, arg ...string) {
|
||||
m.Log(CTX_CLOSE, c.Cap(CTX_FOLLOW))
|
||||
m.Log(CTX_CLOSE, c.Prefix())
|
||||
kit.If(c.server != nil, func() { c.server.Close(m, arg...) })
|
||||
}
|
||||
|
||||
@ -271,7 +271,9 @@ func (m *Message) Spawn(arg ...Any) *Message {
|
||||
return msg
|
||||
}
|
||||
func (m *Message) Start(key string, arg ...string) *Message {
|
||||
return m.Search(key+PT, func(p *Context, s *Context) { s.Start(m.Spawn(s), arg...) })
|
||||
return m.Search(key+PT, func(p *Context, s *Context) {
|
||||
m.Cmd(ROUTINE, CREATE, kit.Select(m.Prefix(), key), func() { s.Start(m.Spawn(s), arg...) })
|
||||
})
|
||||
}
|
||||
func (m *Message) Travel(cb Any) *Message {
|
||||
target := m.target
|
||||
|
Loading…
x
Reference in New Issue
Block a user