func (c *Ctx) EX(args ...string) (string, error) { nargs := []string{} exec_res := map[string]string{} for _, a := range args { if strings.HasPrefix(a, "#") { kv := strings.SplitN(a, "=", 2) if len(kv) < 2 { continue } exec_res[strings.Trim(kv[0], "#")] = kv[1] } else { nargs = append(nargs, c.Compile(a)) } } bys, err := exec.Command(C_SH, "-c", strings.Join(nargs, " ")).Output() data := string(bys) c.log("EX response data:%v", data) if k, ok := exec_res["data"]; ok { js, err := util.Json2Map(data) if err == nil { c.SET(k, js) } else { c.SET(k, strings.Trim(data, "\t \n")) } } if k, ok := exec_res["err"]; ok && err != nil { c.SET(k, err.Error()) } return data, err }
func (c *Ctx) R(args ...string) (interface{}, error) { if len(args) < 1 { return nil, Err("write file error:%v", "file path not set") } fpath := c.Compile(args[0]) f, err := os.Open(fpath) if err != nil { return nil, err } defer f.Close() bys, err := ioutil.ReadAll(f) if err != nil { return nil, err } var val interface{} = nil val, err = util.Json2Map(string(bys)) if err != nil { val = string(bys) } if len(args) > 1 && strings.HasPrefix(args[1], "#") { kv := strings.SplitN(args[1], "=", 2) if len(kv) == 2 && kv[0] == "#data" { c.log("setting data to %v", kv[1]) c.SET(kv[1], val) } } c.log("read %d data from file:%v", len(bys), fpath) return val, nil }
func (c *Ctx) SET(path string, val interface{}) error { if sval, ok := val.(string); ok { sval = c.Compile(sval) js, err := util.Json2Map(sval) if err == nil { return c.Kvs.SetValP(c.Compile(path), js) } // c.log("convert to json object err:%v", err.Error()) ary, err := util.Json2Ary(sval) if err == nil { return c.Kvs.SetValP(c.Compile(path), ary) } // c.log("convert to json object err:%v", err.Error()) spath := c.Compile(path) c.log("SET %v %v", spath, sval) return c.Kvs.SetValP(spath, sval) } else { spath := c.Compile(path) c.log("SET %v %v", spath, val) return c.Kvs.SetValP(spath, val) } }
func (c *Ctx) HR(args ...string) (int, string, error) { if len(args) < 2 { return 0, "", errors.New(fmt.Sprintf("Usage:method url args")) } var turl = c.Compile(args[1]) var method = c.Compile(args[0]) fkey, fp := "", "" fields := map[string]string{} header := map[string]string{} exec_res := map[string]string{} for _, arg := range args[2:] { kv := strings.SplitN(arg, "=", 2) if len(kv) < 2 { continue } if strings.HasPrefix(kv[0], "^") { header[strings.TrimLeft(kv[0], "^")] = c.Compile(kv[1]) } else if strings.HasPrefix(kv[0], "%") { fkey, fp = strings.TrimLeft(kv[0], "%"), c.Compile(kv[1]) } else if strings.HasPrefix(kv[0], "#") { exec_res[strings.TrimLeft(kv[0], "#")] = c.Compile(kv[1]) } else { fields[kv[0]] = c.Compile(kv[1]) } } var code int var data string var err error if method == "POST" { c.log("POST(%v) fileds(%v) header(%v) fkey(%v) fpath(%v)", turl, fields, header, fkey, fp) code, data, err = c.H.HPostF_H(turl, fields, header, fkey, fp) c.log("POST response data:%v", data) } else if method == "GET" { vs := url.Values{} for k, v := range fields { vs.Add(k, v) } turl = fmt.Sprintf("%v?%v", turl, vs.Encode()) c.log("GET(%v) header(%v)", turl, header) code, data, err = c.H.HGet_H(header, "%v", turl) c.log("GET response data:%v", data) } else { return 0, "", errors.New(fmt.Sprintf("unknow method(%v)", args[0])) } if k, ok := exec_res["code"]; ok { k = c.Compile(k) c.SET(k, code) } if k, ok := exec_res["data"]; ok { k = c.Compile(k) js, err := util.Json2Map(data) if err == nil { c.SET(k, js) } else { c.SET(k, data) } } if k, ok := exec_res["err"]; ok && err != nil { c.SET(k, err.Error()) } return code, data, err }