1
0
forked from x/icebergs

opt icebergs

This commit is contained in:
bergyu 2021-10-25 16:41:11 +08:00
parent 0fb0de03fd
commit e29b3f7b48
13 changed files with 99 additions and 151 deletions

View File

@ -12,6 +12,7 @@ import (
ice "shylinux.com/x/icebergs"
"shylinux.com/x/icebergs/base/aaa"
"shylinux.com/x/icebergs/base/cli"
"shylinux.com/x/icebergs/base/ctx"
"shylinux.com/x/icebergs/base/mdb"
"shylinux.com/x/icebergs/base/nfs"
@ -121,7 +122,9 @@ func (f *Frame) parse(m *ice.Message, line string) string {
continue
}
msg.Cmdy(ls[0], ls[1:])
if msg.Cmdy(ls[0], ls[1:]); msg.Result(1) == ice.ErrNotFound {
msg.Set(ice.MSG_RESULT).Cmdy(cli.SYSTEM, ls)
}
_args, _ := msg.Optionv(ice.MSG_ARGS).([]interface{})
f.res = Render(msg, msg.Option(ice.MSG_OUTPUT), _args...)

View File

@ -62,7 +62,7 @@ func Render(msg *ice.Message, cmd string, args ...interface{}) {
msg.Echo(kit.Format(cmd, args...))
}
msg.W.Header().Set(ContentType, ContentJSON)
fmt.Fprint(msg.W, msg.Formats(kit.MDB_META))
fmt.Fprint(msg.W, msg.FormatMeta())
}
}
func RenderHeader(msg *ice.Message, key, value string) {

View File

@ -177,7 +177,7 @@ func _space_send(m *ice.Message, space string, arg ...string) {
m.Call(m.Option("_async") == "", func(res *ice.Message) *ice.Message {
// 返回结果
if delete(frame.send, id); res != nil && m != nil {
return m.Cost(kit.Format("[%v]->%v %v %v", id, target, arg, m.Copy(res).Format(ice.MSG_APPEND)))
return m.Cost(kit.Format("[%v]->%v %v %v", id, target, arg, m.Copy(res).FormatSize()))
}
return nil
})

View File

@ -207,7 +207,7 @@ func init() {
break
}
}
m.Log(ice.LOG_EXPORT, "%s %s", m.Option("begin"), m.Format("append"))
m.Log(ice.LOG_EXPORT, "%s %s", m.Option("begin"), m.FormatSize())
case PUSH:
if m.Richs(CACHE, nil, m.Option("data"), nil) == nil {

12
conf.go
View File

@ -36,12 +36,6 @@ const (
CMD = "cmd"
ARG = "arg"
OPT = "opt"
SRC = "src"
ETC = "etc"
BIN = "bin"
VAR = "var"
USR = "usr"
)
const ( // MOD
MOD_DIR = 0750
@ -67,6 +61,12 @@ const ( // REPOS
PUBLISH = "publish"
)
const ( // DIR
SRC = "src"
ETC = "etc"
BIN = "bin"
VAR = "var"
USR = "usr"
USR_VOLCANOS = "usr/volcanos"
USR_LEARNING = "usr/learning"
USR_ICEBERGS = "usr/icebergs"

19
data.go
View File

@ -32,20 +32,10 @@ func (m *Message) Save(arg ...string) *Message {
arg = append(arg, k)
}
}
list := []string{}
for _, k := range arg {
list = append(list, m.Prefix(k))
}
m.Cmd("config", SAVE, m.Prefix("json"), list)
return m
return m.Cmd("config", SAVE, m.Prefix("json"), arg)
}
func (m *Message) Load(arg ...string) *Message {
list := []string{}
for _, k := range arg {
list = append(list, m.Prefix(k))
}
m.Cmd("config", LOAD, m.Prefix("json"), list)
return m
return m.Cmd("config", LOAD, m.Prefix("json"), arg)
}
func (m *Message) Richs(prefix string, chain interface{}, raw interface{}, cb interface{}) (res map[string]interface{}) {
@ -56,13 +46,10 @@ func (m *Message) Richs(prefix string, chain interface{}, raw interface{}, cb in
switch cb := cb.(type) {
case func(*sync.Mutex, string, map[string]interface{}):
mu := &sync.Mutex{}
wg := &sync.WaitGroup{}
wg, mu := &sync.WaitGroup{}, &sync.Mutex{}
defer wg.Wait()
res = miss.Richs(kit.Keys(prefix, chain), cache, raw, func(key string, value map[string]interface{}) {
wg.Add(1)
m.Go(func() {
defer wg.Done()
cb(mu, key, value)

50
exec.go
View File

@ -22,15 +22,15 @@ func (m *Message) TryCatch(msg *Message, silent bool, hand ...func(msg *Message)
m.Log(LOG_WARN, "catch: %s %s", e, fileline)
m.Log(kit.MDB_STACK, msg.FormatStack())
m.Log(LOG_WARN, "catch: %s %s", e, fileline)
if len(hand) > 1 { // 捕获异常
if len(hand) > 1 {
m.TryCatch(msg, silent, hand[1:]...)
} else if !silent { // 抛出异常
m.Assert(e)
} else if !silent {
m.Assert(e) // 抛出异常
}
}
}()
if len(hand) > 0 { // 运行函数
if len(hand) > 0 {
hand[0](msg)
}
return m
@ -54,44 +54,44 @@ func (m *Message) Sleep(d string) *Message {
return m
}
func (m *Message) Hold(n int) *Message {
ctx := m.target.root
if c := m.target; c.context != nil && c.context.wg != nil {
ctx = c.context
for ctx := m.target; ctx != nil; ctx = ctx.context {
if ctx.wg != nil {
ctx.wg.Add(n)
break
}
}
ctx.wg.Add(n)
return m
}
func (m *Message) Done(b bool) bool {
if !b {
func (m *Message) Done(ok bool) bool {
if !ok {
return false
}
defer func() { recover() }()
ctx := m.target.root
if c := m.target; c.context != nil && c.context.wg != nil {
ctx = c.context
for ctx := m.target; ctx != nil; ctx = ctx.context {
if ctx.wg != nil {
ctx.wg.Done()
break
}
}
ctx.wg.Done()
return true
return ok
}
func (m *Message) Call(sync bool, cb func(*Message) *Message) *Message {
wait := make(chan bool, 2)
p := kit.Select("10s", m.Option(kit.MDB_TIMEOUT))
t := time.AfterFunc(kit.Duration(p), func() {
m.Warn(true, "%s timeout %v", p, m.Detailv())
m.Warn(true, "timeout", p, "of", m.Detailv())
m.Back(nil)
wait <- false
})
m.cb = func(sub *Message) *Message {
if sync {
m.cb = func(res *Message) *Message {
if res = cb(res); sync {
wait <- true
t.Stop()
}
return cb(sub)
return res
}
if sync {
@ -128,7 +128,7 @@ func (m *Message) Watch(key string, arg ...string) *Message {
if len(arg) == 0 {
arg = append(arg, m.Prefix(AUTO))
}
m.Cmd("event", "action", "listen", "event", key, CMD, strings.Join(arg, SP))
m.Cmd("event", "action", "listen", "event", key, CMD, kit.Join(arg, SP))
return m
}
func (m *Message) Event(key string, arg ...string) *Message {
@ -137,8 +137,8 @@ func (m *Message) Event(key string, arg ...string) *Message {
}
func (m *Message) Right(arg ...interface{}) bool {
return m.Option(MSG_USERROLE) == "root" || !m.Warn(m.Cmdx("role", "right",
m.Option(MSG_USERROLE), strings.ReplaceAll(kit.Keys(arg...), "/", PT)) != OK,
ErrNotRight, m.Option(MSG_USERROLE), OF, strings.Join(kit.Simple(arg), PT), " at ", kit.FileLine(2, 3))
m.Option(MSG_USERROLE), strings.ReplaceAll(kit.Keys(arg...), PS, PT)) != OK,
ErrNotRight, m.Option(MSG_USERROLE), OF, kit.Join(kit.Simple(arg), PT), " at ", kit.FileLine(2, 3))
}
func (m *Message) Space(arg interface{}) []string {
if arg == nil || arg == "" || kit.Format(arg) == m.Conf("runtime", "node.name") {
@ -152,7 +152,7 @@ func (m *Message) PodCmd(arg ...interface{}) bool {
msg := m.Cmd("cache", "upload")
m.Option(MSG_UPLOAD, msg.Append(kit.MDB_HASH), msg.Append(kit.MDB_NAME), msg.Append(kit.MDB_SIZE))
}
m.Cmdy(append([]interface{}{"space", pod}, arg...))
m.Cmdy(append(kit.List("space", pod), arg...))
return true
}
return false

31
init.go
View File

@ -16,7 +16,6 @@ func (f *Frame) Spawn(m *Message, c *Context, arg ...string) Server {
return &Frame{}
}
func (f *Frame) Begin(m *Message, arg ...string) Server {
m.Log(LOG_BEGIN, ICE)
defer m.Cost(LOG_BEGIN, ICE)
list := map[*Context]*Message{m.target: m}
@ -30,25 +29,18 @@ func (f *Frame) Begin(m *Message, arg ...string) Server {
return f
}
func (f *Frame) Start(m *Message, arg ...string) bool {
m.Log(LOG_START, ICE)
defer m.Cost(LOG_START, ICE)
m.Cap(CTX_STATUS, CTX_START)
m.Cap(CTX_STREAM, strings.Split(m.Time(), SP)[1])
m.Cmdy(INIT, arg)
m.target.root.wg = &sync.WaitGroup{}
for _, k := range kit.Split("log,gdb,ssh") {
m.Start(k)
}
defer m.TryCatch(m, true, func(msg *Message) { m.target.root.wg.Wait() })
m.Cmdy(arg)
return true
}
func (f *Frame) Close(m *Message, arg ...string) bool {
m.Log(LOG_CLOSE, ICE)
defer m.Cost(LOG_CLOSE, ICE)
list := map[*Context]*Message{m.target: m}
@ -61,9 +53,7 @@ func (f *Frame) Close(m *Message, arg ...string) bool {
return true
}
var Index = &Context{Name: "ice", Help: "冰山模块", Caches: map[string]*Cache{
CTX_FOLLOW: {Value: ICE}, CTX_STATUS: {Value: CTX_BEGIN}, CTX_STREAM: {Value: SHY},
}, Configs: map[string]*Config{
var Index = &Context{Name: "ice", Help: "冰山模块", Configs: map[string]*Config{
HELP: {Value: kit.Data("index", Info.Help)},
}, Commands: map[string]*Command{
CTX_INIT: {Hand: func(m *Message, c *Context, cmd string, arg ...string) {
@ -82,8 +72,9 @@ var Index = &Context{Name: "ice", Help: "冰山模块", Caches: map[string]*Cach
m.Echo(m.Config("index"))
}},
EXIT: {Name: "exit", Help: "结束", Hand: func(m *Message, c *Context, cmd string, arg ...string) {
defer c.server.(*Frame).Close(m.root.Spawn(), arg...)
m.root.Option(EXIT, kit.Select("0", arg, 0))
defer c.Close(m.root.Spawn(), arg...)
m.Cmd("source", ETC_EXIT_SHY)
m.root.Cmd(CTX_EXIT)
}},
@ -97,7 +88,7 @@ var Index = &Context{Name: "ice", Help: "冰山模块", Caches: map[string]*Cach
}
})
}},
}}
}, server: &Frame{}, wg: &sync.WaitGroup{}}
var Pulse = &Message{
time: time.Now(), code: 0,
meta: map[string][]string{},
@ -114,18 +105,14 @@ func Run(arg ...string) string {
arg = append(arg, HELP)
}
frame := &Frame{}
Index.Merge(Index)
Index.server = frame
Index.root = Index
Pulse.root = Pulse
Index.root, Pulse.root = Index, Pulse
switch kit.Select("", arg, 0) {
case "space", "serve":
if log.LogDisable = false; frame.Begin(Pulse.Spawn(), arg...).Start(Pulse, arg...) {
switch Index.Merge(Index).Begin(Pulse.Spawn(), arg...); kit.Select("", arg, 0) {
case "serve", "space":
if log.LogDisable = false; Index.Start(Pulse, arg...) {
Pulse.TryCatch(Pulse, true, func(Pulse *Message) { Index.wg.Wait() })
os.Exit(kit.Int(Pulse.Option(EXIT)))
}
default:
if Pulse.Cmdy(arg); Pulse.Result() == "" {
Pulse.Table()

59
logs.go
View File

@ -1,7 +1,6 @@
package ice
import (
"encoding/json"
"runtime"
"strings"
"time"
@ -21,11 +20,11 @@ func (m *Message) log(level string, str string, arg ...interface{}) *Message {
// 日志颜色
prefix, suffix := "", ""
switch level {
case LOG_IMPORT, LOG_CREATE, LOG_INSERT, LOG_MODIFY, LOG_EXPORT:
case LOG_CREATE, LOG_INSERT, LOG_MODIFY, LOG_EXPORT, LOG_IMPORT:
prefix, suffix = "\033[36;44m", "\033[0m"
case LOG_CMDS, LOG_START, LOG_SERVE:
prefix, suffix = "\033[32m", "\033[0m"
case LOG_WARN, LOG_CLOSE, LOG_ERROR:
case LOG_WARN, LOG_ERROR, LOG_CLOSE:
prefix, suffix = "\033[31m", "\033[0m"
case LOG_AUTH, LOG_COST:
prefix, suffix = "\033[33m", "\033[0m"
@ -33,10 +32,10 @@ func (m *Message) log(level string, str string, arg ...interface{}) *Message {
// 文件行号
switch level {
case LOG_CMDS, LOG_INFO, "refer", "form":
case LOG_INFO, LOG_CMDS, "refer", "form":
case LOG_BEGIN:
default:
suffix += " " + kit.FileLine(3, 3)
suffix += SP + kit.FileLine(3, 3)
}
// 长度截断
@ -173,60 +172,22 @@ func (m *Message) FormatChain() string {
meta := append([]string{}, NL)
for i := len(ms) - 1; i >= 0; i-- {
msg := ms[i]
meta = append(meta, kit.Format("%s %s:%d %v %s:%d %v %s:%d %v %s:%d %v", msg.FormatPrefix(),
MSG_DETAIL, len(msg.meta[MSG_DETAIL]), msg.meta[MSG_DETAIL],
MSG_OPTION, len(msg.meta[MSG_OPTION]), msg.meta[MSG_OPTION],
MSG_APPEND, len(msg.meta[MSG_APPEND]), msg.meta[MSG_APPEND],
MSG_RESULT, len(msg.meta[MSG_RESULT]), msg.meta[MSG_RESULT],
))
if len(msg.meta[MSG_OPTION]) > 0 {
for _, k := range msg.meta[MSG_OPTION] {
if v, ok := msg.meta[k]; ok {
meta = append(meta, kit.Format("\t%s: %d %v", k, len(v), v))
}
for _, k := range msg.meta[MSG_OPTION] {
if v, ok := msg.meta[k]; ok {
meta = append(meta, kit.Format("\t%s: %d %v", k, len(v), v))
}
}
if len(msg.meta[MSG_APPEND]) > 0 {
for _, k := range msg.meta[MSG_APPEND] {
if v, ok := msg.meta[k]; ok {
meta = append(meta, kit.Format("\t%s: %d %v", k, len(v), v))
}
for _, k := range msg.meta[MSG_APPEND] {
if v, ok := msg.meta[k]; ok {
meta = append(meta, kit.Format("\t%s: %d %v", k, len(v), v))
}
}
}
return kit.Join(meta, NL)
}
func (m *Message) Format(key interface{}) string {
switch key := key.(type) {
case []byte:
json.Unmarshal(key, &m.meta)
case string:
switch key {
case kit.MDB_PREFIX:
return m.FormatPrefix()
case kit.MDB_SHIP:
return m.FormatShip()
case kit.MDB_COST:
return m.FormatCost()
case kit.MDB_SIZE:
return m.FormatSize()
case kit.MDB_META:
return m.FormatMeta()
case kit.MDB_CHAIN:
return m.FormatChain()
case kit.MDB_STACK:
return m.FormatStack()
}
}
return m.FormatTime()
}
func (m *Message) Formats(key string) string {
switch key {
case kit.MDB_META:
return kit.Formats(m.meta)
}
return m.Format(key)
}

14
meta.go
View File

@ -143,7 +143,6 @@ func (m *Message) Push(key string, value interface{}, arg ...interface{}) *Messa
m.Add(MSG_APPEND, key, v)
}
}
return m
}
func (m *Message) Echo(str string, arg ...interface{}) *Message {
@ -154,7 +153,7 @@ func (m *Message) Copy(msg *Message, arg ...string) *Message {
if m == nil || m == msg {
return m
}
if len(arg) > 0 { // 精确复制
if len(arg) > 0 {
for _, k := range arg[1:] {
m.Add(arg[0], kit.Simple(k, msg.meta[k])...)
}
@ -346,13 +345,12 @@ func (m *Message) Optionv(key string, arg ...interface{}) interface{} {
m.meta[MSG_OPTION] = append(m.meta[MSG_OPTION], key)
}
switch str := arg[0].(type) {
switch delete(m.data, key); str := arg[0].(type) {
case nil:
delete(m.meta, key)
case string:
m.meta[key] = kit.Simple(arg...)
case []string:
delete(m.data, key)
m.meta[key] = str
default:
m.data[key] = str
@ -386,9 +384,17 @@ func (m *Message) Appendv(key string, arg ...interface{}) []string {
if m.FieldsIsDetail() {
for i, k := range m.meta[kit.MDB_KEY] {
if k == key {
if len(arg) > 0 {
m.meta[kit.MDB_VALUE][i] = kit.Format(arg[0])
}
return []string{kit.Select("", m.meta[kit.MDB_VALUE], i)}
}
}
if len(arg) > 0 {
m.Add(MSG_APPEND, kit.MDB_KEY, key)
m.Add(MSG_APPEND, kit.MDB_VALUE, kit.Format(arg[0]))
return []string{kit.Format(arg[0])}
}
return nil
}

18
misc.go
View File

@ -185,10 +185,7 @@ func (m *Message) cmd(arg ...interface{}) *Message {
})
}
// 系统命令
if m.Warn(!ok, ErrNotFound, list) {
return m.Set(MSG_RESULT).Cmdy("system", list)
}
m.Warn(!ok, ErrNotFound, list)
return m
}
func (c *Context) cmd(m *Message, cmd *Command, key string, arg ...string) *Message {
@ -273,7 +270,7 @@ func (c *Context) split(name string) (list []interface{}) {
case "text":
list = append(list, kit.List(kit.MDB_TYPE, TEXTAREA, kit.MDB_NAME, "text")...)
case "auto":
list = append(list, kit.List(kit.MDB_TYPE, BUTTON, kit.MDB_NAME, "list", kit.MDB_VALUE, AUTO)...)
list = append(list, kit.List(kit.MDB_TYPE, BUTTON, kit.MDB_NAME, "list", kit.MDB_ACTION, AUTO)...)
list = append(list, kit.List(kit.MDB_TYPE, BUTTON, kit.MDB_NAME, "back")...)
button = true
case "page":
@ -314,12 +311,13 @@ func (c *Context) split(name string) (list []interface{}) {
}
func Display(file string, arg ...string) map[string]string {
if file != "" && !strings.HasPrefix(file, "/") {
ls := strings.Split(kit.FileLine(2, 100), "usr")
file = kit.Select(file+".js", file, strings.HasSuffix(file, ".js"))
file = path.Join("/require/shylinux.com/x", path.Dir(ls[len(ls)-1]), file)
if file == "" {
file = kit.FileName(2) + ".js"
}
return map[string]string{"display": file, kit.MDB_STYLE: kit.Select("", arg, 0)}
if !strings.HasPrefix(file, "/") {
file = path.Join("/require", kit.ModPath(2, file))
}
return map[string]string{"display": file, kit.MDB_STYLE: kit.Join(arg, " ")}
}
func MergeAction(list ...map[string]*Action) map[string]*Action {
if len(list) == 0 {

View File

@ -80,11 +80,6 @@ func (m *Message) IsCliUA() bool {
}
return false
}
func (m *Message) PushDownload(key string, arg ...interface{}) { // [name] file
if !m.IsCliUA() {
m.Push(key, Render(m, RENDER_DOWNLOAD, arg...))
}
}
func (m *Message) PushAnchor(arg ...interface{}) { // [name] link
if !m.IsCliUA() {
m.Push(kit.MDB_LINK, Render(m, RENDER_ANCHOR, arg...))
@ -115,6 +110,11 @@ func (m *Message) PushVideos(key, src string, arg ...string) { // key src [size]
m.Push(key, Render(m, RENDER_VIDEOS, src, arg))
}
}
func (m *Message) PushDownload(key string, arg ...interface{}) { // [name] file
if !m.IsCliUA() {
m.Push(key, Render(m, RENDER_DOWNLOAD, arg...))
}
}
func (m *Message) PushAction(list ...interface{}) {
m.Table(func(index int, value map[string]string, head []string) {
@ -159,9 +159,6 @@ func (m *Message) PushSearch(args ...interface{}) {
}
}
func (m *Message) EchoDownload(arg ...interface{}) *Message { // [name] file
return m.Echo(Render(m, RENDER_DOWNLOAD, arg...))
}
func (m *Message) EchoAnchor(arg ...interface{}) *Message { // [name] link
return m.Echo(Render(m, RENDER_ANCHOR, arg...))
}
@ -180,3 +177,6 @@ func (m *Message) EchoImages(src string, arg ...string) *Message { // src [size]
func (m *Message) EchoVideos(src string, arg ...string) *Message { // src [size]
return m.Echo(Render(m, RENDER_VIDEOS, src, arg))
}
func (m *Message) EchoDownload(arg ...interface{}) *Message { // [name] file
return m.Echo(Render(m, RENDER_DOWNLOAD, arg...))
}

20
type.go
View File

@ -151,7 +151,6 @@ func (c *Context) Merge(s *Context) *Context {
for k, v := range s.Configs {
c.Configs[k] = v
}
if c.Caches == nil {
c.Caches = map[string]*Cache{}
}
@ -162,7 +161,7 @@ func (c *Context) Merge(s *Context) *Context {
}
func (c *Context) Spawn(m *Message, name string, help string, arg ...string) *Context {
s := &Context{Name: name, Help: help, Caches: map[string]*Cache{}, Configs: map[string]*Config{}}
s := &Context{Name: name, Help: help}
if m.target.server != nil {
c.Register(s, m.target.server.Spawn(m, s, arg...))
} else {
@ -172,7 +171,11 @@ func (c *Context) Spawn(m *Message, name string, help string, arg ...string) *Co
return s
}
func (c *Context) Begin(m *Message, arg ...string) *Context {
c.Caches[CTX_FOLLOW] = &Cache{Name: CTX_FOLLOW, Value: kit.Keys(kit.Select("", c.context.Cap(CTX_FOLLOW), c.context != Index), c.Name)}
follow := c.Name
if c.context != nil && c.context != Index {
follow = kit.Keys(c.context.Cap(CTX_FOLLOW), c.Name)
}
c.Caches[CTX_FOLLOW] = &Cache{Name: CTX_FOLLOW, Value: follow}
c.Caches[CTX_STATUS] = &Cache{Name: CTX_STATUS, Value: CTX_BEGIN}
c.Caches[CTX_STREAM] = &Cache{Name: CTX_STREAM, Value: ""}
m.Log(LOG_BEGIN, c.Cap(CTX_FOLLOW))
@ -183,10 +186,14 @@ func (c *Context) Begin(m *Message, arg ...string) *Context {
return c
}
func (c *Context) Start(m *Message, arg ...string) bool {
wait := make(chan bool)
defer func() { <-wait }()
m.Hold(1)
m.Go(func() {
defer m.Done(true)
wait <- true
c.Cap(CTX_STATUS, CTX_START)
m.Log(LOG_START, c.Cap(CTX_FOLLOW))
@ -335,6 +342,7 @@ func (m *Message) Search(key string, cb interface{}) *Message {
// 查找模块
p := m.target.root
key = strings.TrimPrefix(key, "ice.")
if ctx, ok := Info.names[key].(*Context); ok {
p = ctx
} else if key == "ice." {
@ -479,12 +487,10 @@ func (m *Message) Capv(arg ...interface{}) interface{} {
for _, s := range []*Context{m.target} {
for c := s; c != nil; c = c.context {
if caps, ok := c.Caches[key]; ok {
if len(arg) > 0 {
// 写数据
if len(arg) > 0 { // 写数据
caps.Value = kit.Format(arg[0])
}
// 读数据
return caps.Value
return caps.Value // 读数据
}
}
}