1
0
mirror of https://shylinux.com/x/icebergs synced 2025-05-03 20:07:01 +08:00
This commit is contained in:
IT 老营长 @云轩领航-创始人 2023-04-02 21:37:23 +08:00
parent 9e2bc71dff
commit 883ec3c0f2
8 changed files with 108 additions and 31 deletions

View File

@ -89,7 +89,8 @@ func _system_exec(m *ice.Message, cmd *exec.Cmd) {
cmd.Stdout, cmd.Stderr = out, err
defer func() {
m.Push(CMD_OUT, out.String()).Push(CMD_ERR, err.String())
m.Echo(strings.TrimRight(out.String(), ice.NL))
// m.Echo(strings.TrimRight(out.String(), ice.NL))
m.Echo(out.String())
if m.IsErr() {
m.Option(ice.MSG_ARGS, kit.Simple(http.StatusBadRequest, cmd.Args, err.String()))
m.Echo(strings.TrimRight(err.String(), ice.NL))

View File

@ -86,3 +86,23 @@ func init() {
func IsLocalHost(m *ice.Message, ip string) bool { return m.Cmdx(HOST, ISLOCAL, ip) == ice.OK }
func PublishLocalhost(m *ice.Message, url string) string { return m.Cmdx(HOST, PUBLISH, url) }
func init() {
ice.Info.Stack[Prefix()] = func(m *ice.Message, key string, arg ...ice.Any) ice.Any {
switch key {
case kit.FuncName(IsLocalHost):
for _, v := range arg {
switch v := v.(type) {
case *ice.Message:
m = v
case string:
return IsLocalHost(m, v)
}
}
return false
default:
m.ErrorNotImplement(key)
return nil
}
}
}

View File

@ -1,9 +1,14 @@
package tcp
import ice "shylinux.com/x/icebergs"
import (
ice "shylinux.com/x/icebergs"
kit "shylinux.com/x/toolkits"
)
const TCP = "tcp"
var Index = &ice.Context{Name: TCP, Help: "通信模块"}
func init() { ice.Index.Register(Index, nil, HOST, PORT, CLIENT, SERVER) }
func Prefix(arg ...string) string { return kit.Keys(TCP, kit.Keys(arg)) }

View File

@ -140,6 +140,7 @@ func (s *Expr) end(m *ice.Message) Any {
return s.getv(m, 0)
}
func (s *Expr) cals(m *ice.Message) Any {
line := s.s.line
if s.s.skip == -1 {
m.Debug("expr calcs %v %s:%d", s.s.rest, s.s.name, s.s.line)
} else {
@ -209,11 +210,36 @@ func (s *Expr) cals(m *ice.Message) Any {
s.push(Number{value: k})
}
if s.gets(-2) == "!" {
s.pops(2, s.opv(-1, "!"))
s.pops(2, s.opv(m, -1, "!", nil))
}
}
return false
})
if cmds := false; len(s.list) == 1 && s.s.skip < 2 {
m.Search(s.gets(0), func(key string, cmd *ice.Command) {
if cmds = true; s.s.line == line {
args := kit.List(s.gets(0))
for {
s := NewExpr(s.s)
s.cals(m)
if v := s.getv(m, 0); v != nil {
args = append(args, trans(v))
} else {
args = append(args, v)
}
if s.s.line != line {
break
}
}
m.Cmdy(args...)
} else {
m.Cmdy(s.gets(0))
}
})
if cmds {
return nil
}
}
return s.end(m)
}
func (s *Expr) call(m *ice.Message, obj Any, key string) Any {

View File

@ -86,7 +86,15 @@ func (s *Stack) value(m *ice.Message, key string, arg ...Any) Any {
m.Debug("value set %d %v %#v", n, key, arg[0])
f.value[key] = arg[0]
})
return f.value[key]
if v, ok := f.value[key]; ok {
return v
}
if k := kit.Select(key, strings.Split(key, ice.PT), -1); strings.ToUpper(k) == k {
if c, ok := ice.Info.Index[strings.ToLower(k)].(*ice.Context); ok && (key == k || key == c.Prefix(k)) {
return strings.ToLower(key)
}
}
return nil
}
func (s *Stack) runable() bool { return s.peekf().status > STATUS_DISABLE }
func (s *Stack) token() string { return kit.Select("", s.rest, s.skip) }
@ -140,7 +148,7 @@ func (s *Stack) run(m *ice.Message) {
m.Cmdy(k, kit.Slice(s.rest, s.skip+1))
} else {
s.skip--
m.Cmd(EXPR, kit.Slice(s.rest, s.skip))
m.Cmdy(EXPR, kit.Slice(s.rest, s.skip))
}
return false
})
@ -187,6 +195,9 @@ func (s *Stack) call(m *ice.Message, obj Any, key Any, cb func(*Frame, Function)
return value
case Caller:
return obj.Call(kit.Format(key), arg...)
case func(*ice.Message, string, ...Any) Any:
kit.For(arg, func(i int, v Any) { arg[i] = trans(arg[i]) })
return wrap(obj(m, kit.Format(key), arg...))
case func(string, ...Any) Any:
return obj(kit.Format(key), arg...)
case func():
@ -271,29 +282,34 @@ func init() {
})
}},
ice.RUN: {Hand: func(m *ice.Message, arg ...string) {
nfs.Open(m, existsFile(m, arg[0]), func(r io.Reader, p string) {
s := NewStack().parse(m.Spawn(), p, r, nil)
action := mdb.LIST
if len(arg) > 2 && arg[1] == ice.ACTION && s.value(m, arg[2]) != nil {
action, arg = arg[2], arg[3:]
} else {
arg = arg[1:]
}
i := 0
s.call(m, s, action, func(f *Frame, v Function) {
kit.For(v.arg, func(k string) {
switch k {
case "m":
f.value[k] = Message{m}
case ice.ARG:
list := kit.List()
kit.For(arg, func(v string) { list = append(list, String{v}) })
f.value[k] = Value{list}
default:
f.value[k] = String{m.Option(k, kit.Select(m.Option(k), arg, i))}
i++
}
})
s := mdb.Cache(m, arg[0], func() (stack Any) {
nfs.Open(m, existsFile(m, arg[0]), func(r io.Reader, p string) {
stack = NewStack().parse(m.Spawn(), p, r, nil)
})
return
}).(*Stack)
kit.If(m.Option("debug") == ice.TRUE, func() { mdb.Cache(m, arg[0], nil) })
m.StatusTime()
action := mdb.LIST
if len(arg) > 2 && arg[1] == ice.ACTION && s.value(m, arg[2]) != nil {
action, arg = arg[2], arg[3:]
} else {
arg = arg[1:]
}
i := 0
s.call(m, s, action, func(f *Frame, v Function) {
kit.For(v.arg, func(k string) {
switch k {
case "m":
f.value[k] = Message{m}
case ice.ARG:
list := kit.List()
kit.For(arg, func(v string) { list = append(list, String{v}) })
f.value[k] = Value{list}
default:
f.value[k] = String{m.Option(k, kit.Select(m.Option(k), arg, i))}
i++
}
})
})
}},

View File

@ -27,6 +27,8 @@ func (s Boolean) MarshalJSON() ([]byte, error) { return json.Marshal(s.value) }
func wrap(v Any) Any {
switch v := v.(type) {
case bool:
return Boolean{v}
case string:
return String{v}
default:
@ -180,7 +182,10 @@ func (m Message) Call(cmd string, arg ...Any) Any {
func (s *Stack) load(m *ice.Message) *Stack {
f := s.pushf(m.Options(STACK, s), "")
f.value["m"] = Message{m}
f.value["kit"] = func(key string, arg ...Any) Any {
for k, v := range ice.Info.Stack {
f.value[k] = v
}
f.value["kit"] = func(m *ice.Message, key string, arg ...Any) Any {
kit.For(arg, func(i int, v Any) { arg[i] = trans(v) })
switch key {
case "Dict":

View File

@ -39,6 +39,7 @@ var Info = struct {
Gomod Maps
Route Maps
Index Map
Stack map[string]func(m *Message, key string, arg ...Any) Any
merges []Any
render map[string]func(*Message, ...Any) string
@ -52,6 +53,7 @@ var Info = struct {
Gomod: Maps{},
Route: Maps{},
Index: Map{},
Stack: map[string]func(m *Message, key string, arg ...Any) Any{},
render: map[string]func(*Message, ...Any) string{},
Load: func(m *Message, key ...string) *Message { return m },

View File

@ -147,8 +147,10 @@ func (m *Message) IsErrNotFound() bool {
return m.IsErr(ErrNotFound)
}
func (m *Message) Debug(str string, arg ...Any) {
kit.Format(str == "", func() { str = m.FormatMeta() })
m.log(LOG_DEBUG, str, arg...)
if m.Option("debug") == TRUE {
kit.Format(str == "", func() { str = m.FormatMeta() })
m.log(LOG_DEBUG, str, arg...)
}
}
func (m *Message) FormatPrefix() string {