Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default index configuration and raw results output option #31

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions pkg/cmds/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"strings"

"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
es_layers "github.com/go-go-golems/escuse-me/pkg/cmds/layers"
Expand All @@ -19,8 +22,6 @@ import (
"github.com/pkg/errors"
orderedmap "github.com/wk8/go-ordered-map/v2"
"gopkg.in/yaml.v3"
"io"
"strings"
)

type EscuseMeCommandDescription struct {
Expand All @@ -31,6 +32,7 @@ type EscuseMeCommandDescription struct {
Flags []*parameters.ParameterDefinition `yaml:"flags,omitempty"`
Arguments []*parameters.ParameterDefinition `yaml:"arguments,omitempty"`

DefaultIndex string `yaml:"default-index,omitempty"`
QueryTemplate string `yaml:"queryTemplate,omitempty"`
// Query is used for single file escuse-me commands, while QueryTemplate is used for directories,
// where main.yaml is used to describe the command and the file given in the query template
Expand All @@ -44,6 +46,7 @@ type ElasticSearchCommand struct {
*cmds.CommandDescription `yaml:",inline"`
QueryStringTemplate string `yaml:"query"`
QueryNodeTemplate *RawNode
DefaultIndex string `yaml:"default-index,omitempty"`
clientFactory ESClientFactory
}

Expand All @@ -54,6 +57,7 @@ func NewElasticSearchCommand(
clientFactory ESClientFactory,
queryStringTemplate string,
queryNodeTemplate *RawNode,
defaultIndex string,
) (*ElasticSearchCommand, error) {
glazedParameterLayer, err := settings.NewGlazedParameterLayers()
if err != nil {
Expand All @@ -74,6 +78,7 @@ func NewElasticSearchCommand(
clientFactory: clientFactory,
QueryStringTemplate: queryStringTemplate,
QueryNodeTemplate: queryNodeTemplate,
DefaultIndex: defaultIndex,
}, nil
}

Expand Down Expand Up @@ -244,6 +249,10 @@ func (esc *ElasticSearchCommand) RunQueryIntoGlaze(
os = append(os, es.Search.WithExplain(esHelperSettings.Explain))
if esHelperSettings.Index != "" {
os = append(os, es.Search.WithIndex(esHelperSettings.Index))
} else if esc.DefaultIndex != "" {
os = append(os, es.Search.WithIndex(esc.DefaultIndex))
} else {
return errors.New("No index specified")
}

res, err := es.Search(os...)
Expand All @@ -269,12 +278,23 @@ func (esc *ElasticSearchCommand) RunQueryIntoGlaze(
}
}

var r ElasticSearchResult
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}

if esHelperSettings.RawResults {
// For raw results, just output the entire response as a single row
row := orderedmap.New[string, interface{}]()
row.Set("raw_results", json.RawMessage(body))
err = gp.AddRow(ctx, row)
if err != nil {
return err
}
return nil
}

var r ElasticSearchResult
if err := json.Unmarshal(body, &r); err != nil {
return errors.New("Error parsing the response body")
}
Expand Down
1 change: 1 addition & 0 deletions pkg/cmds/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func GetCobraCommandEscuseMeMiddlewares(
middlewares.WrapWithWhitelistedLayers(
[]string{
layers.EsConnectionSlug,
layers.ESHelpersSlug,
},
middlewares.GatherFlagsFromViper(parameters.WithParseStepSource("viper")),
),
Expand Down
7 changes: 7 additions & 0 deletions pkg/cmds/layers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type ESHelperSettings struct {
PrintQuery bool `glazed.parameter:"print-query"`
Explain bool `glazed.parameter:"explain"`
Index string `glazed.parameter:"es-index"`
RawResults bool `glazed.parameter:"raw-results"`
}

func NewESHelpersParameterLayer(
Expand All @@ -34,6 +35,12 @@ func NewESHelpersParameterLayer(
parameters.ParameterTypeString,
parameters.WithHelp("The index to search in"),
),
parameters.NewParameterDefinition(
"raw-results",
parameters.ParameterTypeBool,
parameters.WithHelp("Whether to return raw results"),
parameters.WithDefault(false),
),
))
ret, err := layers.NewParameterLayer(ESHelpersSlug, "ES Helpers", options_...)
if err != nil {
Expand Down
16 changes: 14 additions & 2 deletions pkg/cmds/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ func (escl *ElasticSearchCommandLoader) loadCommandsFromFile(
options_...,
)

esc, err := NewElasticSearchCommand(description, escl.clientFactory, "", escd.Query)
esc, err := NewElasticSearchCommand(
description,
escl.clientFactory,
"",
escd.Query,
escd.DefaultIndex,
)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -204,7 +210,13 @@ func (escl *ElasticSearchCommandLoader) loadCommandsFromDir(
options_...,
)

esc, err := NewElasticSearchCommand(description, escl.clientFactory, queryTemplate, nil)
esc, err := NewElasticSearchCommand(
description,
escl.clientFactory,
queryTemplate,
escd.Query,
escd.DefaultIndex,
)
if err != nil {
return nil, err
}
Expand Down
1 change: 0 additions & 1 deletion pkg/helpers/error.go

This file was deleted.

Loading