-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use strings.Cut where possible #4470
base: main
Are you sure you want to change the base?
Changes from 1 commit
0e2d611
d9e1005
a8da6c6
a4ed2d8
32fa40a
486c451
b33fe69
4c86035
cddca40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,20 +124,17 @@ func getSubCgroupPaths(args []string) (map[string]string, error) { | |
paths := make(map[string]string, len(args)) | ||
for _, c := range args { | ||
// Split into controller:path. | ||
cs := strings.SplitN(c, ":", 3) | ||
if len(cs) > 2 { | ||
return nil, fmt.Errorf("invalid --cgroup argument: %s", c) | ||
} | ||
if len(cs) == 1 { // no controller: prefix | ||
if ctr, path, ok := strings.Cut(c, ":"); ok { | ||
// There may be a few comma-separated controllers. | ||
for _, ctrl := range strings.Split(ctr, ",") { | ||
paths[ctrl] = path | ||
} | ||
} else { | ||
// No controller: prefix. | ||
if len(args) != 1 { | ||
return nil, fmt.Errorf("invalid --cgroup argument: %s (missing <controller>: prefix)", c) | ||
} | ||
paths[""] = c | ||
Comment on lines
+133
to
137
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was trying to somewhat grasp what this case is for;
So; # valid: single arg
--cgroup a,b:/hello-world
# valid: single arg without controller
--cgroup /hello-world
# invalid: multiple args, one without controller
--cgroup a,b:/hello-world --cgroup /hello-world
--cgroup /hello-world --cgroup a,b:/hello-world So, if no controller is passed, it's for everything which is only allowed if no other (per-controller) paths are specified, correct? (so not allowed to be combined). ☝️ Perhaps that's something we should capture in a comment on the function Also;
|
||
} else { | ||
// There may be a few comma-separated controllers. | ||
for _, ctrl := range strings.Split(cs[0], ",") { | ||
paths[ctrl] = cs[1] | ||
} | ||
} | ||
} | ||
return paths, nil | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I havn't checked that whether this will lead to some regressions or not, for example, if c == "a:b:c:d:e":
When using
SplitN
,cs[1]
will be "b";When using
Cut
,path
will be "b:c:d:e".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, the old code would error out when
c == "a:b:c:d:e"
.The new code would not, assigning
b:c:d:e
topath
.To me, the new code is (ever so slightly) more correct, since
:
is a valid symbol which should be allowed inpath
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, was looking at that as well; agreed, looks like new code is more correct here.
I still need to have a quick look at the splitting on commas, and the
if len(args) != 1 {
check below. Probably will check out this branch locally