Example #1
0
func newExecHandler(ctx, params map[string]interface{}) (Handler, error) {
	if nil == ctx {
		return nil, errors.New("ctx is nil")
	}
	if nil == params {
		return nil, errors.New("params is nil")
	}

	work_directory := stringWithDefault(params, "work_directory", *default_directory)
	prompt := stringWithDefault(params, "prompt", "")
	command := stringWithDefault(params, "command", "")
	environments := stringsWithDefault(params, "environments", ";", nil)
	if 0 == len(command) {
		return nil, errors.New("'command' is required.")
	}

	if args, ok := params["arguments"]; ok {
		args = preprocessArgs(args)

		if props, ok := args.(map[string]interface{}); ok {
			if _, ok := props["self"]; !ok {
				props["self"] = params
				defer delete(props, "self")
			}
		}

		var e error
		command, e = genText(command, args)
		if nil != e {
			return nil, e
		}
		prompt, e = genText(prompt, args)
		if nil != e {
			return nil, e
		}
		for idx, s := range environments {
			s, e = genText(s, args)
			if nil != e {
				return nil, e
			}
			environments[idx] = s
		}
	}

	arguments, e := shellwords.Split(command)
	if nil != e {
		return nil, errors.New("split shell command failed, " + e.Error())
	}

	return &execHandler{work_directory: work_directory,
		prompt:       prompt,
		command:      arguments[0],
		arguments:    arguments[1:],
		environments: environments}, nil
}
Example #2
0
func toStrings(v interface{}) ([]string, error) {
	switch s := v.(type) {
	case string:
		cmd, e := shellwords.Split(s)
		if nil != e {
			return nil, e
		}
		return cmd, nil
	case []string:
		if 0 == len(s) {
			return nil, errors.New("command is empty array")
		}
		return s, nil
	case []interface{}:
		if 0 == len(s) {
			return nil, errors.New("command is empty array")
		}
		cmd := make([]string, 0, len(s))
		for _, h := range s {
			if nil == h {
				break
			}
			cmd = append(cmd, fmt.Sprint(h))
		}
		return cmd, nil
	case map[string]interface{}:
		cmd := stringWithDefault(s, "command", "")
		if 0 == len(s) {
			return nil, errors.New("command is required")
		}

		return newRedisArguments(cmd,
			stringWithDefault(s, "arg0", ""),
			stringWithDefault(s, "arg1", ""),
			stringWithDefault(s, "arg2", ""),
			stringWithDefault(s, "arg3", ""),
			stringWithDefault(s, "arg4", ""),
			stringWithDefault(s, "arg5", ""),
			stringWithDefault(s, "arg6", "")), nil

	default:
		return nil, fmt.Errorf("command is unsupported type - %T", v)
	}
}
Example #3
0
func ExecShell2(ws *websocket.Conn) {
	defer ws.Close()

	query_params := ws.Request().URL.Query()
	wd := query_params.Get("wd")
	charset := query_params.Get("charset")
	pa := query_params.Get("exec")
	timeout := query_params.Get("timeout")

	ss, e := shellwords.Split(pa)
	if nil != e {
		io.WriteString(ws, "命令格式不正确:")
		io.WriteString(ws, e.Error())
		return
	}
	pa = ss[0]
	args := ss[1:]

	execShell(ws, pa, args, charset, wd, timeout)
}
Example #4
0
func newRedisHandler(ctx, params map[string]interface{}) (Handler, error) {
	if nil == ctx {
		return nil, errors.New("ctx is nil")
	}
	if nil == params {
		return nil, errors.New("params is nil")
	}

	o, ok := ctx["redis"]
	if !ok {
		return nil, errors.New("'redis' in the ctx is required")
	}
	client, ok := o.(*redis_gateway)
	if !ok {
		return nil, fmt.Errorf("'redis' in the ctx is not a *Redis - %T", o)
	}
	if nil == client {
		return nil, errors.New("'redis' in the ctx is nil")
	}

	args := params["arguments"]

	var array []string
	var e error
	o, ok = params["command"]
	if !ok {
		goto commands_label
	}

	array, e = toStrings(params)
	if nil != e {
		return nil, e
	}

	array, e = replacePlaceHolders(array, args)
	if nil != e {
		return nil, e
	}

	return &redisHandler{client: client, commands: [][]string{array}}, nil

commands_label:
	v, ok := params["commands"]
	if !ok {
		return nil, errors.New("'command' or 'commands' is required")
	}

	switch ss := v.(type) {
	case []string:
		if 0 == len(ss) {
			return nil, errors.New("commands is empty array")
		}
		commands := make([][]string, 0, len(ss))
		for _, s := range ss {
			cmd, e := shellwords.Split(s)
			if nil != e {
				return nil, e
			}

			cmd, e = replacePlaceHolders(cmd, args)
			if nil != e {
				return nil, e
			}

			commands = append(commands, cmd)
		}
		return &redisHandler{client: client, commands: commands}, nil
	case []interface{}:
		if 0 == len(ss) {
			return nil, errors.New("commands is empty array")
		}
		commands := make([][]string, 0, len(ss))
		for _, h := range ss {
			cmd, e := toStrings(h)
			if nil != e {
				return nil, e
			}

			cmd, e = replacePlaceHolders(cmd, args)
			if nil != e {
				return nil, e
			}

			commands = append(commands, cmd)
		}
		return &redisHandler{client: client, commands: commands}, nil
	default:
		return nil, fmt.Errorf("command is unsupported type - %T", v)
	}
}