Skip to content
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

Refactors for safer handling of strings #1578

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions cmd/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,20 +369,12 @@ func rallyCommandAction(cmd *cobra.Command, args []string) error {
}

func getPackageNameAndVersion(packageFromRegistry string) (string, string, error) {
packageData := strings.SplitN(packageFromRegistry, "-", 2)

if len(packageData) != 2 {
return "", "", fmt.Errorf("package name and version from registry not valid (%s)", packageFromRegistry)
}

packageName := packageData[0]
packageVersion := packageData[1]

if len(packageName) > 0 && len(packageVersion) == 0 {
name, version, valid := strings.Cut(packageFromRegistry, "-")
if !valid || name == "" || version == "" {
return "", "", fmt.Errorf("package name and version from registry not valid (%s)", packageFromRegistry)
}

return packageName, packageVersion, nil
return name, version, nil
}

func getSystemCommand() *cobra.Command {
Expand Down
9 changes: 6 additions & 3 deletions internal/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,14 @@ func (p *portMapping) UnmarshalYAML(node *yaml.Node) error {
}

// First, parse out the protocol.
parts := strings.Split(str, "/")
p.Protocol = parts[1]
mapping, protocol, found := strings.Cut(str, "/")
if !found {
return errors.New("could not find protocol in port mapping")
}
p.Protocol = protocol

// Now, try to parse out external host, external IP, and internal port.
parts = strings.Split(parts[0], ":")
parts := strings.Split(mapping, ":")
var externalIP, internalPortStr, externalPortStr string
switch len(parts) {
case 1:
Expand Down
10 changes: 5 additions & 5 deletions internal/packages/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ func parsePackageRequirements(keyValuePairs []string) (*packageRequirements, err
var pr packageRequirements

for _, keyPair := range keyValuePairs {
s := strings.SplitN(keyPair, "=", 2)
if len(s) != 2 {
key, value, valid := strings.Cut(keyPair, "=")
if !valid {
return nil, fmt.Errorf("invalid key-value pair: %s", keyPair)
}

switch s[0] {
switch key {
case kibanaVersionRequirement:
ver, err := semver.NewVersion(s[1])
ver, err := semver.NewVersion(value)
if err != nil {
return nil, fmt.Errorf("can't parse kibana.version as valid semver: %w", err)
}
Expand All @@ -73,7 +73,7 @@ func parsePackageRequirements(keyValuePairs []string) (*packageRequirements, err
}
pr.kibana.version = &withoutPrerelease
default:
return nil, fmt.Errorf("unknown package requirement: %s", s[0])
return nil, fmt.Errorf("unknown package requirement: %s", key)
}
}
return &pr, nil
Expand Down
6 changes: 3 additions & 3 deletions internal/stack/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ func newServiceStatus(description *docker.ContainerDescription) (*ServiceStatus,
}

func getVersionFromDockerImage(dockerImage string) string {
fields := strings.Split(dockerImage, ":")
if len(fields) == 2 {
return fields[1]
_, version, found := strings.Cut(dockerImage, ":")
if found {
return version
}
return "latest"
}
7 changes: 2 additions & 5 deletions internal/stack/parselogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,12 @@ func ParseLogs(options ParseLogsOptions, process func(log LogLine) error) error
for scanner.Scan() {
line := scanner.Text()

messageSlice := strings.SplitN(line, "|", 2)

if len(messageSlice) != 2 {
_, messageLog, valid := strings.Cut(line, "|")
if !valid {
logger.Debugf("skipped malformed docker-compose log line: %s", line)
continue
}

messageLog := messageSlice[1]

var log LogLine
err := json.Unmarshal([]byte(messageLog), &log)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ func (prs PackageVersions) Strings() []string {
func ParsePackageVersions(packageVersions []string) (PackageVersions, error) {
var parsed PackageVersions
for _, pv := range packageVersions {
s := strings.Split(pv, "-")
if len(s) != 2 {
name, version, valid := strings.Cut(pv, "-")
if !valid || name == "" || version == "" {
return nil, fmt.Errorf("invalid package revision format (expected: <package_name>-<version>): %s", pv)
}

revision, err := NewPackageVersion(s[0], s[1])
revision, err := NewPackageVersion(name, version)
if err != nil {
return nil, fmt.Errorf("can't create package version (%s): %w", s, err)
return nil, fmt.Errorf("can't create package version (name: %s, version: %s): %w", name, version, err)
}
parsed = append(parsed, *revision)
}
Expand Down