From b8e0ee074ceb414d3d5c8ed4401635a5f7974b80 Mon Sep 17 00:00:00 2001 From: "T. von Dein" Date: Tue, 7 Jul 2026 10:41:34 +0200 Subject: [PATCH] internal/gofix and fix error wrapping (#76) --- .woodpecker/build.yaml | 4 ++-- cmd/utilities.go | 2 +- go.mod | 2 +- pkg/cfg/transport.go | 2 +- pkg/es/api.go | 8 ++++---- pkg/es/ccr.go | 4 ++-- pkg/es/ccr_follower.go | 12 ++++++------ pkg/es/cluster.go | 6 ++---- pkg/es/cluster_settings.go | 12 ++++++------ pkg/es/datastream.go | 14 +++++++------- pkg/es/doc.go | 10 +++++----- pkg/es/errors.go | 14 +++++++++----- pkg/es/ilm.go | 14 +++++++------- pkg/es/ilm_forecast.go | 8 ++------ pkg/es/index.go | 14 +++++++------- pkg/es/index_alias.go | 8 ++++---- pkg/es/index_template.go | 21 ++++++++++----------- pkg/es/license.go | 2 +- pkg/es/node.go | 10 +++++----- pkg/es/parallel.go | 2 +- pkg/es/role.go | 6 +++--- pkg/es/role_diff.go | 12 ++++++------ pkg/es/search.go | 14 +++++++------- pkg/es/shard.go | 6 +++--- pkg/es/snapshot.go | 6 +++--- pkg/es/task.go | 4 ++-- pkg/printer/pager.go | 7 ------- pkg/printer/table.go | 4 ++-- 28 files changed, 109 insertions(+), 119 deletions(-) diff --git a/.woodpecker/build.yaml b/.woodpecker/build.yaml index 6b90521..1fc42a8 100644 --- a/.woodpecker/build.yaml +++ b/.woodpecker/build.yaml @@ -2,7 +2,7 @@ matrix: platform: - linux/amd64 goversion: - - 1.25.8 + - 1.26.4 labels: platform: ${platform} @@ -21,6 +21,6 @@ steps: event: [push,manual] image: golang:${goversion} commands: - - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh | sh -s -- -b $(go env GOPATH)/bin v2.5.0 + - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh | sh -s -- -b $(go env GOPATH)/bin v2.12.2 - golangci-lint --version - golangci-lint run ./... diff --git a/cmd/utilities.go b/cmd/utilities.go index 85702eb..c8efb97 100644 --- a/cmd/utilities.go +++ b/cmd/utilities.go @@ -26,7 +26,7 @@ import ( func addReference(ref string) string { indentedRef := []string{} - for _, line := range strings.Split(ref, "\n") { + for line := range strings.SplitSeq(ref, "\n") { indentedRef = append(indentedRef, " "+line) } return fmt.Sprintf("%s\nREFERENCE:\n%s\n", diff --git a/go.mod b/go.mod index 152498b..a24f5bd 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ // along with this program. If not, see . module codeberg.org/scip/esctl -go 1.25.8 +go 1.26.4 require ( github.com/MichaelMure/go-term-markdown v0.1.4 diff --git a/pkg/cfg/transport.go b/pkg/cfg/transport.go index 3eb17b5..f4c8015 100644 --- a/pkg/cfg/transport.go +++ b/pkg/cfg/transport.go @@ -45,7 +45,7 @@ func (t *DebugTransport) RoundTrip(req *http.Request) (*http.Response, error) { var pretty bytes.Buffer err = json.Indent(&pretty, buf.Bytes(), "", "\t") if err != nil { - return nil, fmt.Errorf("json parse error: %s", err) + return nil, fmt.Errorf("json parse error: %w", err) } content = pretty.String() diff --git a/pkg/es/api.go b/pkg/es/api.go index 1ed3c24..fdb2fcc 100644 --- a/pkg/es/api.go +++ b/pkg/es/api.go @@ -94,7 +94,7 @@ func ApiRepl(conf *cfg.Config) error { }) if err != nil { - return fmt.Errorf("failed to initialize readline lib: %s", err) + return fmt.Errorf("failed to initialize readline lib: %w", err) } for { @@ -237,7 +237,7 @@ func CallAPI(conf *cfg.Config, verb, path, data string) ([]byte, error) { // Read and print response body, err := io.ReadAll(resp.Body) if err != nil { - return nil, fmt.Errorf("failed to read response body: %s", err) + return nil, fmt.Errorf("failed to read response body: %w", err) } return body, nil @@ -261,7 +261,7 @@ func prettyfiJson(conf *cfg.Config, raw []byte) (string, error) { var pretty bytes.Buffer err := json.Indent(&pretty, raw, "", "\t") if err != nil { - return "", fmt.Errorf("json parse error: %s", err) + return "", fmt.Errorf("json parse error: %w", err) } return pretty.String(), nil @@ -295,7 +295,7 @@ func readJSON(input string) (string, error) { check := map[string]any{} err := json.Unmarshal([]byte(data), &check) if err != nil { - return "", fmt.Errorf("error: input data is not proper JSON: %s", err) + return "", fmt.Errorf("error: input data is not proper JSON: %w", err) } diff --git a/pkg/es/ccr.go b/pkg/es/ccr.go index ca7c5a4..7817318 100644 --- a/pkg/es/ccr.go +++ b/pkg/es/ccr.go @@ -58,7 +58,7 @@ func CcrStatus(conf *cfg.Config, leader, follower string) error { res, err := conf.Clusters[alias].ES().Cat.Indices(). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get indicies on %s: %s", alias, esErrorString(err)) + return fmt.Errorf("failed to get indicies on %s: %w", alias, esErrorString(err)) } indices[alias] = make(map[string]*types.IndicesRecord, len(res)) @@ -87,7 +87,7 @@ func CcrRemoteInfo(conf *cfg.Config, index string) error { res, err := conf.DefaultCluster.ES().Cluster.RemoteInfo(). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to retrieve follower info: %s", esErrorString(err)) + return fmt.Errorf("failed to retrieve follower info: %w", esErrorString(err)) } slog.Debug("ccr remote info", "info", res) diff --git a/pkg/es/ccr_follower.go b/pkg/es/ccr_follower.go index e8bb674..b2da1b7 100644 --- a/pkg/es/ccr_follower.go +++ b/pkg/es/ccr_follower.go @@ -29,7 +29,7 @@ func getRemoteName(conf *cfg.Config) (string, error) { res, err := conf.DefaultCluster.ES().Cluster.RemoteInfo(). Do(context.Background()) if err != nil { - return "", fmt.Errorf("failed to retrieve follower info: %s", esErrorString(err)) + return "", fmt.Errorf("failed to retrieve follower info: %w", esErrorString(err)) } remote := "" @@ -91,7 +91,7 @@ func CcrFollowerResume(conf *cfg.Config, index string) error { Do(context.Background()) if err != nil { - return fmt.Errorf("failed to resume ccr following: %s", esErrorString(err)) + return fmt.Errorf("failed to resume ccr following: %w", esErrorString(err)) } return nil @@ -102,7 +102,7 @@ func CcrFollowerPause(conf *cfg.Config, index string) error { Do(context.Background()) if err != nil { - return fmt.Errorf("failed to pause ccr following: %s", esErrorString(err)) + return fmt.Errorf("failed to pause ccr following: %w", esErrorString(err)) } return nil @@ -113,7 +113,7 @@ func CcrFollowerUnfollow(conf *cfg.Config, index string) error { Do(context.Background()) if err != nil { - return fmt.Errorf("failed to unfollow index: %s", esErrorString(err)) + return fmt.Errorf("failed to unfollow index: %w", esErrorString(err)) } return nil @@ -136,7 +136,7 @@ func CcrFollowerAdd(conf *cfg.Config, index string) error { _, err = create.Do(context.Background()) if err != nil { - return fmt.Errorf("failed to create follower index: %s", esErrorString(err)) + return fmt.Errorf("failed to create follower index: %w", esErrorString(err)) } return nil @@ -146,7 +146,7 @@ func CcrFollowerShow(conf *cfg.Config, index string) error { res, err := conf.DefaultCluster.ES().Ccr.FollowStats(index). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to retrieve follower index info: %s", esErrorString(err)) + return fmt.Errorf("failed to retrieve follower index info: %w", esErrorString(err)) } slog.Debug("ES result", "follower stats", res.Indices) diff --git a/pkg/es/cluster.go b/pkg/es/cluster.go index ac8904b..178af8b 100644 --- a/pkg/es/cluster.go +++ b/pkg/es/cluster.go @@ -44,16 +44,14 @@ func ClusterList(conf *cfg.Config) error { // check endpoints in parallel to speed things up for name, cluster := range conf.Clusters { - wg.Add(1) - go func() { - defer wg.Done() + wg.Go(func() { online, err := cluster.IsReachable() mu.Lock() reachable[name] = clusterReachable{reachable: online, err: err} mu.Unlock() - }() + }) } wg.Wait() diff --git a/pkg/es/cluster_settings.go b/pkg/es/cluster_settings.go index 8941221..0587250 100644 --- a/pkg/es/cluster_settings.go +++ b/pkg/es/cluster_settings.go @@ -35,7 +35,7 @@ func ClusterSettingsList(conf *cfg.Config) error { FlatSettings(true). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get cluster settings: %s", esErrorString(err)) + return fmt.Errorf("failed to get cluster settings: %w", esErrorString(err)) } table := printer.NewTable(conf, 2, 0) @@ -75,7 +75,7 @@ func ClusterSettingsSet(conf *cfg.Config, args cli.Args) error { case conf.Transient: message, err := json.Marshal(value) if err != nil { - return fmt.Errorf("failed to marshall transient value <%v> to valid JSON: %s", value, err) + return fmt.Errorf("failed to marshall transient value <%v> to valid JSON: %w", value, err) } put.AddTransient(setting, message) case conf.Persistent: @@ -83,7 +83,7 @@ func ClusterSettingsSet(conf *cfg.Config, args cli.Args) error { default: message, err := json.Marshal(value) if err != nil { - return fmt.Errorf("failed to marshall persistent value <%v> to valid JSON: %s", value, err) + return fmt.Errorf("failed to marshall persistent value <%v> to valid JSON: %w", value, err) } put.AddPersistent(setting, message) } @@ -91,7 +91,7 @@ func ClusterSettingsSet(conf *cfg.Config, args cli.Args) error { _, err := put.Do(context.Background()) if err != nil { - return fmt.Errorf("failed to set settings: %s", esErrorString(err)) + return fmt.Errorf("failed to set settings: %w", esErrorString(err)) } return nil @@ -100,7 +100,7 @@ func ClusterSettingsSet(conf *cfg.Config, args cli.Args) error { func ClusterSettingsSetSingle(conf *cfg.Config, setting, value string) error { message, err := json.Marshal(value) if err != nil { - return fmt.Errorf("failed to marshall persistent value <%v> to valid JSON: %s", value, err) + return fmt.Errorf("failed to marshall persistent value <%v> to valid JSON: %w", value, err) } _, err = conf.DefaultCluster.ES().Cluster.PutSettings(). @@ -108,7 +108,7 @@ func ClusterSettingsSetSingle(conf *cfg.Config, setting, value string) error { Do(context.Background()) if err != nil { - return fmt.Errorf("failed to set %s: %s", setting, esErrorString(err)) + return fmt.Errorf("failed to set %s: %w", setting, esErrorString(err)) } return nil diff --git a/pkg/es/datastream.go b/pkg/es/datastream.go index 78b1e18..9cd652b 100644 --- a/pkg/es/datastream.go +++ b/pkg/es/datastream.go @@ -32,7 +32,7 @@ func DatastreamNames(conf *cfg.Config) ([]string, error) { res, err := conf.DefaultCluster.ES().Indices.GetDataStream(). Do(context.Background()) if err != nil { - return nil, fmt.Errorf("failed to get data streams: %s", esErrorString(err)) + return nil, fmt.Errorf("failed to get data streams: %w", esErrorString(err)) } dss := make([]string, len(res.DataStreams)) @@ -47,7 +47,7 @@ func DatastreamList(conf *cfg.Config) error { res, err := conf.DefaultCluster.ES().Indices.GetDataStream(). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get data streams: %s", esErrorString(err)) + return fmt.Errorf("failed to get data streams: %w", esErrorString(err)) } slog.Debug("ES result", "data streams", res) @@ -128,7 +128,7 @@ func DatastreamShow(conf *cfg.Config, dsname string) error { Name(dsname). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get data stream: %s", esErrorString(err)) + return fmt.Errorf("failed to get data stream: %w", esErrorString(err)) } slog.Debug("ES result", "data stream", res) @@ -141,7 +141,7 @@ func DatastreamShow(conf *cfg.Config, dsname string) error { Name(dsname). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get data stream stats: %s", esErrorString(err)) + return fmt.Errorf("failed to get data stream stats: %w", esErrorString(err)) } table := printer.NewTable(conf, 11, 2) @@ -208,7 +208,7 @@ func DatastreamCreate(conf *cfg.Config, dsname string) error { Do(context.Background()) if err != nil { - return fmt.Errorf("failed to create datastream: %s", esErrorString(err)) + return fmt.Errorf("failed to create datastream: %w", esErrorString(err)) } return nil @@ -219,7 +219,7 @@ func DatastreamDelete(conf *cfg.Config, dsname string) error { Do(context.Background()) if err != nil { - return fmt.Errorf("failed to delete datastream: %s", esErrorString(err)) + return fmt.Errorf("failed to delete datastream: %w", esErrorString(err)) } return nil @@ -229,7 +229,7 @@ func DatastreamRollover(conf *cfg.Config, ds string) error { res, err := RolloverAlias(conf, ds) if err != nil { - return fmt.Errorf("failed to rollover data stream: %s", esErrorString(err)) + return fmt.Errorf("failed to rollover data stream: %w", esErrorString(err)) } table := printer.NewTable(conf, 2, 5) diff --git a/pkg/es/doc.go b/pkg/es/doc.go index 16109c8..e001f67 100644 --- a/pkg/es/doc.go +++ b/pkg/es/doc.go @@ -42,7 +42,7 @@ func DocAdd(conf *cfg.Config, jsondoc string) error { err := json.Unmarshal([]byte(jsondoc), &data) if err != nil { - return fmt.Errorf("supplied document was not valid JSON: %s", err) + return fmt.Errorf("supplied document was not valid JSON: %w", err) } now := fmt.Sprintf("%d", rand.Int64()) @@ -51,7 +51,7 @@ func DocAdd(conf *cfg.Config, jsondoc string) error { Document(data). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to create new doc in index %s: %s", conf.Index, esErrorString(err)) + return fmt.Errorf("failed to create new doc in index %s: %w", conf.Index, esErrorString(err)) } fmt.Println(res.Id_) @@ -63,7 +63,7 @@ func DocShow(conf *cfg.Config, id string) error { res, err := conf.DefaultCluster.ES().Get(conf.Index, id). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to retrieve doc in index %s: %s", conf.Index, esErrorString(err)) + return fmt.Errorf("failed to retrieve doc in index %s: %w", conf.Index, esErrorString(err)) } if !res.Found { @@ -94,7 +94,7 @@ func DocDelete(conf *cfg.Config, queries []string) error { _, err := conf.DefaultCluster.ES().Delete(conf.Index, id). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to delete doc in index %s: %s", conf.Index, esErrorString(err)) + return fmt.Errorf("failed to delete doc in index %s: %w", conf.Index, esErrorString(err)) } return nil @@ -117,7 +117,7 @@ func DocDelete(conf *cfg.Config, queries []string) error { Request(req). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to delete docs in index %s: %s", conf.Index, esErrorString(err)) + return fmt.Errorf("failed to delete docs in index %s: %w", conf.Index, esErrorString(err)) } return nil diff --git a/pkg/es/errors.go b/pkg/es/errors.go index 7cda3b2..a810493 100644 --- a/pkg/es/errors.go +++ b/pkg/es/errors.go @@ -17,29 +17,33 @@ along with this program. If not, see . package es import ( + "errors" "fmt" + "strings" "github.com/elastic/go-elasticsearch/v9/typedapi/types" ) -func esErrorString(err error) string { +func esErrorString(err error) error { msg := err.Error() switch e := err.(type) { case *types.ElasticsearchError: - causes := "" + var causes strings.Builder for _, cause := range e.ErrorCause.RootCause { - causes += fmt.Sprintf("%s\n", *cause.Reason) + // FIXME: re-activate linter here, see https://github.com/golangci/golangci-lint/issues/6662 + //nolint:staticcheck + causes.WriteString(fmt.Sprintf("%s\n", *cause.Reason)) } if e.ErrorCause.Reason != nil { - msg = *e.ErrorCause.Reason + ": " + causes + msg = *e.ErrorCause.Reason + ": " + causes.String() } else { msg = fmt.Sprintf("http status %d: ", e.Status) } } - return msg + return errors.New(msg) } diff --git a/pkg/es/ilm.go b/pkg/es/ilm.go index e82067d..8a65994 100644 --- a/pkg/es/ilm.go +++ b/pkg/es/ilm.go @@ -37,7 +37,7 @@ func IlmRetry(conf *cfg.Config, index string) error { _, err := conf.DefaultCluster.ES().Ilm.Retry(index). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to retry ilm: %s", esErrorString(err)) + return fmt.Errorf("failed to retry ilm: %w", esErrorString(err)) } return nil @@ -47,7 +47,7 @@ func IlmStatus(conf *cfg.Config) error { res, err := conf.DefaultCluster.ES().Ilm.GetStatus(). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get ilm status: %s", esErrorString(err)) + return fmt.Errorf("failed to get ilm status: %w", esErrorString(err)) } fmt.Println(res.OperationMode.Name) @@ -59,7 +59,7 @@ func IlmNames(conf *cfg.Config) ([]string, error) { res, err := conf.DefaultCluster.ES().Ilm.GetLifecycle(). Do(context.Background()) if err != nil { - return nil, fmt.Errorf("failed to get ilm policies: %s", esErrorString(err)) + return nil, fmt.Errorf("failed to get ilm policies: %w", esErrorString(err)) } names := make([]string, len(res)) @@ -82,7 +82,7 @@ func IlmList(conf *cfg.Config, pattern string) error { res, err := ilm.Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get ilm policies: %s", esErrorString(err)) + return fmt.Errorf("failed to get ilm policies: %w", esErrorString(err)) } if conf.Debug { @@ -110,7 +110,7 @@ func IlmShow(conf *cfg.Config, policy string) error { Policy(policy). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get ilm status: %s", esErrorString(err)) + return fmt.Errorf("failed to get ilm status: %w", esErrorString(err)) } if conf.Debug { @@ -270,7 +270,7 @@ func IlmExplain(conf *cfg.Config, index string) error { res, err := conf.DefaultCluster.ES().Ilm.ExplainLifecycle(index). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get ilm state: %s", esErrorString(err)) + return fmt.Errorf("failed to get ilm state: %w", esErrorString(err)) } slog.Debug("ilm status", "ilm", res) @@ -523,7 +523,7 @@ func IlmCreate(conf *cfg.Config, policyname string) error { _, err = ilm.Do(context.Background()) if err != nil { - return fmt.Errorf("failed create ilm policy: %s", esErrorString(err)) + return fmt.Errorf("failed create ilm policy: %w", esErrorString(err)) } return nil diff --git a/pkg/es/ilm_forecast.go b/pkg/es/ilm_forecast.go index 63d9faa..aaadd2f 100644 --- a/pkg/es/ilm_forecast.go +++ b/pkg/es/ilm_forecast.go @@ -157,11 +157,7 @@ func IlmForecastShow(conf *cfg.Config) error { var toBeFreed int64 = 0 for _, phase := range phaseData { - age := virtualAge(&phase) - - if age < phase.age { - age = phase.age - } + age := max(virtualAge(&phase), phase.age) if age+within >= phase.minage { toBeFreed += phase.size @@ -200,7 +196,7 @@ func getIlmPhaseData(conf *cfg.Config) ([]PhaseData, error) { var indicesres *indices.Response var ilmpolicies getlifecycle.Response - for i := 0; i < 3; i++ { + for range 3 { r := <-responses if r.error != nil { diff --git a/pkg/es/index.go b/pkg/es/index.go index 3f2a170..d1a8f92 100644 --- a/pkg/es/index.go +++ b/pkg/es/index.go @@ -39,7 +39,7 @@ func IndexNames(conf *cfg.Config) ([]string, error) { Do(context.Background()) if err != nil { - return nil, fmt.Errorf("failed to get indicies: %s", esErrorString(err)) + return nil, fmt.Errorf("failed to get indicies: %w", esErrorString(err)) } indices := make([]string, len(res)) @@ -97,7 +97,7 @@ func IndexList(conf *cfg.Config) error { res, err := cat.Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get indicies: %s", esErrorString(err)) + return fmt.Errorf("failed to get indicies: %w", esErrorString(err)) } slog.Debug("ES result", "indicies", res) @@ -133,7 +133,7 @@ func IndexShow(conf *cfg.Config, indexpattern string) error { res, err := conf.DefaultCluster.ES().Indices.Get(indexpattern). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get index: %s", esErrorString(err)) + return fmt.Errorf("failed to get index: %w", esErrorString(err)) } slog.Debug("index show", "index", res) @@ -234,7 +234,7 @@ func IndexCreate(conf *cfg.Config, index string, mappings []string) error { Do(context.Background()) if err != nil { - return fmt.Errorf("failed to create index: %s", esErrorString(err)) + return fmt.Errorf("failed to create index: %w", esErrorString(err)) } return nil @@ -244,7 +244,7 @@ func IndexDelete(conf *cfg.Config, index string) error { _, err := conf.DefaultCluster.ES().Indices.Delete(index). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to delete index: %s", esErrorString(err)) + return fmt.Errorf("failed to delete index: %w", esErrorString(err)) } return nil @@ -255,7 +255,7 @@ func IndexClose(conf *cfg.Config, index string) error { Do(context.Background()) if err != nil { - return fmt.Errorf("failed to close index: %s", esErrorString(err)) + return fmt.Errorf("failed to close index: %w", esErrorString(err)) } return nil @@ -267,7 +267,7 @@ func IndexFields(conf *cfg.Config, index string) error { Fields("*"). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to retrieve field capabilties: %s", esErrorString(err)) + return fmt.Errorf("failed to retrieve field capabilties: %w", esErrorString(err)) } table := printer.NewTable(conf, 5, 0) diff --git a/pkg/es/index_alias.go b/pkg/es/index_alias.go index 0745488..f6622bb 100644 --- a/pkg/es/index_alias.go +++ b/pkg/es/index_alias.go @@ -35,7 +35,7 @@ func IndexAliasCreate(conf *cfg.Config, index, alias string) error { slog.Debug("create alias", "result", res) if err != nil { - return fmt.Errorf("failed to create index alias: %s", esErrorString(err)) + return fmt.Errorf("failed to create index alias: %w", esErrorString(err)) } return nil @@ -54,7 +54,7 @@ func IndexAliasList(conf *cfg.Config) error { Do(context.Background()) if err != nil { - return fmt.Errorf("failed to list index aliases: %s", esErrorString(err)) + return fmt.Errorf("failed to list index aliases: %w", esErrorString(err)) } slog.Debug("aliases list", "result", res) @@ -102,7 +102,7 @@ func IndexAliasDelete(conf *cfg.Config, index, alias string) error { slog.Debug("delete alias", "result", res) if err != nil { - return fmt.Errorf("failed to delete index alias: %s", esErrorString(err)) + return fmt.Errorf("failed to delete index alias: %w", esErrorString(err)) } return nil @@ -112,7 +112,7 @@ func IndexAliasRollover(conf *cfg.Config, alias string) error { res, err := RolloverAlias(conf, alias) if err != nil { - return fmt.Errorf("failed to rollover index alias: %s", esErrorString(err)) + return fmt.Errorf("failed to rollover index alias: %w", esErrorString(err)) } table := printer.NewTable(conf, 2, 5) diff --git a/pkg/es/index_template.go b/pkg/es/index_template.go index 55a546f..030ca88 100644 --- a/pkg/es/index_template.go +++ b/pkg/es/index_template.go @@ -22,6 +22,7 @@ import ( "errors" "fmt" "log/slog" + "maps" "strconv" "strings" @@ -37,7 +38,7 @@ func IndexTemplateList(conf *cfg.Config) error { Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get index templates: %s", esErrorString(err)) + return fmt.Errorf("failed to get index templates: %w", esErrorString(err)) } slog.Debug("res", "index templates", res) @@ -64,7 +65,7 @@ func IndexTemplateShow(conf *cfg.Config, tplname string) error { Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get index template: %s", esErrorString(err)) + return fmt.Errorf("failed to get index template: %w", esErrorString(err)) } slog.Debug("res", "index template", res) @@ -225,7 +226,7 @@ func IndexTemplateCreate(conf *cfg.Config, name string, mappings []string) error _, err := create.Do(context.Background()) if err != nil { - return fmt.Errorf("failed to create index template: %s", esErrorString(err)) + return fmt.Errorf("failed to create index template: %w", esErrorString(err)) } return nil @@ -242,7 +243,7 @@ func IndexTemplateModify(conf *cfg.Config, name string, mappings []string) error Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get index template: %s", esErrorString(err)) + return fmt.Errorf("failed to get index template: %w", esErrorString(err)) } slog.Debug("res", "index template", res) @@ -336,7 +337,7 @@ func IndexTemplateModify(conf *cfg.Config, name string, mappings []string) error _, err = modify.Do(context.Background()) if err != nil { - return fmt.Errorf("failed to modify index template: %s", esErrorString(err)) + return fmt.Errorf("failed to modify index template: %w", esErrorString(err)) } if conf.Rollover { @@ -352,7 +353,7 @@ func IndexTemplateDelete(conf *cfg.Config, name string) error { _, err := conf.DefaultCluster.ES().Indices.DeleteIndexTemplate(name). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to delete index template: %s", esErrorString(err)) + return fmt.Errorf("failed to delete index template: %w", esErrorString(err)) } return nil @@ -367,7 +368,7 @@ func rolloverAliasIndexTemplate(conf *cfg.Config, name string) error { Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get index template: %s", esErrorString(err)) + return fmt.Errorf("failed to get index template: %w", esErrorString(err)) } slog.Debug("res", "index template", res) @@ -384,7 +385,7 @@ func rolloverAliasIndexTemplate(conf *cfg.Config, name string) error { res, err := conf.DefaultCluster.ES().Indices.ResolveIndex(pattern). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to resolve index pattern: %s", esErrorString(err)) + return fmt.Errorf("failed to resolve index pattern: %w", esErrorString(err)) } for _, index := range res.Indices { @@ -448,9 +449,7 @@ func modMappings(mappings []string) (types.TypeMappingVariant, error) { func modMeta(conf *cfg.Config, meta types.Metadata) (map[string]json.RawMessage, error) { metadata := map[string]json.RawMessage{} - for key, value := range meta { - metadata[key] = value - } + maps.Copy(metadata, meta) for _, meta := range conf.Meta { parts := strings.Split(meta, ":") diff --git a/pkg/es/license.go b/pkg/es/license.go index cf74e1f..b9fed8b 100644 --- a/pkg/es/license.go +++ b/pkg/es/license.go @@ -29,7 +29,7 @@ func LicenseShow(conf *cfg.Config) error { res, err := conf.DefaultCluster.ES().License.Get(). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get license: %s", esErrorString(err)) + return fmt.Errorf("failed to get license: %w", esErrorString(err)) } slog.Debug("license show", "license", res) diff --git a/pkg/es/node.go b/pkg/es/node.go index bfa930c..1bbaa5a 100644 --- a/pkg/es/node.go +++ b/pkg/es/node.go @@ -32,7 +32,7 @@ func NodeList(conf *cfg.Config) error { // get nodes nodes, err := conf.DefaultCluster.ES().Cat.Nodes().Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get nodes: %s", esErrorString(err)) + return fmt.Errorf("failed to get nodes: %w", esErrorString(err)) } slog.Debug("ES result", "nodes", nodes) @@ -64,7 +64,7 @@ func NodeNames(conf *cfg.Config) ([]string, error) { nodes, err := conf.DefaultCluster.ES().Cat.Nodes(). Do(context.Background()) if err != nil { - return nil, fmt.Errorf("failed to get nodes: %s", esErrorString(err)) + return nil, fmt.Errorf("failed to get nodes: %w", esErrorString(err)) } slog.Debug("ES result", "nodes", nodes) @@ -86,7 +86,7 @@ func NodeShow(conf *cfg.Config, nodename string) error { Metric("os, jvm, thread_pool, remote_cluster_server"). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get node info: %s", esErrorString(err)) + return fmt.Errorf("failed to get node info: %w", esErrorString(err)) } slog.Debug("ES result", "node", res) @@ -95,7 +95,7 @@ func NodeShow(conf *cfg.Config, nodename string) error { NodeId(nodename). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get node stats: %s", esErrorString(err)) + return fmt.Errorf("failed to get node stats: %w", esErrorString(err)) } slog.Debug("ES result", "stat", stats) @@ -159,7 +159,7 @@ func NodeClients(conf *cfg.Config, nodename string) error { NodeId(nodename). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get node stats: %s", esErrorString(err)) + return fmt.Errorf("failed to get node stats: %w", esErrorString(err)) } slog.Debug("ES result", "stat", stats) diff --git a/pkg/es/parallel.go b/pkg/es/parallel.go index 25a47be..6af0b0c 100644 --- a/pkg/es/parallel.go +++ b/pkg/es/parallel.go @@ -158,7 +158,7 @@ func getApiData( } if arerr != nil { - ar.error = fmt.Errorf("failed to get data from API: %s", arerr) + ar.error = fmt.Errorf("failed to get data from API: %w", arerr) } reschan <- ar diff --git a/pkg/es/role.go b/pkg/es/role.go index 357e1c1..2e190a1 100644 --- a/pkg/es/role.go +++ b/pkg/es/role.go @@ -30,7 +30,7 @@ func RoleNames(conf *cfg.Config) ([]string, error) { res, err := conf.DefaultCluster.ES().Security.GetRole(). Do(context.Background()) if err != nil { - return nil, fmt.Errorf("failed to get roles: %s", esErrorString(err)) + return nil, fmt.Errorf("failed to get roles: %w", esErrorString(err)) } roles := make([]string, len(res)) @@ -47,7 +47,7 @@ func RoleList(conf *cfg.Config) error { res, err := conf.DefaultCluster.ES().Security.GetRole(). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get roles: %s", esErrorString(err)) + return fmt.Errorf("failed to get roles: %w", esErrorString(err)) } slog.Debug("ES result", "roles", res) @@ -74,7 +74,7 @@ func RoleShow(conf *cfg.Config, rolename string) error { Name(rolename). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get role: %s", esErrorString(err)) + return fmt.Errorf("failed to get role: %w", esErrorString(err)) } slog.Debug("ES result", "role", res) diff --git a/pkg/es/role_diff.go b/pkg/es/role_diff.go index 838a5a8..68c4171 100644 --- a/pkg/es/role_diff.go +++ b/pkg/es/role_diff.go @@ -75,7 +75,7 @@ type Register struct { func getCsvRecords(conf *cfg.Config, csvfile string) (map[string]Record, error) { data, err := os.ReadFile(csvfile) if err != nil { - return nil, fmt.Errorf("failed to read CSV file: %s", err) + return nil, fmt.Errorf("failed to read CSV file: %w", err) } csvreader := csv.NewReader(bytes.NewReader(data)) @@ -85,7 +85,7 @@ func getCsvRecords(conf *cfg.Config, csvfile string) (map[string]Record, error) rows, err := csvreader.ReadAll() if err != nil { - return nil, fmt.Errorf("failed to parse CSV: %s", err) + return nil, fmt.Errorf("failed to parse CSV: %w", err) } records := make(map[string]Record, len(rows)-1) @@ -118,7 +118,7 @@ func getCsvRecords(conf *cfg.Config, csvfile string) (map[string]Record, error) func getCsvRecord(conf *cfg.Config, csvfile, rolename string) (*Record, error) { fd, err := os.Open(csvfile) if err != nil { - return nil, fmt.Errorf("failed to open CSV file: %s", err) + return nil, fmt.Errorf("failed to open CSV file: %w", err) } defer func() { if err := fd.Close(); err != nil { @@ -210,7 +210,7 @@ func RoleDiff(conf *cfg.Config, csvfile, role string) error { res, err := conf.DefaultCluster.ES().Security.GetRole(). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get roles: %s", esErrorString(err)) + return fmt.Errorf("failed to get roles: %w", esErrorString(err)) } rows := diffRoles(conf, records, res) @@ -245,7 +245,7 @@ func getRoleMappingGroups(conf *cfg.Config, rolename string) ([]string, error) { mappings, err := conf.DefaultCluster.ES().Security.GetRoleMapping(). Do(context.Background()) if err != nil { - return nil, fmt.Errorf("failed to get role mappings: %s", esErrorString(err)) + return nil, fmt.Errorf("failed to get role mappings: %w", esErrorString(err)) } groups := []string{} @@ -279,7 +279,7 @@ func RoleDiffSingle(conf *cfg.Config, csvfile, rolename string) error { Name(rolename). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get role: %s", esErrorString(err)) + return fmt.Errorf("failed to get role: %w", esErrorString(err)) } record, err := getCsvRecord(conf, csvfile, rolename) diff --git a/pkg/es/search.go b/pkg/es/search.go index 19a2204..f1ffa1a 100644 --- a/pkg/es/search.go +++ b/pkg/es/search.go @@ -82,13 +82,13 @@ func explainSearch(conf *cfg.Config, search *search.Search) error { Size(1). // one's enough for explain Do(context.Background()) if err != nil { - return fmt.Errorf("failed to call explain search (esdsl): %s", esErrorString(err)) + return fmt.Errorf("failed to call explain search (esdsl): %w", esErrorString(err)) } if conf.Debug { raw, err := json.Marshal(res) if err != nil { - return fmt.Errorf("failed to marshal explain result: %s", err) + return fmt.Errorf("failed to marshal explain result: %w", err) } value := gjson.Get(string(raw), "hits.hits.0._explanation") @@ -134,7 +134,7 @@ func validateSearch(conf *cfg.Config, queries []string) error { res, err := validate. Do(context.Background()) if err != nil { - return fmt.Errorf("failed to validate search (esdsl): %s", esErrorString(err)) + return fmt.Errorf("failed to validate search (esdsl): %w", esErrorString(err)) } slog.Debug("ES result", "search", res) @@ -154,7 +154,7 @@ func searchOnce(conf *cfg.Config, search *search.Search) error { Size(conf.To). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to run search (esdsl): %s", esErrorString(err)) + return fmt.Errorf("failed to run search (esdsl): %w", esErrorString(err)) } slog.Debug("ES result", "search", res) @@ -169,7 +169,7 @@ func searchPit(conf *cfg.Config, req *search.Request) error { ctx := context.Background() pit, err := conf.DefaultCluster.ES().OpenPointInTime(conf.Index).KeepAlive("1m").Do(ctx) if err != nil { - return fmt.Errorf("failed to open point-in-time request for search: %s", err) + return fmt.Errorf("failed to open point-in-time request for search: %w", err) } defer func() { _, err := conf.DefaultCluster.ES().ClosePointInTime().Id(pit.Id).Do(ctx) @@ -192,7 +192,7 @@ func searchPit(conf *cfg.Config, req *search.Request) error { for { res, err := search.Do(ctx) if err != nil { - return fmt.Errorf("failed to run search (esdsl pit): %s", esErrorString(err)) + return fmt.Errorf("failed to run search (esdsl pit): %w", esErrorString(err)) } if len(res.Hits.Hits) == 0 { @@ -222,7 +222,7 @@ func searchTail(conf *cfg.Config, search *search.Search) error { for { res, err := search.Do(context.Background()) if err != nil { - return fmt.Errorf("failed to run search (esdsl): %s", esErrorString(err)) + return fmt.Errorf("failed to run search (esdsl): %w", esErrorString(err)) } slog.Debug("ES result", "search", res) diff --git a/pkg/es/shard.go b/pkg/es/shard.go index 9325c13..38e5dcd 100644 --- a/pkg/es/shard.go +++ b/pkg/es/shard.go @@ -86,7 +86,7 @@ func ShardList(conf *cfg.Config) error { res, err := conf.DefaultCluster.ES().Cat.Shards(). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get shards: %s", esErrorString(err)) + return fmt.Errorf("failed to get shards: %w", esErrorString(err)) } shardlist := filterShards(conf, res) @@ -136,7 +136,7 @@ func ShardShow(conf *cfg.Config, index string) error { res, err := conf.DefaultCluster.ES().Cat.Shards().Index(index). Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get shards: %s", esErrorString(err)) + return fmt.Errorf("failed to get shards: %w", esErrorString(err)) } slog.Debug("ES result", "shards", res) @@ -158,7 +158,7 @@ func ShardAllocation(conf *cfg.Config, index string) error { res, err := explain.Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get shard allocation explain: %s", esErrorString(err)) + return fmt.Errorf("failed to get shard allocation explain: %w", esErrorString(err)) } slog.Debug("ES result", "explain", res) diff --git a/pkg/es/snapshot.go b/pkg/es/snapshot.go index b7144b9..789abde 100644 --- a/pkg/es/snapshot.go +++ b/pkg/es/snapshot.go @@ -45,7 +45,7 @@ func SnapshotList(conf *cfg.Config) error { // get partial indicies ires, err := conf.DefaultCluster.ES().Cat.Indices().Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get indicies: %s", esErrorString(err)) + return fmt.Errorf("failed to get indicies: %w", esErrorString(err)) } indicies := map[string]int{} @@ -58,7 +58,7 @@ func SnapshotList(conf *cfg.Config) error { // get snapshots sres, err := conf.DefaultCluster.ES().Cat.Snapshots().Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get snapshots: %s", esErrorString(err)) + return fmt.Errorf("failed to get snapshots: %w", esErrorString(err)) } slog.Debug("ES result", "indicies", sres) @@ -108,7 +108,7 @@ func SnapshotList(conf *cfg.Config) error { func SnapshotShow(conf *cfg.Config, snapshot string) error { res, err := conf.DefaultCluster.ES().Snapshot.Get("*", snapshot).Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get snapshot: %s", esErrorString(err)) + return fmt.Errorf("failed to get snapshot: %w", esErrorString(err)) } slog.Debug("ES result", "snapshot", res) diff --git a/pkg/es/task.go b/pkg/es/task.go index ebba906..1e35909 100644 --- a/pkg/es/task.go +++ b/pkg/es/task.go @@ -32,7 +32,7 @@ func TaskList(conf *cfg.Config) error { Do(context.Background()) if err != nil { - return fmt.Errorf("failed to get index templates: %s", esErrorString(err)) + return fmt.Errorf("failed to get index templates: %w", esErrorString(err)) } slog.Debug("res", "tasks", res) @@ -68,7 +68,7 @@ func TaskCancel(conf *cfg.Config, taskid string) error { Do(context.Background()) if err != nil { - return fmt.Errorf("failed to cancel task: %s", esErrorString(err)) + return fmt.Errorf("failed to cancel task: %w", esErrorString(err)) } return nil diff --git a/pkg/printer/pager.go b/pkg/printer/pager.go index ee8e6a3..037cc3d 100644 --- a/pkg/printer/pager.go +++ b/pkg/printer/pager.go @@ -115,13 +115,6 @@ func (m model) footerView() string { return lipgloss.JoinHorizontal(lipgloss.Center, line, info) } -func max(a, b int) int { - if a > b { - return a - } - return b -} - func Pager(title, message string) { p := tea.NewProgram( model{content: message, title: title}, diff --git a/pkg/printer/table.go b/pkg/printer/table.go index 26020a2..fe59672 100644 --- a/pkg/printer/table.go +++ b/pkg/printer/table.go @@ -104,7 +104,7 @@ func (data *Table) PrintYAML() error { body, err := yaml.Marshal(raw) if err != nil { - return fmt.Errorf("failed to produce YAML output: %s", err) + return fmt.Errorf("failed to produce YAML output: %w", err) } fmt.Println(string(body)) @@ -117,7 +117,7 @@ func (data *Table) PrintJSON() error { body, err := json.MarshalIndent(raw, "", " ") if err != nil { - return fmt.Errorf("failed to produce JSON output: %s", err) + return fmt.Errorf("failed to produce JSON output: %w", err) } fmt.Println(string(body))