Skip to content

Commit

Permalink
Refactor for safer handling of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoriano committed Dec 1, 2023
1 parent 5f59b9e commit 0f6a155
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 31 deletions.
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", value)
}
}
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

0 comments on commit 0f6a155

Please sign in to comment.