mirror of
https://shylinux.com/x/community
synced 2025-05-08 06:46:55 +08:00
82 lines
2.8 KiB
Go
82 lines
2.8 KiB
Go
package gonganxitong
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
|
|
"shylinux.com/x/ice"
|
|
"shylinux.com/x/icebergs/base/nfs"
|
|
kit "shylinux.com/x/toolkits"
|
|
|
|
"shylinux.com/x/community/src/gonganxitong/model"
|
|
)
|
|
|
|
type command struct {
|
|
Table
|
|
service service
|
|
modify string `name:"modify service_uid index name icon"`
|
|
}
|
|
|
|
func CommandUID(m *ice.Message, cmd string) string {
|
|
return kit.Hashs(m.Option(model.SERVICE_UID), cmd)
|
|
}
|
|
func (s command) Modify(m *ice.Message, arg ...string) {
|
|
uid := CommandUID(m, m.Option(model.INDEX))
|
|
if s.Select(m.Spawn(), model.UID, uid).Length() == 0 {
|
|
s.Insert(m, kit.Simple(arg, model.UID, uid)...)
|
|
} else {
|
|
s.Update(m, kit.Dict(arg), model.UID, uid)
|
|
}
|
|
m.SetResult().Echo(uid)
|
|
}
|
|
func (s command) Autogen(m *ice.Message, arg ...string) {
|
|
space, portal := map[string][]string{}, map[string][]string{}
|
|
s.List(m).Table(func(value ice.Maps) {
|
|
if strings.HasSuffix(value[model.INDEX], ".allow") {
|
|
space[value[model.SPACE]] = append(space[value[model.SPACE]], strings.ReplaceAll(value[model.INDEX], ".allow", ".portal"))
|
|
}
|
|
space[value[model.SPACE]] = append(space[value[model.SPACE]], value[model.INDEX])
|
|
portal[value[model.PORTAL]] = append(portal[value[model.PORTAL]], value[model.INDEX])
|
|
})
|
|
for p, v := range space {
|
|
nfs.Create(m.Message, "src/api/"+p+".go", func(w io.Writer) {
|
|
fmt.Fprintf(w, "package api\n\n")
|
|
for _, v := range v {
|
|
fmt.Fprintf(w, "const %s = \"%s\"\n", strings.ToUpper(strings.ReplaceAll(strings.TrimPrefix(v, "web.team."), ".", "_")), v)
|
|
}
|
|
})
|
|
}
|
|
nfs.Create(m.Message, "src/api/space.go", func(w io.Writer) {
|
|
fmt.Fprintf(w, "package api\n\n")
|
|
fmt.Fprintf(w, "var Space = map[string]string{\n")
|
|
defer fmt.Fprintf(w, "}\n")
|
|
for space, cmd := range space {
|
|
kit.For(cmd, func(cmd string) { fmt.Fprintf(w, "\"%s\": \"%s\",\n", cmd, space) })
|
|
}
|
|
})
|
|
nfs.Create(m.Message, "src/api/portal.go", func(w io.Writer) {
|
|
fmt.Fprintf(w, "package api\n\n")
|
|
fmt.Fprintf(w, "var Portal = map[string]string{\n")
|
|
defer fmt.Fprintf(w, "}\n")
|
|
for portal, cmd := range portal {
|
|
kit.For(cmd, func(cmd string) { fmt.Fprintf(w, "\"%s\": \"%s\",\n", cmd, portal) })
|
|
}
|
|
})
|
|
}
|
|
func (s command) Cache(m *ice.Message, arg ...string) {
|
|
kit.For(cmdPortal, func(k, v string) { m.Push(model.SPACE, cmdSpace[k]).Push(model.PORTAL, v).Push(model.INDEX, k) })
|
|
m.Sort("space,portal,index")
|
|
}
|
|
func (s command) List(m *ice.Message, arg ...string) *ice.Message {
|
|
s.Tables(m, s.service).Fields(m, s.Key(s, model.UPDATED_AT),
|
|
s.Key(s, model.INDEX), s.AS(s.Key(s.service, "`"+model.INDEX+"`"), model.PORTAL), s.AS(model.NODENAME, model.SPACE),
|
|
).Orders(m, "`space`,`commands`.`index`").Limit(m, 300).Select(m).Action(s.Cache, s.Autogen)
|
|
return m
|
|
}
|
|
|
|
func init() { ice.TeamCtxCmd(command{}) }
|
|
|
|
var cmdPortal = map[string]string{}
|
|
var cmdSpace = map[string]string{}
|