<commit_before>// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor: <NAME> <EMAIL> [:ulfr]
package main

import (
	"fmt"
	"github.com/jvehent/cljs"
	"mig"
	"mig/client"
	"regexp"
	"strings"
	"time"
)

type searchParameters struct {
	sType   string
	query   string
	version string
}

// search runs a search for actions, commands or agents
func search(input string, cli client.Client) (err error) {
	defer func() {
		if e := recover(); e != nil {
			err = fmt.Errorf("search() -> %v", e)
		}
	}()
	orders := strings.Split(input, " ")
	if len(orders) < 2 {
		orders = append(orders, "help")
	}
	sType := ""
	switch orders[1] {
	case "action", "agent", "command", "investigator":
		sType = orders[1]
	case "", "help":
		fmt.Printf(`usage: search <action|agent|command|investigator> where <parameters> [<and|or> <parameters>]
The following search parameters are available:
`)
		return nil
	default:
		return fmt.Errorf("Invalid search '%s'. Try `search help`.\n", input)
	}
	sp, err := parseSearchQuery(orders)
	if err != nil {
		panic(err)
	}
	items, err := runSearchQuery(sp, cli)
	if err != nil {
		panic(err)
	}
	switch sType {
	case "agent":
		agents, err := filterAgentItems(sp, items, cli)
		if err != nil {
			panic(err)
		}
		fmt.Println("----    ID      ---- + ----         Name         ---- + -- Last Heartbeat --")
		for _, agt := range agents {
			name := agt.Name
			if useShortNames {
				name = shorten(name)
			}
			if len(name) < 30 {
				for i := len(name); i < 30; i++ {
					name += " "
				}
			}
			if len(name) > 30 {
				name = name[0:27] + "..."
			}
			fmt.Printf("%20.0f   %s   %s\n", agt.ID, name[0:30], agt.HeartBeatTS.Format(time.RFC3339))
		}
	case "action", "command":
		fmt.Println("----    ID      ---- + ----         Name         ---- + --- Last Updated ---")
		for _, item := range items {
			for _, data := range item.Data {
				if data.Name != sType {
					continue
				}
				switch data.Name {
				case "action":
					idstr, name, datestr, _, err := actionPrintShort(data.Value)
					if err != nil {
						panic(err)
					}
					fmt.Printf("%s   %s   %s\n", idstr, name, datestr)
				case "command":
					cmd, err := client.ValueToCommand(data.Value)
					if err != nil {
						panic(err)
					}
					name := cmd.Action.Name
					if len(name) < 30 {<commit_msg>[minor] display status in console agent search results<commit_after>// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor: <NAME> <EMAIL> [:ulfr]
package main

import (
	"fmt"
	"github.com/jvehent/cljs"
	"mig"
	"mig/client"
	"regexp"
	"strings"
	"time"
)

type searchParameters struct {
	sType   string
	query   string
	version string
}

// search runs a search for actions, commands or agents
func search(input string, cli client.Client) (err error) {
	defer func() {
		if e := recover(); e != nil {
			err = fmt.Errorf("search() -> %v", e)
		}
	}()
	orders := strings.Split(input, " ")
	if len(orders) < 2 {
		orders = append(orders, "help")
	}
	sType := ""
	switch orders[1] {
	case "action", "agent", "command", "investigator":
		sType = orders[1]
	case "", "help":
		fmt.Printf(`usage: search <action|agent|command|investigator> where <parameters> [<and|or> <parameters>]
The following search parameters are available:
`)
		return nil
	default:
		return fmt.Errorf("Invalid search '%s'. Try `search help`.\n", input)
	}
	sp, err := parseSearchQuery(orders)
	if err != nil {
		panic(err)
	}
	items, err := runSearchQuery(sp, cli)
	if err != nil {
		panic(err)
	}
	switch sType {
	case "agent":
		agents, err := filterAgentItems(sp, items, cli)
		if err != nil {
			panic(err)
		}
		fmt.Println("----    ID      ---- + ----         Name         ---- + -- Status -- + -- Last Heartbeat --")
		for _, agt := range agents {
			name := agt.Name
			if useShortNames {
				name = shorten(name)
			}
			if len(name) < 30 {
				for i := len(name); i < 30; i++ {
					name += " "
				}
			}
			if len(name) > 30 {
				name = name[0:27] + "..."
			}
			status := agt.Status
			if len(status) < 12 {
				for i := len(status); i < 12; i++ {
					status += " "
				}
			}
			if len(status) > 12 {
				status = status[0:12]
			}
			fmt.Printf("%20.0f   %s   %s   %s\n", agt.ID, name, status, agt.HeartBeatTS.Format(time.RFC3339))
		}
	case "action", "command":
		fmt.Println("----    ID      ---- + ----         Name         ---- + --- Last Updated ---")
		for _, item := range items {
			for _, data := range item.Data {
				if data.Name != sType {
					continue
				}
				switch data.Name {
				case "action":
					idstr, name, datestr, _, err := actionPrintShort(data.Value)
					if err != nil {
						panic(err)
					}
					fmt.Printf("%s   %s   %s\n", idstr, name, datestr)
				case "command":
					cmd, err := client.ValueToCommand(data.Value)
					if err != nil {
						panic(err)
					}
					name := cmd.Action.Name
					if len(name) < 30 {
{'fraction_non_alphanumeric': 0.16989164086687306, 'fraction_numerical': 0.011996904024767802, 'mean_word_length': 2.4163912756113683, 'pattern_counts': {'":': 12, '<': 25, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2209619', 'n_tokens_mistral': 2152, 'n_tokens_neox': 1836, 'n_words': 519}