modernize: use strings.CutPrefix/SplitSeq/FieldsSeq
Use the CutPrefix(), SplitSeq(), and FieldsSeq() functions from the strings package when chopping up or iterating over parts of strings, per golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
This commit is contained in:
parent
ebc9b40491
commit
16680a4dfd
|
@ -277,8 +277,8 @@ func containerOutputHeader(truncate bool) {
|
||||||
|
|
||||||
func parseCtrFilter(filter string) (*containerFilterParams, error) {
|
func parseCtrFilter(filter string) (*containerFilterParams, error) {
|
||||||
params := new(containerFilterParams)
|
params := new(containerFilterParams)
|
||||||
filters := strings.Split(filter, ",")
|
filters := strings.SplitSeq(filter, ",")
|
||||||
for _, param := range filters {
|
for param := range filters {
|
||||||
pair := strings.SplitN(param, "=", 2)
|
pair := strings.SplitN(param, "=", 2)
|
||||||
if len(pair) != 2 {
|
if len(pair) != 2 {
|
||||||
return nil, fmt.Errorf("incorrect filter value %q, should be of form filter=value", param)
|
return nil, fmt.Errorf("incorrect filter value %q, should be of form filter=value", param)
|
||||||
|
|
|
@ -801,7 +801,7 @@ func copierWithSubprocess(bulkReader io.Reader, bulkWriter io.Writer, req reques
|
||||||
}
|
}
|
||||||
loggedOutput := strings.TrimSuffix(errorBuffer.String(), "\n")
|
loggedOutput := strings.TrimSuffix(errorBuffer.String(), "\n")
|
||||||
if len(loggedOutput) > 0 {
|
if len(loggedOutput) > 0 {
|
||||||
for _, output := range strings.Split(loggedOutput, "\n") {
|
for output := range strings.SplitSeq(loggedOutput, "\n") {
|
||||||
logrus.Debug(output)
|
logrus.Debug(output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1588,8 +1588,8 @@ func mapWithPrefixedKeysWithoutKeyPrefix[K any](m map[string]K, p string) map[st
|
||||||
}
|
}
|
||||||
cloned := make(map[string]K, len(m))
|
cloned := make(map[string]K, len(m))
|
||||||
for k, v := range m {
|
for k, v := range m {
|
||||||
if strings.HasPrefix(k, p) {
|
if after, ok := strings.CutPrefix(k, p); ok {
|
||||||
cloned[strings.TrimPrefix(k, p)] = v
|
cloned[after] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cloned
|
return cloned
|
||||||
|
@ -1819,7 +1819,7 @@ func copierHandlerPut(bulkReader io.Reader, req request, idMappings *idtools.IDM
|
||||||
return fmt.Errorf("%q is not a subdirectory of %q: %w", directory, req.Root, err)
|
return fmt.Errorf("%q is not a subdirectory of %q: %w", directory, req.Root, err)
|
||||||
}
|
}
|
||||||
subdir := ""
|
subdir := ""
|
||||||
for _, component := range strings.Split(rel, string(os.PathSeparator)) {
|
for component := range strings.SplitSeq(rel, string(os.PathSeparator)) {
|
||||||
subdir = filepath.Join(subdir, component)
|
subdir = filepath.Join(subdir, component)
|
||||||
path := filepath.Join(req.Root, subdir)
|
path := filepath.Join(req.Root, subdir)
|
||||||
if err := os.Mkdir(path, 0o700); err == nil {
|
if err := os.Mkdir(path, 0o700); err == nil {
|
||||||
|
@ -2219,7 +2219,7 @@ func copierHandlerMkdir(req request, idMappings *idtools.IDMappings) (*response,
|
||||||
|
|
||||||
subdir := ""
|
subdir := ""
|
||||||
var created []string
|
var created []string
|
||||||
for _, component := range strings.Split(rel, string(os.PathSeparator)) {
|
for component := range strings.SplitSeq(rel, string(os.PathSeparator)) {
|
||||||
subdir = filepath.Join(subdir, component)
|
subdir = filepath.Join(subdir, component)
|
||||||
path := filepath.Join(req.Root, subdir)
|
path := filepath.Join(req.Root, subdir)
|
||||||
if err := os.Mkdir(path, 0o700); err == nil {
|
if err := os.Mkdir(path, 0o700); err == nil {
|
||||||
|
|
|
@ -65,7 +65,7 @@ func Lgetxattrs(path string) (map[string]string, error) {
|
||||||
return nil, fmt.Errorf("unable to read list of attributes for %q: size would have been too big", path)
|
return nil, fmt.Errorf("unable to read list of attributes for %q: size would have been too big", path)
|
||||||
}
|
}
|
||||||
m := make(map[string]string)
|
m := make(map[string]string)
|
||||||
for _, attribute := range strings.Split(string(list), string('\000')) {
|
for attribute := range strings.SplitSeq(string(list), string('\000')) {
|
||||||
if isRelevantXattr(attribute) {
|
if isRelevantXattr(attribute) {
|
||||||
attributeSize := initialXattrValueSize
|
attributeSize := initialXattrValueSize
|
||||||
var attributeValue []byte
|
var attributeValue []byte
|
||||||
|
|
|
@ -836,12 +836,12 @@ func (b *Executor) Build(ctx context.Context, stages imagebuilder.Stages) (image
|
||||||
}
|
}
|
||||||
case "ADD", "COPY":
|
case "ADD", "COPY":
|
||||||
for _, flag := range child.Flags { // flags for this instruction
|
for _, flag := range child.Flags { // flags for this instruction
|
||||||
if strings.HasPrefix(flag, "--from=") {
|
if after, ok := strings.CutPrefix(flag, "--from="); ok {
|
||||||
// TODO: this didn't undergo variable and
|
// TODO: this didn't undergo variable and
|
||||||
// arg expansion, so if the previous stage
|
// arg expansion, so if the previous stage
|
||||||
// was named using argument values, we might
|
// was named using argument values, we might
|
||||||
// not record the right value here.
|
// not record the right value here.
|
||||||
rootfs := strings.TrimPrefix(flag, "--from=")
|
rootfs := after
|
||||||
b.rootfsMap[rootfs] = struct{}{}
|
b.rootfsMap[rootfs] = struct{}{}
|
||||||
logrus.Debugf("rootfs needed for COPY in stage %d: %q", stageIndex, rootfs)
|
logrus.Debugf("rootfs needed for COPY in stage %d: %q", stageIndex, rootfs)
|
||||||
// Populate dependency tree and check
|
// Populate dependency tree and check
|
||||||
|
@ -885,8 +885,8 @@ func (b *Executor) Build(ctx context.Context, stages imagebuilder.Stages) (image
|
||||||
// dependency calculation.
|
// dependency calculation.
|
||||||
if strings.HasPrefix(flag, "--mount=") && strings.Contains(flag, "from") {
|
if strings.HasPrefix(flag, "--mount=") && strings.Contains(flag, "from") {
|
||||||
mountFlags := strings.TrimPrefix(flag, "--mount=")
|
mountFlags := strings.TrimPrefix(flag, "--mount=")
|
||||||
fields := strings.Split(mountFlags, ",")
|
fields := strings.SplitSeq(mountFlags, ",")
|
||||||
for _, field := range fields {
|
for field := range fields {
|
||||||
if mountFrom, hasFrom := strings.CutPrefix(field, "from="); hasFrom {
|
if mountFrom, hasFrom := strings.CutPrefix(field, "from="); hasFrom {
|
||||||
// Check if this base is a stage if yes
|
// Check if this base is a stage if yes
|
||||||
// add base to current stage's dependency tree
|
// add base to current stage's dependency tree
|
||||||
|
|
|
@ -1913,7 +1913,7 @@ func (s *StageExecutor) getCreatedBy(node *parser.Node, addedContentSummary stri
|
||||||
|
|
||||||
switch command {
|
switch command {
|
||||||
case "ARG":
|
case "ARG":
|
||||||
for _, variable := range strings.Fields(node.Original) {
|
for variable := range strings.FieldsSeq(node.Original) {
|
||||||
if variable != "ARG" {
|
if variable != "ARG" {
|
||||||
s.argsFromContainerfile = append(s.argsFromContainerfile, variable)
|
s.argsFromContainerfile = append(s.argsFromContainerfile, variable)
|
||||||
}
|
}
|
||||||
|
|
8
info.go
8
info.go
|
@ -183,11 +183,11 @@ func getHostDistributionInfo() map[string]string {
|
||||||
|
|
||||||
l := bufio.NewScanner(f)
|
l := bufio.NewScanner(f)
|
||||||
for l.Scan() {
|
for l.Scan() {
|
||||||
if strings.HasPrefix(l.Text(), "ID=") {
|
if after, ok := strings.CutPrefix(l.Text(), "ID="); ok {
|
||||||
dist["Distribution"] = strings.TrimPrefix(l.Text(), "ID=")
|
dist["Distribution"] = after
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(l.Text(), "VERSION_ID=") {
|
if after, ok := strings.CutPrefix(l.Text(), "VERSION_ID="); ok {
|
||||||
dist["Version"] = strings.Trim(strings.TrimPrefix(l.Text(), "VERSION_ID="), "\"")
|
dist["Version"] = strings.Trim(after, "\"")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return dist
|
return dist
|
||||||
|
|
|
@ -543,7 +543,7 @@ func slop(size int64, slop string) int64 {
|
||||||
if slop == "" {
|
if slop == "" {
|
||||||
return size * 5 / 4
|
return size * 5 / 4
|
||||||
}
|
}
|
||||||
for _, factor := range strings.Split(slop, "+") {
|
for factor := range strings.SplitSeq(slop, "+") {
|
||||||
factor = strings.TrimSpace(factor)
|
factor = strings.TrimSpace(factor)
|
||||||
if factor == "" {
|
if factor == "" {
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -240,8 +240,8 @@ func GenerateMeasurement(workloadConfig WorkloadConfig, firmwareLibrary string)
|
||||||
scanner := bufio.NewScanner(&stdout)
|
scanner := bufio.NewScanner(&stdout)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
if strings.HasPrefix(line, prefix+":") {
|
if after, ok := strings.CutPrefix(line, prefix+":"); ok {
|
||||||
return strings.TrimSpace(strings.TrimPrefix(line, prefix+":")), nil
|
return strings.TrimSpace(after), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "", fmt.Errorf("generating measurement: no line starting with %q found in output from krunfw_measurement", prefix+":")
|
return "", fmt.Errorf("generating measurement: no line starting with %q found in output from krunfw_measurement", prefix+":")
|
||||||
|
|
|
@ -474,7 +474,7 @@ func readBuildArgFile(buildargfile string, args map[string]string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, arg := range strings.Split(string(argfile), "\n") {
|
for arg := range strings.SplitSeq(string(argfile), "\n") {
|
||||||
if len(arg) == 0 || arg[0] == '#' {
|
if len(arg) == 0 || arg[0] == '#' {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
@ -733,7 +733,7 @@ func GetBuildOutput(buildOutput string) (define.BuildOutputOption, error) {
|
||||||
isStdout := false
|
isStdout := false
|
||||||
typeSelected := ""
|
typeSelected := ""
|
||||||
pathSelected := ""
|
pathSelected := ""
|
||||||
for _, option := range strings.Split(buildOutput, ",") {
|
for option := range strings.SplitSeq(buildOutput, ",") {
|
||||||
key, value, found := strings.Cut(option, "=")
|
key, value, found := strings.Cut(option, "=")
|
||||||
if !found {
|
if !found {
|
||||||
return define.BuildOutputOption{}, fmt.Errorf("invalid build output options %q, expected format key=value", buildOutput)
|
return define.BuildOutputOption{}, fmt.Errorf("invalid build output options %q, expected format key=value", buildOutput)
|
||||||
|
@ -789,7 +789,7 @@ func GetConfidentialWorkloadOptions(arg string) (define.ConfidentialWorkloadOpti
|
||||||
TempDir: GetTempDir(),
|
TempDir: GetTempDir(),
|
||||||
}
|
}
|
||||||
defaults := options
|
defaults := options
|
||||||
for _, option := range strings.Split(arg, ",") {
|
for option := range strings.SplitSeq(arg, ",") {
|
||||||
var err error
|
var err error
|
||||||
switch {
|
switch {
|
||||||
case strings.HasPrefix(option, "type="):
|
case strings.HasPrefix(option, "type="):
|
||||||
|
@ -936,7 +936,7 @@ func GetAutoOptions(base string) (*storageTypes.AutoUserNsOptions, error) {
|
||||||
if len(parts) == 1 {
|
if len(parts) == 1 {
|
||||||
return &options, nil
|
return &options, nil
|
||||||
}
|
}
|
||||||
for _, o := range strings.Split(parts[1], ",") {
|
for o := range strings.SplitSeq(parts[1], ",") {
|
||||||
v := strings.SplitN(o, "=", 2)
|
v := strings.SplitN(o, "=", 2)
|
||||||
if len(v) != 2 {
|
if len(v) != 2 {
|
||||||
return nil, fmt.Errorf("invalid option specified: %q", o)
|
return nil, fmt.Errorf("invalid option specified: %q", o)
|
||||||
|
|
|
@ -672,10 +672,10 @@ func buildUsingDocker(ctx context.Context, t *testing.T, client *docker.Client,
|
||||||
// read the Dockerfile so that we can pull base images
|
// read the Dockerfile so that we can pull base images
|
||||||
dockerfileContent, err := os.ReadFile(dockerfileName)
|
dockerfileContent, err := os.ReadFile(dockerfileName)
|
||||||
require.NoErrorf(t, err, "reading dockerfile %q", dockerfileName)
|
require.NoErrorf(t, err, "reading dockerfile %q", dockerfileName)
|
||||||
for _, line := range strings.Split(string(dockerfileContent), "\n") {
|
for line := range strings.SplitSeq(string(dockerfileContent), "\n") {
|
||||||
line = strings.TrimSpace(line)
|
line = strings.TrimSpace(line)
|
||||||
if strings.HasPrefix(line, "# syntax=") {
|
if after, ok := strings.CutPrefix(line, "# syntax="); ok {
|
||||||
pullImageIfMissing(t, client, strings.TrimPrefix(line, "# syntax="))
|
pullImageIfMissing(t, client, after)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
parsed, err := imagebuilder.ParseDockerfile(bytes.NewReader(dockerfileContent))
|
parsed, err := imagebuilder.ParseDockerfile(bytes.NewReader(dockerfileContent))
|
||||||
|
@ -1131,8 +1131,8 @@ func applyLayerToFSTree(t *testing.T, layer *Layer, root *FSEntry) {
|
||||||
}
|
}
|
||||||
// if the item is a whiteout, strip the "this is a whiteout
|
// if the item is a whiteout, strip the "this is a whiteout
|
||||||
// entry" prefix and remove the item it names
|
// entry" prefix and remove the item it names
|
||||||
if strings.HasPrefix(base, ".wh.") {
|
if after, ok := strings.CutPrefix(base, ".wh."); ok {
|
||||||
delete(dirEntry.Children, strings.TrimPrefix(base, ".wh."))
|
delete(dirEntry.Children, after)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// if the item already exists, make sure we don't get confused
|
// if the item already exists, make sure we don't get confused
|
||||||
|
@ -1281,8 +1281,8 @@ func compareJSON(a, b map[string]any, skip []string) (missKeys, leftKeys, diffKe
|
||||||
var nextSkip []string
|
var nextSkip []string
|
||||||
prefix := k + ":"
|
prefix := k + ":"
|
||||||
for _, s := range skip {
|
for _, s := range skip {
|
||||||
if strings.HasPrefix(s, prefix) {
|
if after, ok0 := strings.CutPrefix(s, prefix); ok0 {
|
||||||
nextSkip = append(nextSkip, strings.TrimPrefix(s, prefix))
|
nextSkip = append(nextSkip, after)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
submiss, subleft, subdiff, ok := compareJSON(v.(map[string]any), vb.(map[string]any), nextSkip)
|
submiss, subleft, subdiff, ok := compareJSON(v.(map[string]any), vb.(map[string]any), nextSkip)
|
||||||
|
|
Loading…
Reference in New Issue