adopt latest golang enhancements (#91)

This commit is contained in:
T. von Dein
2026-07-13 14:33:41 +02:00
parent c5a21bd677
commit 97a509da9e
19 changed files with 159 additions and 114 deletions

View File

@@ -28,6 +28,7 @@ import (
"io"
"log"
"log/slog"
"maps"
"net/http"
"os"
"os/exec"
@@ -158,7 +159,7 @@ func ApiRepl(conf *cfg.Config) error {
}
func pageJsonOutput(conf *cfg.Config, raw []byte) {
tmpconf := &cfg.Config{HaveJQ: conf.HaveJQ}
tmpconf := new(cfg.Config{HaveJQ: conf.HaveJQ})
if conf.Pager != "" {
tmpconf.HaveJQ = false
@@ -206,16 +207,16 @@ func CallAPI(conf *cfg.Config, verb, path, data string) ([]byte, error) {
verb = strings.ToUpper(verb)
// we're using port-forwards anyway
noVerifyTransport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
noVerifyTransport := new(http.Transport{
TLSClientConfig: new(tls.Config{InsecureSkipVerify: true}),
})
client := &http.Client{Transport: noVerifyTransport}
client := new(http.Client{Transport: noVerifyTransport})
if conf.DebugHTTP {
client = &http.Client{
Transport: &cfg.DebugTransport{
Transport: noVerifyTransport}}
client = new(http.Client{
Transport: new(cfg.DebugTransport{
Transport: noVerifyTransport})})
}
req, err := http.NewRequest(verb, conf.DefaultCluster.Uri+path, bytes.NewBuffer([]byte(data)))
@@ -366,16 +367,7 @@ func ApiList(conf *cfg.Config, pattern string) error {
func ApiPathNames() []string {
assets.LoadAssetOpenApi()
paths := make([]string, len(assets.OpenAPI.Spec().Paths.Paths))
idx := 0
for path := range assets.OpenAPI.Spec().Paths.Paths {
paths[idx] = path
idx++
}
return paths
return slices.Collect(maps.Keys(assets.OpenAPI.Spec().Paths.Paths))
}
func ApiShow(conf *cfg.Config, showpath, verb string) error {
@@ -536,7 +528,7 @@ func getApiExample(op *Op) string {
// otherwise showpath+verb have to match precisely.
func matchOperation(showpath, verb string) (*Op, error) {
ops := []*Op{}
op := &Op{}
op := new(Op{})
var found bool

View File

@@ -17,6 +17,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package es
import (
// "encoding/json/jsontext"
// "encoding/json/v2"
"encoding/json"
"fmt"
@@ -49,11 +53,22 @@ func getHealthReport(conf *cfg.Config) (*HealthReport, error) {
return nil, err
}
report := HealthReport{}
report := new(HealthReport{})
// FIXME: use this once jsonv2 is no more experimental it already
// builds and works like intended, but golangci-lint doesn't
// recognize it with: go: unknown GOEXPERIMENT jsonv2
//
// if err := json.UnmarshalDecode(
// jsontext.NewDecoder(
// bytes.NewBuffer(raw)),
// &report); err != nil {
// return nil, fmt.Errorf("failed to unmarshal healthreport response: %w", err)
// }
if err := json.Unmarshal(raw, &report); err != nil {
return nil, fmt.Errorf("failed to unmarshal healthreport response: %w", err)
}
return &report, nil
return report, nil
}

View File

@@ -101,23 +101,39 @@ func getClusterStatus(conf *cfg.Config) (*apiResponse, error) {
es := conf.DefaultCluster.ES()
responses := make(chan apiResponse, gocount)
wg := &sync.WaitGroup{}
wg := new(sync.WaitGroup{})
wg.Add(gocount)
go getApiData(conf, es, wg, responses, "health")
go getApiData(conf, es, wg, responses, "healthreport")
go getApiData(conf, es, wg, responses, "info")
go getApiData(conf, es, wg, responses, "ccr")
go getApiData(conf, es, wg, responses, "indices")
go getApiData(conf, es, wg, responses, "tasks")
wg.Go(func() {
getApiData(conf, es, responses, "health")
})
wg.Go(func() {
getApiData(conf, es, responses, "healthreport")
})
wg.Go(func() {
getApiData(conf, es, responses, "info")
})
wg.Go(func() {
getApiData(conf, es, responses, "ccr")
})
wg.Go(func() {
getApiData(conf, es, responses, "indices")
})
wg.Go(func() {
getApiData(conf, es, responses, "tasks")
})
if conf.Verbose {
go getApiData(conf, es, wg, responses, "stats")
getApiData(conf, es, responses, "stats")
}
wg.Wait()
all := apiResponse{}
all := new(apiResponse{})
var err error
@@ -144,7 +160,7 @@ func getClusterStatus(conf *cfg.Config) (*apiResponse, error) {
}
}
return &all, err
return all, err
}
func ClusterStatus(conf *cfg.Config) error {

View File

@@ -29,12 +29,12 @@ func ClusterRerouteMove(conf *cfg.Config, index string) error {
move := conf.DefaultCluster.ES().Cluster.Reroute()
commands := esdsl.NewCommand()
moveCommand := &types.CommandMoveAction{
moveCommand := new(types.CommandMoveAction{
Shard: conf.Shards,
FromNode: conf.FromNode,
ToNode: conf.ToNode,
Index: index,
}
})
commands.CommandCaster().Move = moveCommand
@@ -52,11 +52,11 @@ func ClusterRerouteAllocateReplica(conf *cfg.Config, index string) error {
move := conf.DefaultCluster.ES().Cluster.Reroute()
commands := esdsl.NewCommand()
allocCommand := &types.CommandAllocateReplicaAction{
allocCommand := new(types.CommandAllocateReplicaAction{
Shard: conf.Shards,
Node: conf.ToNode,
Index: index,
}
})
commands.CommandCaster().AllocateReplica = allocCommand
@@ -74,12 +74,12 @@ func ClusterRerouteCancel(conf *cfg.Config, index string) error {
move := conf.DefaultCluster.ES().Cluster.Reroute()
commands := esdsl.NewCommand()
cancelCommand := &types.CommandCancelAction{
cancelCommand := new(types.CommandCancelAction{
Shard: conf.Shards,
Node: conf.ToNode,
Index: index,
AllowPrimary: &conf.AllowPrimary,
}
})
commands.CommandCaster().Cancel = cancelCommand
@@ -97,12 +97,12 @@ func ClusterRerouteAllocatePrimary(conf *cfg.Config, index string, stale bool) e
move := conf.DefaultCluster.ES().Cluster.Reroute()
commands := esdsl.NewCommand()
allocCommand := &types.CommandAllocatePrimaryAction{
allocCommand := new(types.CommandAllocatePrimaryAction{
Shard: conf.Shards,
Node: conf.ToNode,
Index: index,
AcceptDataLoss: conf.AcceptDataLoss,
}
})
if stale {
commands.CommandCaster().AllocateStalePrimary = allocCommand

View File

@@ -101,7 +101,7 @@ func DocDelete(conf *cfg.Config, queries []string) error {
return nil
}
req := &deletebyquery.Request{}
req := new(deletebyquery.Request{})
if len(queries) == 0 && conf.All {
req.Query = esdsl.NewMatchAllQuery().QueryCaster()

View File

@@ -22,6 +22,8 @@ import (
"errors"
"fmt"
"log/slog"
"maps"
"slices"
"strings"
"codeberg.org/scip/esctl/pkg/cfg"
@@ -62,13 +64,7 @@ func IlmNames(conf *cfg.Config) ([]string, error) {
return nil, fmt.Errorf("failed to get ilm policies: %w", esErrorString(err))
}
names := make([]string, len(res))
idx := 0
for name := range res {
names[idx] = name
idx++
}
names := slices.Collect(maps.Keys(res))
return names, nil
}
@@ -351,7 +347,7 @@ func IlmCreate(conf *cfg.Config, policyname string) error {
var actions types.IlmActionsVariant = esdsl.NewIlmActions()
rollover := &types.RolloverAction{}
rollover := new(types.RolloverAction{})
haveroll := false
if policy != nil {
@@ -513,8 +509,8 @@ func IlmCreate(conf *cfg.Config, policyname string) error {
phases.PhasesCaster().Delete = policy.Phases.Delete
}
put := &putlifecycle.Request{}
newpolicy := &types.IlmPolicy{}
put := new(putlifecycle.Request{})
newpolicy := new(types.IlmPolicy{})
newpolicy.IlmPolicyCaster().Phases = *phases.PhasesCaster()
put.Policy = newpolicy

View File

@@ -185,12 +185,19 @@ func virtualAge(phase *PhaseData) time.Duration {
// Retrieve all index, ilm-explain and ilm-policies in parallel
func getIlmPhaseData(conf *cfg.Config) ([]PhaseData, error) {
responses := make(chan apiResponse, 3)
wg := &sync.WaitGroup{}
wg.Add(3)
wg := new(sync.WaitGroup{})
go getApiData(conf, conf.DefaultCluster.ES(), wg, responses, "indicesbytes")
go getApiData(conf, conf.DefaultCluster.ES(), wg, responses, "explain")
go getApiData(conf, conf.DefaultCluster.ES(), wg, responses, "policies")
wg.Go(func() {
getApiData(conf, conf.DefaultCluster.ES(), responses, "indicesbytes")
})
wg.Go(func() {
getApiData(conf, conf.DefaultCluster.ES(), responses, "explain")
})
wg.Go(func() {
getApiData(conf, conf.DefaultCluster.ES(), responses, "policies")
})
wg.Wait()
@@ -331,7 +338,7 @@ func findNextPhase(policy types.IlmPolicy, currentPhase string) *NextPhase {
// phase list to determine which comes next
phases, start := registerPhases(policy, currentPhase)
nextPhase := &NextPhase{}
nextPhase := new(NextPhase{})
// finally determine which phase comes next
// exception: hot, where we look for rollover rules

View File

@@ -19,7 +19,6 @@ package es
import (
"context"
"fmt"
"sync"
"codeberg.org/scip/esctl/pkg/cfg"
"github.com/elastic/go-elasticsearch/v9"
@@ -61,14 +60,7 @@ type apiResponse struct {
which int
}
func getApiData(
conf *cfg.Config,
es *elasticsearch.TypedClient,
wg *sync.WaitGroup,
reschan chan apiResponse,
which string) {
defer wg.Done()
func getApiData(conf *cfg.Config, es *elasticsearch.TypedClient, reschan chan apiResponse, which string) {
apiRes := apiResponse{}
var arerr error

View File

@@ -20,6 +20,8 @@ import (
"context"
"fmt"
"log/slog"
"maps"
"slices"
"codeberg.org/scip/esctl/pkg/cfg"
"codeberg.org/scip/esctl/pkg/printer"
@@ -33,15 +35,7 @@ func RoleNames(conf *cfg.Config) ([]string, error) {
return nil, fmt.Errorf("failed to get roles: %w", esErrorString(err))
}
roles := make([]string, len(res))
idx := 0
for name := range res {
roles[idx] = name
idx++
}
return roles, nil
return slices.Collect(maps.Keys(res)), nil
}
func RoleList(conf *cfg.Config) error {

View File

@@ -127,7 +127,7 @@ func getCsvRecord(conf *cfg.Config, csvfile, rolename string) (*Record, error) {
}()
scanner := bufio.NewScanner(fd)
record := Record{role: rolename}
record := new(Record{role: rolename})
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
@@ -150,7 +150,7 @@ func getCsvRecord(conf *cfg.Config, csvfile, rolename string) (*Record, error) {
}
}
return &record, nil
return record, nil
}
func diffRoles(conf *cfg.Config, records map[string]Record, res getrole.Response) []Register {

View File

@@ -56,7 +56,7 @@ func Search(conf *cfg.Config, queries []string) error {
return err
}
req := &search.Request{Query: queryCaster}
req := new(search.Request{Query: queryCaster})
searchEs.Request(req)
@@ -128,7 +128,7 @@ func validateSearch(conf *cfg.Config, queries []string) error {
return err
}
req := &validatequery.Request{Query: queryCaster}
req := new(validatequery.Request{Query: queryCaster})
validate.Request(req)

View File

@@ -80,7 +80,7 @@ func NewFilter(query string) (*filter, error) {
return nil, errors.New("search queries must be in the form field<sep>pattern where <sep> must be one of: = or !=")
}
flt := &filter{term: part[0], filter: part[1], criteria: criteria}
flt := new(filter{term: part[0], filter: part[1], criteria: criteria})
if strings.Contains(part[0], ",") {
// a MultiMatchQuery, match across multiple fields at once

View File

@@ -66,13 +66,13 @@ func SnapshotList(conf *cfg.Config) error {
snapshots := []*Snapshot{} // original snapshot names
for _, snapshot := range sres {
snap := &Snapshot{
snap := new(Snapshot{
Name: *snapshot.Id,
Status: *snapshot.Status,
Start: fmt.Sprintf("%s", snapshot.StartTime),
Forindex: indexFromSnapshot(*snapshot.Id),
Orphaned: "no",
}
})
_, exists := indicies[snap.Forindex]
if !exists {