Skip to content

Commit

Permalink
chore: code cleanup
Browse files Browse the repository at this point in the history
More usage of slices package, less usage of package sort.

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Nov 14, 2024
1 parent 43fe380 commit e26d004
Show file tree
Hide file tree
Showing 62 changed files with 189 additions and 204 deletions.
2 changes: 1 addition & 1 deletion cmd/talosctl/cmd/mgmt/cluster/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ func create(ctx context.Context) error {
types := []machine.Type{machine.TypeControlPlane, machine.TypeWorker}

if withInitNode {
types = append([]machine.Type{machine.TypeInit}, types...)
types = slices.Insert(types, 0, machine.TypeInit)
}

if err = configBundle.Write(".", encoder.CommentsAll, types...); err != nil {
Expand Down
5 changes: 3 additions & 2 deletions cmd/talosctl/cmd/mgmt/cluster/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
package cluster

import (
"cmp"
"context"
"fmt"
"net/netip"
"os"
"sort"
"slices"
"strings"
"text/tabwriter"

Expand Down Expand Up @@ -76,7 +77,7 @@ func showCluster(cluster provision.Cluster) error {
fmt.Fprintf(w, "NAME\tTYPE\tIP\tCPU\tRAM\tDISK\n")

nodes := cluster.Info().Nodes
sort.Slice(nodes, func(i, j int) bool { return nodes[i].Name < nodes[j].Name })
slices.SortFunc(nodes, func(a, b provision.NodeInfo) int { return cmp.Compare(a.Name, b.Name) })

for _, node := range nodes {
cpus := "-"
Expand Down
8 changes: 4 additions & 4 deletions cmd/talosctl/cmd/talos/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"errors"
"fmt"
"os"
"sort"
"slices"
"strings"
"text/tabwriter"
"text/template"
Expand Down Expand Up @@ -279,7 +279,7 @@ var configRemoveCmd = &cobra.Command{
}

func sortInPlace(slc []string) []string {
sort.Slice(slc, func(i, j int) bool { return slc[i] < slc[j] })
slices.Sort(slc)

return slc
}
Expand Down Expand Up @@ -326,7 +326,7 @@ var configGetContextsCmd = &cobra.Command{
}

keys := maps.Keys(c.Contexts)
sort.Strings(keys)
slices.Sort(keys)

w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
fmt.Fprintln(w, "CURRENT\tNAME\tENDPOINTS\tNODES")
Expand Down Expand Up @@ -587,7 +587,7 @@ func CompleteConfigContext(*cobra.Command, []string, string) ([]string, cobra.Sh
}

contextnames := maps.Keys(c.Contexts)
sort.Strings(contextnames)
slices.Sort(contextnames)

return contextnames, cobra.ShellCompDirectiveNoFileComp
}
Expand Down
7 changes: 2 additions & 5 deletions cmd/talosctl/cmd/talos/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"context"
"fmt"
"os"
"sort"
"slices"
"strings"
"text/tabwriter"

Expand Down Expand Up @@ -68,10 +68,7 @@ func containerRender(remotePeer *peer.Peer, resp *machineapi.ContainersResponse)
defaultNode := client.AddrFromPeer(remotePeer)

for _, msg := range resp.Messages {
sort.Slice(msg.Containers,
func(i, j int) bool {
return strings.Compare(msg.Containers[i].Id, msg.Containers[j].Id) < 0
})
slices.SortFunc(msg.Containers, func(a, b *machineapi.ContainerInfo) int { return strings.Compare(a.Id, b.Id) })

for _, p := range msg.Containers {
display := p.Id
Expand Down
3 changes: 2 additions & 1 deletion cmd/talosctl/cmd/talos/diskusage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"os"
"slices"
"strconv"
"text/tabwriter"

Expand Down Expand Up @@ -109,7 +110,7 @@ var duCmd = &cobra.Command{

if multipleNodes {
pattern = "%s\t%s\t%s\n"
args = append([]any{node}, args...)
args = slices.Insert(args, 0, any(node))
}

fmt.Fprintf(w, pattern, args...)
Expand Down
7 changes: 4 additions & 3 deletions cmd/talosctl/cmd/talos/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io"
"os"
"slices"
"strings"
"text/tabwriter"

Expand Down Expand Up @@ -72,7 +73,7 @@ func displayAlarms(messages []alarmMessage) error {
alarm.GetAlarm().String(),
}
if node != "" {
args = append([]any{node}, args...)
args = slices.Insert(args, 0, any(node))
}

fmt.Fprintf(w, pattern, args...)
Expand Down Expand Up @@ -241,7 +242,7 @@ var etcdMemberListCmd = &cobra.Command{
member.IsLearner,
}
if node != "" {
args = append([]any{node}, args...)
args = slices.Insert(args, 0, any(node))
}

fmt.Fprintf(w, pattern, args...)
Expand Down Expand Up @@ -305,7 +306,7 @@ var etcdStatusCmd = &cobra.Command{
strings.Join(message.GetMemberStatus().GetErrors(), ", "),
}
if node != "" {
args = append([]any{node}, args...)
args = slices.Insert(args, 0, any(node))
}

fmt.Fprintf(w, pattern, args...)
Expand Down
7 changes: 2 additions & 5 deletions cmd/talosctl/cmd/talos/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"os"
"slices"
"time"

"github.com/cosi-project/runtime/pkg/safe"
Expand Down Expand Up @@ -62,11 +63,7 @@ func (cl *clusterNodes) InitNodeInfos() error {
nodesByType[machine.TypeWorker] = workerNodeInfos
cl.nodesByType = nodesByType

nodes := make([]cluster.NodeInfo, 0, len(initNodeInfos)+len(controlPlaneNodeInfos)+len(workerNodeInfos))
nodes = append(nodes, initNodeInfos...)
nodes = append(nodes, controlPlaneNodeInfos...)
nodes = append(nodes, workerNodeInfos...)
cl.nodes = nodes
cl.nodes = slices.Concat(initNodeInfos, controlPlaneNodeInfos, workerNodeInfos)

return nil
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/talosctl/cmd/talos/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"context"
"errors"
"fmt"
"sort"
"slices"
"strings"

"github.com/siderolabs/gen/maps"
Expand Down Expand Up @@ -63,7 +63,7 @@ func (m *WipeMode) Set(value string) error {
// Type implements Flag interface.
func (m *WipeMode) Type() string {
options := maps.Keys(wipeOptions)
sort.Strings(options)
slices.Sort(options)

return strings.Join(options, ", ")
}
Expand Down
7 changes: 2 additions & 5 deletions cmd/talosctl/cmd/talos/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"context"
"fmt"
"os"
"sort"
"slices"
"strings"
"text/tabwriter"

Expand Down Expand Up @@ -68,10 +68,7 @@ func statsRender(remotePeer *peer.Peer, resp *machineapi.StatsResponse) error {
defaultNode := client.AddrFromPeer(remotePeer)

for _, msg := range resp.Messages {
sort.Slice(msg.Stats,
func(i, j int) bool {
return strings.Compare(msg.Stats[i].Id, msg.Stats[j].Id) < 0
})
slices.SortFunc(msg.Stats, func(a, b *machineapi.Stat) int { return strings.Compare(a.Id, b.Id) })

for _, s := range msg.Stats {
display := s.Id
Expand Down
7 changes: 4 additions & 3 deletions cmd/talosctl/cmd/talos/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
package talos

import (
"cmp"
"context"
"errors"
"fmt"
"os"
"sort"
"slices"
"strings"
"text/tabwriter"
"time"
Expand Down Expand Up @@ -146,8 +147,8 @@ func upgradeGetActorID(ctx context.Context, c *client.Client, opts []client.Upgr

func init() {
rebootModes := maps.Keys(machine.UpgradeRequest_RebootMode_value)
sort.Slice(rebootModes, func(i, j int) bool {
return machine.UpgradeRequest_RebootMode_value[rebootModes[i]] < machine.UpgradeRequest_RebootMode_value[rebootModes[j]]
slices.SortFunc(rebootModes, func(a, b string) int {
return cmp.Compare(machine.UpgradeRequest_RebootMode_value[a], machine.UpgradeRequest_RebootMode_value[b])
})

rebootModes = xslices.Map(rebootModes, strings.ToLower)
Expand Down
4 changes: 2 additions & 2 deletions cmd/talosctl/pkg/talos/helpers/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package helpers
import (
"fmt"
"os"
"sort"
"slices"
"strings"

"github.com/siderolabs/gen/maps"
Expand Down Expand Up @@ -61,7 +61,7 @@ func (m *Mode) Set(value string) error {
// Type implements Flag interface.
func (m *Mode) Type() string {
options := maps.Keys(m.options)
sort.Strings(options)
slices.Sort(options)

return strings.Join(options, ", ")
}
Expand Down
14 changes: 7 additions & 7 deletions hack/module-sig-verify/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,20 @@ func main() {
}
}

type noOPCloser struct {
io.ReadSeeker
}

func (noOPCloser) Close() error { return nil }

func parseModuleInput(module string) (io.ReadSeekCloser, error) {
if module == "-" {
moduleData, err := io.ReadAll(os.Stdin)
if err != nil {
return nil, fmt.Errorf("failed to read module from stdin: %w", err)
}

return noOPCloser{bytes.NewReader(moduleData)}, nil
return struct {
io.ReadSeeker
io.Closer
}{
bytes.NewReader(moduleData),
io.NopCloser(nil),
}, nil
}

moduleData, err := os.Open(module)
Expand Down
4 changes: 2 additions & 2 deletions internal/app/apid/pkg/backend/apid.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"crypto/tls"
"fmt"
"slices"
"sync"
"time"

Expand Down Expand Up @@ -212,9 +213,8 @@ func (a *APID) AppendInfo(streaming bool, resp []byte) ([]byte, error) {
protowire.AppendVarint(nil, (metadataField<<3)|metadataType),
uint64(len(resp)+len(payload)),
)
resp = append(prefix, resp...)

return append(resp, payload...), err
return slices.Concat(prefix, resp, payload), err
}

// BuildError is called to convert error from upstream into response field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func buildClusterInfo(ctx context.Context,
}

return &clusterState{
nodeInfos: append(slices.Clone(controlPlaneNodeInfos), workerNodeInfos...),
nodeInfos: slices.Concat(controlPlaneNodeInfos, workerNodeInfos),
nodeInfosByType: map[machine.Type][]cluster.NodeInfo{
machine.TypeControlPlane: controlPlaneNodeInfos,
machine.TypeWorker: workerNodeInfos,
Expand Down
28 changes: 4 additions & 24 deletions internal/app/machined/pkg/adapters/network/nftables_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,36 +676,16 @@ func (a nftablesRule) Compile() (*NfTablesCompiled, error) {
result.Rules = [][]expr.Any{append(rulePre, rulePost...)}
case rule4 != nil && rule6 == nil:
result.Rules = [][]expr.Any{
append(rulePre,
append(
append(matchV4, rule4...),
rulePost...,
)...,
),
slices.Concat(rulePre, matchV4, rule4, rulePost),
}
case rule4 == nil && rule6 != nil:
result.Rules = [][]expr.Any{
append(rulePre,
append(
append(matchV6, rule6...),
rulePost...,
)...,
),
slices.Concat(rulePre, matchV6, rule6, rulePost),
}
case rule4 != nil && rule6 != nil:
result.Rules = [][]expr.Any{
append(slices.Clone(rulePre),
append(
append(matchV4, rule4...),
rulePost...,
)...,
),
append(slices.Clone(rulePre),
append(
append(matchV6, rule6...),
rulePost...,
)...,
),
slices.Concat(rulePre, matchV4, rule4, rulePost),
slices.Concat(rulePre, matchV6, rule6, rulePost),
}
}

Expand Down
3 changes: 1 addition & 2 deletions internal/app/machined/pkg/controllers/cluster/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"net/netip"
"slices"
"sort"

"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/safe"
Expand Down Expand Up @@ -75,7 +74,7 @@ func (ctrl *EndpointController) Run(ctx context.Context, r controller.Runtime, l
endpoints = append(endpoints, memberSpec.Addresses...)
}

sort.Slice(endpoints, func(i, j int) bool { return endpoints[i].Compare(endpoints[j]) < 0 })
slices.SortFunc(endpoints, func(a, b netip.Addr) int { return a.Compare(b) })

if err := safe.WriterModify(
ctx,
Expand Down
4 changes: 2 additions & 2 deletions internal/app/machined/pkg/controllers/ctest/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package ctest

import (
"fmt"
"sort"
"slices"
"strings"

"github.com/siderolabs/go-retry/retry"
Expand Down Expand Up @@ -46,7 +46,7 @@ func (agg *assertionAggregator) Error() error {
lines = append(lines, " * "+errorString)
}

sort.Strings(lines)
slices.Sort(lines)

return fmt.Errorf("%s", strings.Join(lines, "\n"))
}
Expand Down
Loading

0 comments on commit e26d004

Please sign in to comment.