Skip to content

Commit ccef81d

Browse files
shota3506Joibel
authored andcommitted
refactor: modernize go code with stdlib improvements (argoproj#14946)
Signed-off-by: shota3506 <s.shota.710.3506@gmail.com> Co-authored-by: Alan Clucas <alan@clucas.org>
1 parent 04e990d commit ccef81d

File tree

28 files changed

+85
-145
lines changed

28 files changed

+85
-145
lines changed

cmd/argo/commands/executorplugin/io.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func loadPluginManifest(pluginDir string) (*spec.Plugin, error) {
4141
}
4242

4343
func addHeader(x []byte, h string) []byte {
44-
return []byte(fmt.Sprintf("%s\n%s", h, string(x)))
44+
return fmt.Appendf(nil, "%s\n%s", h, string(x))
4545
}
4646

4747
func addCodegenHeader(x []byte) []byte {

cmd/argoexec/commands/emissary_windows_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import (
66
"os"
77
"testing"
88

9-
cmdutil "github.com/argoproj/argo-workflows/v3/util/cmd"
10-
"github.com/argoproj/argo-workflows/v3/util/errors"
119
"github.com/stretchr/testify/assert"
1210
"github.com/stretchr/testify/require"
11+
12+
cmdutil "github.com/argoproj/argo-workflows/v3/util/cmd"
13+
"github.com/argoproj/argo-workflows/v3/util/errors"
1314
)
1415

1516
func TestEmissary(t *testing.T) {

hack/docs/configdoc.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,19 +274,19 @@ func createTypeLinkWithSpacing(baseType string) (string, bool) {
274274
return fmt.Sprintf("[`%s`](#%s)", cleanBaseType, strings.ToLower(baseType)), true
275275
}
276276

277-
if strings.HasPrefix(baseType, "wfv1.") {
278-
wfType := strings.TrimPrefix(baseType, "wfv1.")
277+
if after, ok := strings.CutPrefix(baseType, "wfv1."); ok {
278+
wfType := after
279279
return fmt.Sprintf("[`%s`](fields.md#%s)", wfType, strings.ToLower(wfType)), true
280280
}
281281

282-
if strings.HasPrefix(baseType, "apiv1.") {
283-
typeName := strings.TrimPrefix(baseType, "apiv1.")
282+
if after, ok := strings.CutPrefix(baseType, "apiv1."); ok {
283+
typeName := after
284284
anchor := strings.ToLower(typeName)
285285
return fmt.Sprintf("[`%s`](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.32/#%s-v1-core)", typeName, anchor), true
286286
}
287287

288-
if strings.HasPrefix(baseType, "metav1.") {
289-
typeName := strings.TrimPrefix(baseType, "metav1.")
288+
if after, ok := strings.CutPrefix(baseType, "metav1."); ok {
289+
typeName := after
290290
anchor := strings.ToLower(typeName)
291291
return fmt.Sprintf("[`%s`](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.32/#%s-v1-meta)", typeName, anchor), true
292292
}

hack/featuregen/contents.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func format(version string, features []feature) string {
198198
output.WriteString(fmt.Sprintf("- %s by %s %s\n", feature.Description, feature.Author, issuesStr))
199199

200200
if feature.Details != "" {
201-
for _, line := range strings.Split(feature.Details, "\n") {
201+
for line := range strings.SplitSeq(feature.Details, "\n") {
202202
if line != "" {
203203
output.WriteString(fmt.Sprintf(" %s\n", line))
204204
}

server/utils/list_options.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ func BuildListOptions(options metav1.ListOptions, ns, namePrefix, nameFilter, cr
9292
}
9393
}
9494
showRemainingItemCount := false
95-
for _, selector := range strings.Split(options.FieldSelector, ",") {
95+
for selector := range strings.SplitSeq(options.FieldSelector, ",") {
9696
if len(selector) == 0 {
9797
continue
9898
}
99-
if strings.HasPrefix(selector, "metadata.namespace=") {
99+
if after, ok := strings.CutPrefix(selector, "metadata.namespace="); ok {
100100
// for backward compatibility, the field selector 'metadata.namespace' is supported for now despite the addition
101101
// of the new 'namespace' query parameter, which is what the UI uses
102-
fieldSelectedNamespace := strings.TrimPrefix(selector, "metadata.namespace=")
102+
fieldSelectedNamespace := after
103103
switch namespace {
104104
case "":
105105
namespace = fieldSelectedNamespace
@@ -109,16 +109,16 @@ func BuildListOptions(options metav1.ListOptions, ns, namePrefix, nameFilter, cr
109109
return ListOptions{}, status.Errorf(codes.InvalidArgument,
110110
"'namespace' query param (%q) and fieldselector 'metadata.namespace' (%q) are both specified and contradict each other", namespace, fieldSelectedNamespace)
111111
}
112-
} else if strings.HasPrefix(selector, "metadata.name=") {
113-
name = strings.TrimPrefix(selector, "metadata.name=")
114-
} else if strings.HasPrefix(selector, "spec.startedAt>") {
115-
minStartedAt, err = time.Parse(time.RFC3339, strings.TrimPrefix(selector, "spec.startedAt>"))
112+
} else if after, ok := strings.CutPrefix(selector, "metadata.name="); ok {
113+
name = after
114+
} else if after, ok := strings.CutPrefix(selector, "spec.startedAt>"); ok {
115+
minStartedAt, err = time.Parse(time.RFC3339, after)
116116
if err != nil {
117117
// startedAt is populated by us, it should therefore be valid.
118118
return ListOptions{}, ToStatusError(err, codes.Internal)
119119
}
120-
} else if strings.HasPrefix(selector, "spec.startedAt<") {
121-
maxStartedAt, err = time.Parse(time.RFC3339, strings.TrimPrefix(selector, "spec.startedAt<"))
120+
} else if after, ok := strings.CutPrefix(selector, "spec.startedAt<"); ok {
121+
maxStartedAt, err = time.Parse(time.RFC3339, after)
122122
if err != nil {
123123
// no need to use sutils here
124124
return ListOptions{}, ToStatusError(err, codes.Internal)

server/workflow/workflow_server.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,7 @@ func (s *workflowServer) ListWorkflows(ctx context.Context, req *workflowpkg.Wor
250250
var remainCount int64
251251
if options.ShowRemainingItemCount {
252252
// Calculate exact remaining count when requested
253-
remainCount = totalCount - int64(options.Offset) - int64(len(wfs))
254-
if remainCount < 0 {
255-
remainCount = 0
256-
}
253+
remainCount = max(totalCount-int64(options.Offset)-int64(len(wfs)), 0)
257254
meta.RemainingItemCount = &remainCount
258255
} else {
259256
// For pagination without remaining count, use the efficient HasMoreWorkflows method

test/e2e/fixtures/given.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ func (g *Given) WorkflowWorkflow(wf *wfv1.Workflow) *Given {
7171
func (g *Given) readResource(text string, v metav1.Object) {
7272
g.t.Helper()
7373
var file string
74-
if strings.HasPrefix(text, "@") {
75-
file = strings.TrimPrefix(text, "@")
74+
if after, ok := strings.CutPrefix(text, "@"); ok {
75+
file = after
7676
} else {
7777
f, err := os.CreateTemp("", "argo_e2e")
7878
if err != nil {

test/e2e/fixtures/metrics.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ func (mb *MetricBaseline) ExpectIncrease() {
9595
func parseMetricValue(body, metricPattern string) float64 {
9696
// Escape special regex characters in the metric pattern, but keep the spaces
9797
// We'll look for lines that match the pattern and extract the value
98-
lines := strings.Split(body, "\n")
98+
lines := strings.SplitSeq(body, "\n")
9999

100-
for _, line := range lines {
100+
for line := range lines {
101101
line = strings.TrimSpace(line)
102102
if line == "" || strings.HasPrefix(line, "#") {
103103
continue

test/e2e/fixtures/util.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func Exec(name string, args ...string) (string, error) {
2626
if err != nil {
2727
errorln(err)
2828
}
29-
for _, s := range strings.Split(output, "\n") {
29+
for s := range strings.SplitSeq(output, "\n") {
3030
_, _ = fmt.Println(s)
3131
}
3232
return output, err
@@ -56,8 +56,8 @@ func runWithTimeout(cmd *exec.Cmd) (string, error) {
5656
// LoadObject is used to load yaml to runtime.Object
5757
func LoadObject(text string) (runtime.Object, error) {
5858
var yaml string
59-
if strings.HasPrefix(text, "@") {
60-
file := strings.TrimPrefix(text, "@")
59+
if after, ok := strings.CutPrefix(text, "@"); ok {
60+
file := after
6161
f, err := os.ReadFile(filepath.Clean(file))
6262
if err != nil {
6363
return nil, err

test/e2e/fixtures/when.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"maps"
78
"reflect"
89
"strconv"
910
"strings"
@@ -601,9 +602,7 @@ func (w *When) CreateConfigMap(name string, data map[string]string, customLabels
601602

602603
labels := map[string]string{Label: "true"}
603604

604-
for k, v := range customLabels {
605-
labels[k] = v
606-
}
605+
maps.Copy(labels, customLabels)
607606

608607
ctx := logging.TestContext(w.t.Context())
609608
_, err := w.kubeClient.CoreV1().ConfigMaps(Namespace).Create(ctx, &corev1.ConfigMap{
@@ -621,9 +620,7 @@ func (w *When) UpdateConfigMap(name string, data map[string]string, customLabels
621620

622621
labels := map[string]string{Label: "true"}
623622

624-
for k, v := range customLabels {
625-
labels[k] = v
626-
}
623+
maps.Copy(labels, customLabels)
627624

628625
ctx := logging.TestContext(w.t.Context())
629626
_, err := w.kubeClient.CoreV1().ConfigMaps(Namespace).Update(ctx, &corev1.ConfigMap{

0 commit comments

Comments
 (0)