Example #1
0
func init() {
	h := backend.NewHandler()
	h.CommandName = "help"
	h.CommandPattern = "(help)( )(.*)"
	h.Usage = "help"
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		cmdList := ""
		keys := []string{}
		for k, _ := range backend.HandlerRegistry {
			keys = append(keys, k)
		}
		sort.Strings(keys)
		for _, s := range keys {
			//cmdList += s + ": " + backend.HandlerRegistry[s].Usage + "\n"
			cmdList += s + "\n"
		}
		return `
	Help
	----


Write commands require host and port be specified to avoid slip-ups.
Read commands can omit host and port, and session settings will be used.


Commands
--------
` + cmdList + `
`, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
Example #2
0
func init() {
	h := backend.NewHandler()
	h.CommandName = "run"
	h.CommandPattern = "(run)( )(.*)"
	h.Usage = "run"
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		verb := strings.ToLower(cmd.Tokens[0])
		cmdParser := backend.NewCommandParser()
		newCmd, err := cmdParser.Parse(backend.LoadedRequest.Request)
		if err != nil {
			return "Unable to parse loaded query for run command.", false
		}
		dispatcher := backend.NewDispatcher()
		switch verb {
		case "post":
			resp, ok := dispatcher.Dispatch(newCmd)
			return resp, ok
		case "put":
			resp, ok := dispatcher.Dispatch(newCmd)
			return resp, ok
		case "get":
			resp, ok := dispatcher.Dispatch(newCmd)
			return resp, ok
		}
		return "Unable to run loaded query.", false
	}
	backend.HandlerRegistry[h.CommandName] = h
}
Example #3
0
func init() {
	h := backend.NewHandler()
	h.CommandName = "get"
	h.CommandPattern = "(get)( )(.*)"
	h.Usage = "get url"
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		//queryHost := cmd.Tokens[1]
		//queryPort := cmd.Tokens[2]
		//queryArgs := cmd.Tokens[3]

		arg := cmd.Tokens[1]
		u := new(url.URL)
		newUrl, err := u.Parse(arg)
		if err != nil {
			return "Unable to parse url: " + err.Error(), false
		}
		fmt.Println("Request: get", newUrl)
		res, err := backend.GetHttpResource(newUrl.String())
		if err != nil {
			return err.Error(), false
		}
		return res, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
Example #4
0
// TODO: clean up.
func init() {
	h := backend.NewHandler()
	h.CommandName = "doc"
	h.CommandPattern = "(doc)(( )(.*))"
	h.Usage = "doc (get index docId) | (delete index type docId)"
	h.CommandParser = func(cmd *backend.Command) (string, bool) {
		pattFn := map[*regexp.Regexp]func([]string) (string, bool){
			// Doc help
			regexp.MustCompile(`^doc /\?$`): func(s []string) (string, bool) {
				return "", false
			},
			// Get doc
			regexp.MustCompile(`^doc get ([a-zA-Z0-9\.\-_]+) ([a-zA-Z0-9\.\-_]+)$`): func(s []string) (string, bool) {
				fmt.Println("...", s[0])
				fmt.Println("...", s[1])
				fmt.Println("...", s[2])

				d := backend.Resource{
					Endpoint: "_mget",
					Scheme:   "http",
					Host:     backend.Server.Host,
					Port:     backend.Server.Port,
					Index:    s[1],
					Id:       s[2],
				}
				c := DocCmd{}
				r, ok := c.Get(d)
				return r, ok
			},
			// Delete doc
			regexp.MustCompile(`^doc delete ([a-zA-Z0-9\.\-_]+) ([a-zA-Z0-9\.\-_]+) ([a-zA-Z0-9\.\-_]+)$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Endpoint: "_mget",
					Scheme:   "http",
					Host:     backend.Server.Host,
					Port:     backend.Server.Port,
					Index:    s[0],
					Type:     s[1],
					Id:       s[2],
				}
				c := DocCmd{}
				r, ok := c.Delete(d)
				return r, ok
			},
		}
		r, ok := h.Tokenize(strings.TrimSpace(cmd.Instruction), pattFn)
		return r, ok
	}
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		r, ok := h.CommandParser(cmd)
		if !ok {
			if r != "" {
				r += "\n\n"
			}
			return h.Usage, false
		}
		return r, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
Example #5
0
func init() {
	h := backend.NewHandler()
	h.CommandName = "count"
	h.CommandPattern = "(count)( )(.*)"
	h.Usage = "count [host port] indexName [type]"
	h.CommandParser = func(cmd *backend.Command) (string, bool) {
		pattFn := map[*regexp.Regexp]func([]string) (string, bool){
			// Get count: require: index; optional: type
			regexp.MustCompile(`^count ([a-zA-Z0-9\.\-]+)(( )([a-zA-Z0-9\.\-]+))?$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Scheme:   "http",
					Host:     backend.Server.Host,
					Port:     backend.Server.Port,
					Index:    s[1],
					Type:     s[4],
					Endpoint: "_count",
				}
				c := CountCmd{}
				r, ok := c.Get(d)
				return r, ok
			},
			// Get count: require: host, port, index; optional: type
			regexp.MustCompile(`^count ([a-zA-Z0-9\.\-]+) ([0-9]{1,5}) ([a-zA-Z0-9\.\-]+)(( )([a-zA-Z0-9\.\-]+))?$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Scheme:   "http",
					Host:     s[1],
					Port:     s[2],
					Index:    s[3],
					Type:     s[6],
					Endpoint: "_count",
				}
				c := CountCmd{}
				r, ok := c.Get(d)
				return r, ok
			},
			// Count help
			regexp.MustCompile(`^count /\?$`): func(s []string) (string, bool) {
				return "", false
			},
		}
		r, ok := h.Tokenize(strings.TrimSpace(cmd.Instruction), pattFn)
		return r, ok
	}
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		r, ok := h.CommandParser(cmd)
		if !ok {
			if r != "" {
				r += "\n\n"
			}
			return h.Usage, false
		}
		return r, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
Example #6
0
func init() {
	h := backend.NewHandler()
	h.CommandName = "log"
	h.CommandPattern = "(log)( )(.*)"
	h.Usage = "log"
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		backend.LogLevel = 1
		return "Logging level set to: " + strconv.Itoa(backend.LogLevel), true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
Example #7
0
func init() {
	h := backend.NewHandler()
	h.CommandName = "exit"
	h.CommandPattern = "(exit)( )(.*)"
	h.Usage = "exit"
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		fmt.Println("Bye.")
		os.Exit(0)
		return "", true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
Example #8
0
func init() {
	h := backend.NewHandler()
	h.CommandName = "index"
	h.CommandPattern = "(index)(( )(.*))"
	h.Usage = "index ( (create host port indexName) | (delete host port indexName) )"
	h.CommandParser = func(cmd *backend.Command) (string, bool) {
		pattFn := map[*regexp.Regexp]func([]string) (string, bool){
			// Index help
			regexp.MustCompile(`^index /\?$`): func(s []string) (string, bool) {
				return "", false
			},
			// Create index
			//curl -XPUT "http://localhost:9200/indexname/"
			regexp.MustCompile(`^index create ([a-zA-Z0-9\.\-]+) ([0-9]{1,5}) ([a-zA-Z0-9\.\-]+)$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Scheme: "http",
					Host:   s[1],
					Port:   s[2],
					Index:  s[3],
				}
				c := IndexCmd{}
				r, ok := c.Create(d)
				return r, ok
			},
			// Delete index
			regexp.MustCompile(`^index delete ([a-zA-Z0-9\.\-]+) ([0-9]{1,5}) ([a-zA-Z0-9\.\-]+)$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Scheme: "http",
					Host:   s[1],
					Port:   s[2],
					Index:  s[3],
				}
				c := IndexCmd{}
				r, ok := c.Delete(d)
				return r, ok
			},
		}
		r, ok := h.Tokenize(strings.TrimSpace(cmd.Instruction), pattFn)
		return r, ok
	}
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		r, ok := h.CommandParser(cmd)
		if !ok {
			if r != "" {
				r += "\n\n"
			}
			return h.Usage, false
		}
		return r, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
Example #9
0
// reindex localhost:9200/srcindex/type localhost:9200/targetindex/routing
func init() {
	h := backend.NewHandler()
	h.CommandName = "reindex"
	h.CommandPattern = "(reindex)( )(.*)"
	h.Usage = "reindex sourceHost sourcePort sourceIndex sourceType targetHost targetPort targetIndex [routing]"
	h.CommandParser = func(cmd *backend.Command) (string, bool) {
		pattFn := map[*regexp.Regexp]func([]string) (string, bool){
			// Index help
			regexp.MustCompile(`^reindex /\?$`): func(s []string) (string, bool) {
				return "", false
			},
			// Reindex
			regexp.MustCompile(`^reindex ([a-zA-Z0-9\.\-]+) ([0-9]{1,5}) ([a-zA-Z0-9\.\-]+) ([a-zA-Z0-9\.\-]+) ([a-zA-Z0-9\.\-]+) ([0-9]{1,5}) ([a-zA-Z0-9\.\-]+) ([a-zA-Z0-9\.\-]+)?$`): func(s []string) (string, bool) {
				dSource := backend.Resource{
					Scheme: "http",
					Host:   s[1],
					Port:   s[2],
					Index:  s[3],
					Type:   s[4],
				}
				dTarget := backend.Resource{
					Scheme:  "http",
					Host:    s[5],
					Port:    s[6],
					Index:   s[7],
					Routing: s[8],
				}
				c := ReindexCmd{}
				r, ok := c.Reindex(dSource, dTarget)
				return r, ok
			},
		}
		r, ok := h.Tokenize(strings.TrimSpace(cmd.Instruction), pattFn)
		return r, ok
	}
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		fmt.Println("Reindexing...")
		r, ok := h.CommandParser(cmd)
		if !ok {
			if r != "" {
				r += "\n\n"
			}
			return h.Usage, false
		}
		return r, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
Example #10
0
// curl -XPUT "http://localhost:9200/movies/movie/1" -d'{ ... body ... }''
// becomes
// put movie/1 { "title": "Alien", "director": "Ridley Scott", "year": 1979, "genres": ["Science fiction"] }
// TODO: Currently, must be on single line.
func init() {
	h := backend.NewHandler()
	h.CommandName = "put"
	h.CommandPattern = "(put)( )(.*)"
	h.Usage = `put movie/1 { "title": "Alien", "director": "Ridley Scott", "year": 1979, "genres": ["Science fiction"] }`
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		queryHost := cmd.Tokens[1]
		queryPort := cmd.Tokens[2]
		queryArgs := cmd.Tokens[3]
		url := fmt.Sprintf("http://%s:%s/%s", queryHost, queryPort, queryArgs)
		//url := fmt.Sprintf("http://%s:%s/%s/%s", queryHost, queryPort, backend.Server.Index, queryArgs)
		fmt.Println("Request: put", url)
		res, err := backend.PutHttpResource(url, cmd.Body)
		if err != nil {
			return err.Error(), false
		}
		return res, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
Example #11
0
func init() {
	h := backend.NewHandler()
	h.CommandName = "load"
	h.CommandPattern = "(load)( )(.*)"
	h.Usage = "load filename"
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		if len(cmd.Tokens) != 2 {
			return h.Usage, false
		}
		filename := cmd.Tokens[1]
		file, err := ioutil.ReadFile(filename)
		if err != nil {
			return err.Error(), false
		}
		fileText := string(file)
		backend.LoadedRequest.Request = fileText
		return fileText, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
Example #12
0
func init() {
	h := backend.NewHandler()
	h.CommandName = "duplicatescount"
	h.CommandPattern = "(duplicatescount)(( )(.*))"
	h.Usage = "duplicatescount host port index type field"
	h.CommandParser = func(cmd *backend.Command) (string, bool) {
		pattFn := map[*regexp.Regexp]func([]string) (string, bool){
			// Duplicatescount help
			regexp.MustCompile(`^duplicatescount /\?$`): func(s []string) (string, bool) {
				return "", false
			},
			// Count duplicates
			regexp.MustCompile(`^duplicatescount ([a-zA-Z0-9\.\-]+) ([0-9]{1,5}) ([a-zA-Z0-9\.\-]+) ([a-zA-Z0-9\.\-]+) ([a-zA-Z0-9\.\-]+)$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Host:  s[1],
					Port:  s[2],
					Index: s[3],
					Type:  s[4],
					Field: s[5],
				}
				c := DuplicatesCountCmd{}
				r, ok := c.Do(d)
				return r, ok
			},
		}
		r, ok := h.Tokenize(strings.TrimSpace(cmd.Instruction), pattFn)
		return r, ok
	}
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		fmt.Println("Finding duplicates...")
		r, ok := h.CommandParser(cmd)
		if !ok {
			if r != "" {
				r += "\n\n"
			}
			return h.Usage, false
		}
		return r, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
Example #13
0
// curl -XPOST "http://localhost:9200/movies/_search?pretty" -d'{ ... body ... }''
// becomes
// post _search?pretty { "query": { "term": { "director": "scott" } } }
// TODO: Currently, must be on single line.
func init() {
	h := backend.NewHandler()
	h.CommandName = "post"
	h.CommandPattern = "((?i)post(?-i))( )(.*)"
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		fmt.Println("...>", cmd.Tokens)

		queryHost := cmd.Tokens[1]
		queryPort := cmd.Tokens[2]
		queryArgs := cmd.Tokens[3]
		url := fmt.Sprintf("http://%s:%s/%s", queryHost, queryPort, queryArgs)
		fmt.Println("Request: post", url)
		fmt.Println("Request: post body", cmd.Body)
		res, err := backend.PostHttpResource(url, cmd.Body)
		if err != nil {
			return err.Error(), false
		}
		return res, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
Example #14
0
func init() {
	h := backend.NewHandler()
	h.CommandName = "settings"
	h.CommandPattern = "(settings)( )(.*)"
	h.Usage = "settings indexName"
	h.CommandParser = func(cmd *backend.Command) (string, bool) {
		pattFn := map[*regexp.Regexp]func([]string) (string, bool){
			// Get settings
			regexp.MustCompile(`^settings ([a-zA-Z0-9\.\-]+)$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Scheme:   "http",
					Host:     backend.Server.Host,
					Port:     backend.Server.Port,
					Index:    s[1],
					Endpoint: "_settings",
				}
				c := SettingsCmd{}
				r, ok := c.Get(d)
				return r, ok
			},
			// Settings help
			regexp.MustCompile(`^settings /\?$`): func(s []string) (string, bool) {
				return "", false
			},
		}
		r, ok := h.Tokenize(strings.TrimSpace(cmd.Instruction), pattFn)
		return r, ok
	}
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		r, ok := h.CommandParser(cmd)
		if !ok {
			if r != "" {
				r += "\n\n"
			}
			return h.Usage, false
		}
		return r, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
Example #15
0
func init() {
	h := backend.NewHandler()
	h.CommandName = "delete"
	h.CommandPattern = "(delete)( )(.*)"
	// TODO: usage
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		//arg := cmd.Args[0]
		arg := strings.SplitN(cmd.Instruction, " ", 2)[1]
		u := new(url.URL)
		newUrl, err := u.Parse(arg)
		if err != nil {
			return "Unable to parse url: " + err.Error(), false
		}
		fmt.Println("Request:", newUrl)
		res, err := backend.DeleteHttpResource(newUrl.String())
		if err != nil {
			return err.Error(), false
		}
		return res, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
Example #16
0
func init() {
	h := backend.NewHandler()
	h.CommandName = "version"
	h.CommandPattern = "(version)(( )(.*))"
	h.Usage = "version host port"
	h.CommandParser = func(cmd *backend.Command) (string, bool) {
		pattFn := map[*regexp.Regexp]func([]string) (string, bool){
			// Get version
			regexp.MustCompile(`^version ([a-zA-Z0-9\.\-]+) ([0-9]{1,5})$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Scheme: "http",
					Host:   s[1],
					Port:   s[2],
				}
				c := VersionCmd{}
				r, ok := c.Get(d)
				return r, ok
			},
			// Version help
			regexp.MustCompile(`^version /\?$`): func(s []string) (string, bool) {
				return "", false
			},
		}
		r, ok := h.Tokenize(strings.TrimSpace(cmd.Instruction), pattFn)
		return r, ok
	}
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		r, ok := h.CommandParser(cmd)
		if !ok {
			if r != "" {
				r += "\n\n"
			}
			return h.Usage, false
		}
		return r, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
Example #17
0
func init() {
	h := backend.NewHandler()
	h.CommandName = "dir"
	h.CommandPattern = "(dir)( )(.*)"
	h.Usage = "dir"
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		dir := ""
		if len(cmd.Tokens) == 1 {
			dir = "."
		} else {
			dir = cmd.Tokens[1]
		}
		dirFiles, err := ioutil.ReadDir(dir)
		if err != nil {
			return err.Error(), false
		}
		files := ""
		for _, j := range dirFiles {
			files += j.Name() + "\n"
		}
		return files, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
Example #18
0
func init() {
	h := backend.NewHandler()
	h.CommandName = "alias"
	h.CommandPattern = "(alias)( )(.*)"
	h.Usage = "alias\n" +
		"  alias host port\n" +
		"  alias create host port indexName aliasName\n" +
		"  alias remove host port indexName aliasName\n" +
		"  alias move host port fromIndex toIndex aliasName"
	h.CommandParser = func(cmd *backend.Command) (string, bool) {
		pattFn := map[*regexp.Regexp]func([]string) (string, bool){
			// List all aliases on host
			regexp.MustCompile(`^alias$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Endpoint: "_aliases",
					Scheme:   "http",
					Host:     backend.Server.Host,
					Port:     backend.Server.Port,
				}
				c := AliasCmd{}
				r, ok := c.GetAll(d)
				return r, ok
			},
			regexp.MustCompile(`^alias ([a-zA-Z0-9\.\-]+) ([0-9]{1,5})$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Endpoint: "_aliases",
					Scheme:   "http",
					Host:     s[1],
					Port:     s[2],
				}
				c := AliasCmd{}
				r, ok := c.GetAll(d)
				return r, ok
			},
			// Alias help
			regexp.MustCompile(`^alias /\?$`): func(s []string) (string, bool) {
				return "", false
			},
			// Create alias
			regexp.MustCompile(`^alias create ([a-zA-Z0-9\.\-]+) ([0-9]{1,5}) ([a-zA-Z0-9\.\-]+) ([a-zA-Z0-9\.\-]+)$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Endpoint: "_aliases",
					Host:     s[1],
					Port:     s[2],
					Index:    s[3],
					Alias:    s[4],
				}
				c := AliasCmd{}
				r, ok := c.Create(d)
				return r, ok
			},
			// Remove alias
			regexp.MustCompile(`^alias remove ([a-zA-Z0-9\.\-]+) ([0-9]{1,5}) ([a-zA-Z0-9\.\-]+) ([a-zA-Z0-9\.\-]+)$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Endpoint: "_aliases",
					Host:     s[1],
					Port:     s[2],
					Index:    s[3],
					Alias:    s[4],
				}
				c := AliasCmd{}
				r, ok := c.Remove(d)
				return r, ok
			},
			// Move alias
			regexp.MustCompile(`^alias move ([a-zA-Z0-9\.\-]+) ([0-9]{1,5}) ([a-zA-Z0-9\.\-]+) ([a-zA-Z0-9\.\-]+) ([a-zA-Z0-9\.\-]+)$`): func(s []string) (string, bool) {
				dFrom := backend.Resource{
					Endpoint: "_aliases",
					Host:     s[1],
					Port:     s[2],
					Index:    s[3],
					Alias:    s[5],
				}
				dTarget := backend.Resource{
					Index: s[4],
					Alias: s[5],
				}
				c := AliasCmd{}
				r, ok := c.Move(dFrom, dTarget)
				return r, ok
			},
		}
		r, ok := h.Tokenize(strings.TrimSpace(cmd.Instruction), pattFn)
		return r, ok
	}
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		r, ok := h.CommandParser(cmd)
		if !ok {
			if r != "" {
				r += "\n\n"
			}
			return h.Usage, false
		}
		return r, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}
Example #19
0
// reindex localhost:9200/srcindex/type localhost:9200/targetindex/routing
func init() {
	h := backend.NewHandler()
	h.CommandName = "update"
	h.CommandPattern = "(update)( )(.*)"
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		fmt.Println("Updating...")
		//args := strings.TrimPrefix(entry, CommandReindex+" ")
		//args := cmd.Args[0]
		args := strings.SplitN(cmd.Instruction, " ", 2)[1]

		// \w+|"[\w\s]*"
		r, err := regexp.Compile(`^(.*?):(\d+?)/(.*?)/(.*?)/? (.*?):(\d+?)/(.*?)(/(.*?))?$`)
		if err != nil {
			return err.Error(), false
		}
		fmt.Println("Parsing command...")
		matches := r.FindAllStringSubmatch(args, -1)[0]
		fmt.Println("Parsed matches:", len(matches))
		srcHost := matches[1]
		srcPort := matches[2]
		srcIndex := matches[3]
		srcType := matches[4]
		tgtHost := matches[5]
		tgtPort := matches[6]
		tgtIndex := matches[7]
		tgtRouting := matches[9]

		api := elastigo.NewConn()
		api.Domain = srcHost
		api.Port = srcPort

		fmt.Println("Scanning...")
		scanArgs := map[string]interface{}{"search_type": "scan", "scroll": "1m", "size": "1000"}
		scanResult, err := api.SearchUri(srcIndex, srcType, scanArgs)
		if err != nil {
			fmt.Println("Failed getting scan result for index:", srcIndex, "; err:", err)
			return err.Error(), false
		}

		//total := scanResult.Hits.Total

		scrollId := scanResult.ScrollId
		counter := 0
		failures := 0

		fmt.Println("Scrolling...")
		scrollArgs := map[string]interface{}{"scroll": "1m"}
		scrollResult, err := api.Scroll(scrollArgs, scrollId)
		if err != nil {
			fmt.Println("Failed getting scroll result for index:", srcIndex, "; err:", err)
			return err.Error(), false
		}

		fmt.Println("Indexing...")
		var indexArgs map[string]interface{} = nil
		if tgtRouting != "" {
			indexArgs = map[string]interface{}{"routing": tgtRouting}
		}
		for len(scrollResult.Hits.Hits) > 0 {
			fmt.Println("Scroll result hits:", len(scrollResult.Hits.Hits))
			for _, j := range scrollResult.Hits.Hits {
				api.Domain = tgtHost
				api.Port = tgtPort

				_, err := api.Index(tgtIndex, srcType, j.Id, indexArgs, j.Source)
				if err != nil {
					fmt.Println("Failed inserting document, id:", j.Id, "; ", err)
					failures++
					continue
				}
				counter++
			}

			api.Domain = srcHost
			api.Port = srcPort
			// ScrollId changes with every request.
			scrollId = scrollResult.ScrollId
			scrollArgs := map[string]interface{}{"scroll": "1m"}
			scrollResult, err = api.Scroll(scrollArgs, scrollId)
			if err != nil {
				fmt.Println("Failed getting scroll result for index:", srcIndex, "; err:", err)
				return err.Error(), false
			}
		}
		return fmt.Sprintf("Total processed: %d.  %d failed.", counter, failures), true
	}
	// TODO: implement this command and uncomment the registration below.
	//backend.HandlerRegistry[h.CommandName] = h
}
Example #20
0
func init() {
	h := backend.NewHandler()
	h.CommandName = "env"
	h.CommandPattern = "(env)(( )(.*))"
	h.Usage = "env [ (host hostname) | (port portnumber) | (index indexname ) ]"
	h.CommandParser = func(cmd *backend.Command) (string, bool) {
		pattFn := map[*regexp.Regexp]func([]string) (string, bool){
			// Get env
			regexp.MustCompile(`^env$`): func(s []string) (string, bool) {
				d := backend.Resource{}
				c := EnvCmd{}
				r, ok := c.Get(d)
				return r, ok
			},
			// Env help
			regexp.MustCompile(`^env /\?$`): func(s []string) (string, bool) {
				return "", false
			},
			// Set env host
			regexp.MustCompile(`^env host ([a-zA-Z0-9\.\-]+)$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Host: s[1],
				}
				c := EnvCmd{}
				r, ok := c.SetHost(d)
				return r, ok
			},
			// Set env port
			regexp.MustCompile(`^env port ([0-9]{1,5})$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Port: s[1],
				}
				c := EnvCmd{}
				r, ok := c.SetPort(d)
				return r, ok
			},
			// Set env index
			regexp.MustCompile(`^env index ([a-zA-Z0-9\.\-]+)$`): func(s []string) (string, bool) {
				d := backend.Resource{
					Index: s[1],
				}
				c := EnvCmd{}
				r, ok := c.SetIndex(d)
				return r, ok
			},
		}
		r, ok := h.Tokenize(strings.TrimSpace(cmd.Instruction), pattFn)
		return r, ok
	}
	h.HandlerFunc = func(cmd *backend.Command) (string, bool) {
		r, ok := h.CommandParser(cmd)
		if !ok {
			if r != "" {
				r += "\n\n"
			}
			return h.Usage, false
		}
		return r, true
	}
	backend.HandlerRegistry[h.CommandName] = h
}