replace strings.SplitN with strings.Cut

Signed-off-by: Amir M. Ghazanfari <a.m.ghazanfari76@gmail.com>
This commit is contained in:
Amir M. Ghazanfari
2024-09-28 10:02:21 +03:30
parent 15904913c2
commit faffe1b9ee
5 changed files with 18 additions and 20 deletions

View File

@@ -247,15 +247,15 @@ func getProcess(context *cli.Context, bundle string) (*specs.Process, error) {
} }
// Override the user, if passed. // Override the user, if passed.
if user := context.String("user"); user != "" { if user := context.String("user"); user != "" {
u := strings.SplitN(user, ":", 2) uids, gids, ok := strings.Cut(user, ":")
if len(u) > 1 { if ok {
gid, err := strconv.Atoi(u[1]) gid, err := strconv.Atoi(gids)
if err != nil { if err != nil {
return nil, fmt.Errorf("bad gid: %w", err) return nil, fmt.Errorf("bad gid: %w", err)
} }
p.User.GID = uint32(gid) p.User.GID = uint32(gid)
} }
uid, err := strconv.Atoi(u[0]) uid, err := strconv.Atoi(uids)
if err != nil { if err != nil {
return nil, fmt.Errorf("bad uid: %w", err) return nil, fmt.Errorf("bad uid: %w", err)
} }

View File

@@ -282,11 +282,11 @@ func getPageUsageByNUMA(path string) (cgroups.PageUsageByNUMA, error) {
line := scanner.Text() line := scanner.Text()
columns := strings.SplitN(line, " ", maxColumns) columns := strings.SplitN(line, " ", maxColumns)
for i, column := range columns { for i, column := range columns {
byNode := strings.SplitN(column, "=", 2) key, val, ok := strings.Cut(column, "=")
// Some custom kernels have non-standard fields, like // Some custom kernels have non-standard fields, like
// numa_locality 0 0 0 0 0 0 0 0 0 0 // numa_locality 0 0 0 0 0 0 0 0 0 0
// numa_exectime 0 // numa_exectime 0
if len(byNode) < 2 { if !ok {
if i == 0 { if i == 0 {
// Ignore/skip those. // Ignore/skip those.
break break
@@ -296,7 +296,6 @@ func getPageUsageByNUMA(path string) (cgroups.PageUsageByNUMA, error) {
return stats, malformedLine(path, file, line) return stats, malformedLine(path, file, line)
} }
} }
key, val := byNode[0], byNode[1]
if i == 0 { // First column: key is name, val is total. if i == 0 { // First column: key is name, val is total.
field = getNUMAField(&stats, key) field = getNUMAField(&stats, key)
if field == nil { // unknown field (new kernel?) if field == nil { // unknown field (new kernel?)

View File

@@ -21,13 +21,13 @@ func RangeToBits(str string) ([]byte, error) {
if r == "" { if r == "" {
continue continue
} }
ranges := strings.SplitN(r, "-", 2) startr, endr, ok := strings.Cut(r, "-")
if len(ranges) > 1 { if ok {
start, err := strconv.ParseUint(ranges[0], 10, 32) start, err := strconv.ParseUint(startr, 10, 32)
if err != nil { if err != nil {
return nil, err return nil, err
} }
end, err := strconv.ParseUint(ranges[1], 10, 32) end, err := strconv.ParseUint(endr, 10, 32)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -38,7 +38,7 @@ func RangeToBits(str string) ([]byte, error) {
bits.SetBit(bits, int(i), 1) bits.SetBit(bits, int(i), 1)
} }
} else { } else {
val, err := strconv.ParseUint(ranges[0], 10, 32) val, err := strconv.ParseUint(startr, 10, 32)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -259,11 +259,10 @@ func containerInit(t initType, config *initConfig, pipe *syncSocket, consoleSock
// current processes's environment. // current processes's environment.
func populateProcessEnvironment(env []string) error { func populateProcessEnvironment(env []string) error {
for _, pair := range env { for _, pair := range env {
p := strings.SplitN(pair, "=", 2) name, val, ok := strings.Cut(pair, "=")
if len(p) < 2 { if !ok {
return errors.New("invalid environment variable: missing '='") return errors.New("invalid environment variable: missing '='")
} }
name, val := p[0], p[1]
if name == "" { if name == "" {
return errors.New("invalid environment variable: name cannot be empty") return errors.New("invalid environment variable: name cannot be empty")
} }

View File

@@ -101,14 +101,14 @@ func SearchLabels(labels []string, key string) (string, bool) {
func Annotations(labels []string) (bundle string, userAnnotations map[string]string) { func Annotations(labels []string) (bundle string, userAnnotations map[string]string) {
userAnnotations = make(map[string]string) userAnnotations = make(map[string]string)
for _, l := range labels { for _, l := range labels {
parts := strings.SplitN(l, "=", 2) name, value, ok := strings.Cut(l, "=")
if len(parts) < 2 { if !ok {
continue continue
} }
if parts[0] == "bundle" { if name == "bundle" {
bundle = parts[1] bundle = value
} else { } else {
userAnnotations[parts[0]] = parts[1] userAnnotations[name] = value
} }
} }
return return