Example #1
0
func (shell *Shell) Run() {
	shell.prompt = NewPrompt()
	shell.refreshClient()

	gl := goline.NewGoLine(shell.prompt)

	for {
		line, err := gl.Line()
		if err != nil {
			if err == goline.UserTerminatedError {
				return
			}
			util.LogError(err.Error())
		} else {
			fmt.Println()
			line = strings.TrimSpace(line)
			if len(line) > 0 {
				command, arguments := parseLine(line)

				method := reflect.ValueOf(shell).MethodByName(command)
				if method.IsValid() {
					method.Call([]reflect.Value{reflect.ValueOf(arguments)})
				} else {
					util.LogError(fmt.Sprint("Unknown command: '", command, "'"))
				}
			}
		}
	}
}
Example #2
0
func (shell *Shell) Ping(args string) {
	service := elastic.NewPingService(shell.client)
	result, _, err := service.Do()
	if err == nil {
		json, err := util.JsonString(result)
		if err == nil {
			util.LogInfo(json)
		} else {
			util.LogError(err.Error())
		}
	} else {
		util.LogError(err.Error())
	}
}
Example #3
0
func (shell *Shell) Nodes(args string) {
	service := elastic.NewNodesInfoService(shell.client)
	response, err := service.Do()
	if err == nil {
		json, err := util.JsonString(response)
		if err == nil {
			util.LogInfo(json)
		} else {
			util.LogError(err.Error())
		}
	} else {
		util.LogError(err.Error())
	}
}
Example #4
0
func (shell *Shell) refreshClient() {
	url := fmt.Sprint("http://", shell.prompt.Host, ":", strconv.Itoa(shell.prompt.Port), "/")
	util.LogInfo(fmt.Sprint("Connecting to ", url, "..."))
	client, err := elastic.NewClient(
		elastic.SetURL(url),
	)
	if err == nil {
		shell.client = client
	} else {
		util.LogError(err.Error())
	}
}
Example #5
0
func parseTerms(args string) map[string]string {
	terms := make(map[string]string)
	for _, pair := range strings.Split(args, "&") {
		if pair != "" {
			parts := strings.SplitN(pair, "=", 2)
			if len(parts) == 2 {
				terms[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
			} else {
				util.LogError(fmt.Sprint("Can't parse ", pair))
			}
		}
	}
	return terms
}