mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 04:54:18 +02:00
add index template {ls,sh} (#33)
This commit is contained in:
@@ -27,7 +27,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
SETTINGS = `https://www.elastic.co/docs/reference/elasticsearch/configuration-reference`
|
||||
SETTINGS = ``
|
||||
)
|
||||
|
||||
func ClusterSettings(conf *cfg.Config) *cli.Command {
|
||||
@@ -85,7 +85,9 @@ func ClusterSettingsSet(conf *cfg.Config) *cli.Command {
|
||||
Name: "set",
|
||||
Usage: "set|update cluster settings",
|
||||
Aliases: []string{"set", "update"},
|
||||
UsageText: "set [options] setting:value [setting:value ...]\n\nReference: " + SETTINGS,
|
||||
UsageText: "set [options] setting:value [setting:value ...]",
|
||||
CustomHelpTemplate: addReference(
|
||||
"https://www.elastic.co/docs/reference/elasticsearch/configuration-reference"),
|
||||
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
|
||||
@@ -41,8 +41,11 @@ func Index(conf *cfg.Config) *cli.Command {
|
||||
IndexClose(conf),
|
||||
IndexAllocation(conf),
|
||||
IndexModify(conf),
|
||||
IndexAlias(conf),
|
||||
IndexFields(conf),
|
||||
|
||||
// sub commands
|
||||
IndexAlias(conf),
|
||||
IndexTemplate(conf),
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -165,6 +168,7 @@ Valid field mapping types: integer, text, date, keyword`,
|
||||
Usage: "number of replicas to create",
|
||||
Destination: &conf.Replicas,
|
||||
Aliases: []string{"r"},
|
||||
Value: 1,
|
||||
},
|
||||
},
|
||||
|
||||
@@ -240,6 +244,7 @@ func IndexModify(conf *cfg.Config) *cli.Command {
|
||||
Usage: "number of replicas",
|
||||
Destination: &conf.Replicas,
|
||||
Aliases: []string{"r"},
|
||||
Value: 1,
|
||||
},
|
||||
},
|
||||
|
||||
|
||||
203
cmd/index_template.go
Normal file
203
cmd/index_template.go
Normal file
@@ -0,0 +1,203 @@
|
||||
/*
|
||||
Copyright © 2026 Thomas von Dein
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"codeberg.org/scip/esctl/pkg/cfg"
|
||||
"codeberg.org/scip/esctl/pkg/es"
|
||||
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
func IndexTemplate(conf *cfg.Config) *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "template",
|
||||
Aliases: []string{"tpl"},
|
||||
Usage: "manage index templates",
|
||||
|
||||
Commands: []*cli.Command{
|
||||
IndexTemplateList(conf),
|
||||
IndexTemplateShow(conf),
|
||||
IndexTemplateCreate(conf, false),
|
||||
IndexTemplateCreate(conf, true),
|
||||
IndexTemplateDelete(conf),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func IndexTemplateList(conf *cfg.Config) *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "list",
|
||||
Aliases: []string{"ls"},
|
||||
Usage: "list index templates",
|
||||
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
return es.IndexTemplateList(conf)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func IndexTemplateShow(conf *cfg.Config) *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "show",
|
||||
Aliases: []string{"sh"},
|
||||
Usage: "show details about an index template",
|
||||
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
index := cmd.Args().Get(0)
|
||||
if index == "" {
|
||||
return errors.New("no index template specified")
|
||||
}
|
||||
|
||||
return es.IndexTemplateShow(conf, cmd.Args().Get(0))
|
||||
},
|
||||
|
||||
ShellComplete: func(ctx context.Context, cmd *cli.Command) {
|
||||
complete(cmd, Cindex)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func IndexTemplateCreate(conf *cfg.Config, modify bool) *cli.Command {
|
||||
name := "create"
|
||||
alias := "+"
|
||||
required := true
|
||||
|
||||
if modify {
|
||||
name = "modify"
|
||||
alias = "mod"
|
||||
required = false
|
||||
}
|
||||
|
||||
return &cli.Command{
|
||||
Name: name,
|
||||
Aliases: []string{alias},
|
||||
Usage: name + " a new index template",
|
||||
UsageText: name + ` <name> <fieldmapping:type>...`,
|
||||
CustomHelpTemplate: addReference(
|
||||
`Valid field mapping types: integer, text, date, keyword
|
||||
|
||||
Doc for index settings:
|
||||
https://www.elastic.co/docs/reference/elasticsearch/index-settings
|
||||
`),
|
||||
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: "data-stream",
|
||||
Usage: "template creates a data stream",
|
||||
Destination: &conf.Stream,
|
||||
Aliases: []string{"S"},
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "allow-auto-create",
|
||||
Usage: "automatically create indices",
|
||||
Destination: &conf.AutoCreate,
|
||||
Aliases: []string{"a"},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "mode",
|
||||
Usage: "index mode, one of: standard, timeseries, logsdb or lookup",
|
||||
Destination: &conf.Mode,
|
||||
Aliases: []string{"m"},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "retention",
|
||||
Usage: "data retention, e.g. -1 (infinitely), 30d, 3m or 1y (data stream only)",
|
||||
Destination: &conf.Retention,
|
||||
Aliases: []string{"r"},
|
||||
Required: required,
|
||||
},
|
||||
&cli.IntFlag{
|
||||
Name: "priority",
|
||||
Usage: "index template priority",
|
||||
Destination: &conf.Priority,
|
||||
Aliases: []string{"p"},
|
||||
},
|
||||
&cli.StringSliceFlag{
|
||||
Name: "index-pattern",
|
||||
Usage: "index matching pattern",
|
||||
Destination: &conf.Patterns,
|
||||
Aliases: []string{"i"},
|
||||
Required: required,
|
||||
},
|
||||
&cli.StringSliceFlag{
|
||||
Name: "settings",
|
||||
Usage: "individual template settings, format: name:value, multiple possible",
|
||||
Destination: &conf.Settings,
|
||||
Aliases: []string{"s"},
|
||||
},
|
||||
&cli.StringSliceFlag{
|
||||
Name: "component",
|
||||
Usage: "include component template, multiple possible",
|
||||
Destination: &conf.Components,
|
||||
Aliases: []string{"c"},
|
||||
},
|
||||
&cli.StringSliceFlag{
|
||||
Name: "meta",
|
||||
Usage: "custom meta field, format: name:value, multiple possible",
|
||||
Destination: &conf.Meta,
|
||||
Aliases: []string{"M"},
|
||||
},
|
||||
&cli.StringSliceFlag{
|
||||
Name: "aliases",
|
||||
Usage: "set up aliases to associate with indices",
|
||||
Destination: &conf.Aliases,
|
||||
Aliases: []string{"A"},
|
||||
},
|
||||
},
|
||||
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
args := cmd.Args()
|
||||
if args.Len() == 0 {
|
||||
return fmt.Errorf("no name specified")
|
||||
}
|
||||
|
||||
mappings := args.Slice()[1:]
|
||||
|
||||
if modify {
|
||||
return es.IndexTemplateModify(conf, args.Get(0), mappings)
|
||||
}
|
||||
|
||||
return es.IndexTemplateCreate(conf, args.Get(0), mappings)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func IndexTemplateDelete(conf *cfg.Config) *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "delete",
|
||||
Aliases: []string{"rm"},
|
||||
Usage: "delete an index template",
|
||||
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
index := cmd.Args().Get(0)
|
||||
if index == "" {
|
||||
return errors.New("no index template specified")
|
||||
}
|
||||
|
||||
return es.IndexTemplateDelete(conf, cmd.Args().Get(0))
|
||||
},
|
||||
|
||||
ShellComplete: func(ctx context.Context, cmd *cli.Command) {
|
||||
complete(cmd, Cindex)
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ import (
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
const SearchUsage = `<sep> might be one of:
|
||||
const SearchUsage = `field <sep> might be one of:
|
||||
=: Must match
|
||||
!=: Must not match
|
||||
|
||||
@@ -43,15 +43,15 @@ For datetime range format refer to:
|
||||
https://www.elastic.co/docs/reference/elasticsearch/rest-apis/common-options#date-math
|
||||
|
||||
For timestamp formats refer to:
|
||||
https://www.elastic.co/docs/reference/elasticsearch/mapping-reference/mapping-date-format
|
||||
`
|
||||
https://www.elastic.co/docs/reference/elasticsearch/mapping-reference/mapping-date-format`
|
||||
|
||||
func Search(conf *cfg.Config) *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "search",
|
||||
Aliases: []string{"/"},
|
||||
Usage: "search within an index",
|
||||
UsageText: "search [options] [<[field<sep>]pattern> ...]\n" + SearchUsage,
|
||||
Name: "search",
|
||||
Aliases: []string{"/"},
|
||||
Usage: "search within an index",
|
||||
UsageText: "search [options] [<[field<sep>]pattern> ...]\n",
|
||||
CustomHelpTemplate: addReference(SearchUsage),
|
||||
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
|
||||
35
cmd/utilities.go
Normal file
35
cmd/utilities.go
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
Copyright © 2026 Thomas von Dein
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
func addReference(ref string) string {
|
||||
indentedRef := []string{}
|
||||
|
||||
for _, line := range strings.Split(ref, "\n") {
|
||||
indentedRef = append(indentedRef, " "+line)
|
||||
}
|
||||
return fmt.Sprintf("%s\nREFERENCE:\n%s\n",
|
||||
cli.SubcommandHelpTemplate,
|
||||
strings.Join(indentedRef, "\n"))
|
||||
}
|
||||
Reference in New Issue
Block a user