File: changes.go

package info (click to toggle)
podman 5.7.0%2Bds2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,824 kB
  • sloc: sh: 4,700; python: 2,798; perl: 1,885; ansic: 1,484; makefile: 977; ruby: 42; csh: 8
file content (34 lines) | stat: -rw-r--r-- 1,228 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package util

import (
	"strings"
	"unicode"
)

// DecodeChanges reads one or more changes from a slice and cleans them up,
// since what we've advertised as being acceptable in the past isn't really.
func DecodeChanges(changes []string) []string {
	result := make([]string, 0, len(changes))
	for _, possiblyMultilineChange := range changes {
		for change := range strings.SplitSeq(possiblyMultilineChange, "\n") {
			// In particular, we document that we accept values
			// like "CMD=/bin/sh", which is not valid Dockerfile
			// syntax, so we can't just pass such a value directly
			// to a parser that's going to rightfully reject it.
			// If we trim the string of whitespace at both ends,
			// and the first occurrence of "=" is before the first
			// whitespace, replace that "=" with whitespace.
			change = strings.TrimSpace(change)
			if change == "" {
				continue
			}
			firstEqualIndex := strings.Index(change, "=")
			firstSpaceIndex := strings.IndexFunc(change, unicode.IsSpace)
			if firstEqualIndex != -1 && (firstSpaceIndex == -1 || firstEqualIndex < firstSpaceIndex) {
				change = change[:firstEqualIndex] + " " + change[firstEqualIndex+1:]
			}
			result = append(result, change)
		}
	}
	return result
}