Skip to content

Commit

Permalink
Merge pull request #31 from wesen/task/add-default-index
Browse files Browse the repository at this point in the history
Add default index configuration and raw results output option
  • Loading branch information
wesen authored Jan 29, 2025
2 parents 7279702 + bb6c5f5 commit 22f9091
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
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.

0 comments on commit 22f9091

Please sign in to comment.