wgcfg: use string cut instead of string split

Signed-off-by: julianknodt <julianknodt@gmail.com>
This commit is contained in:
julianknodt
2021-07-13 10:12:47 -07:00
committed by Julian Knodt
parent 664edbe566
commit d349a3231e
2 changed files with 13 additions and 6 deletions

View File

@@ -69,6 +69,14 @@ func parseKeyHex(s string) (*wgkey.Key, error) {
return &key, nil
}
// stringsCut is strings.Cut from proposed https://github.com/golang/go/issues/46336.
func stringsCut(s, sep string) (before, after string, found bool) {
if i := strings.Index(s, sep); i >= 0 {
return s[:i], s[i+len(sep):], true
}
return s, "", false
}
// FromUAPI generates a Config from r.
// r should be generated by calling device.IpcGetOperation;
// it is not compatible with other uapi streams.
@@ -83,12 +91,10 @@ func FromUAPI(r io.Reader) (*Config, error) {
if line == "" {
continue
}
parts := strings.Split(line, "=")
if len(parts) != 2 {
return nil, fmt.Errorf("failed to parse line %q, found %d =-separated parts, want 2", line, len(parts))
key, value, ok := stringsCut(line, "=")
if !ok {
return nil, fmt.Errorf("failed to cut line %q on =", line)
}
key := parts[0]
value := parts[1]
valueBytes := scanner.Bytes()[len(key)+1:]
if key == "public_key" {