Skip to content

Commit

Permalink
wrappers: review feedback
Browse files Browse the repository at this point in the history
add new function []int => string in strutils, use that instead of chained string functions
  • Loading branch information
Meulengracht committed Feb 24, 2022
1 parent eaac0d3 commit be20d29
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
13 changes: 13 additions & 0 deletions strutil/strutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ func SizeToStr(size int64) string {
panic("SizeToStr got a size bigger than math.MaxInt64")
}

// SliceToCommaSeparatedString converts an int array to a comma-separated string
func SliceToCommaSeparatedString(vals []int) string {
b := &strings.Builder{}
last := len(vals) - 1
for i, v := range vals {
b.WriteString(strconv.Itoa(v))
if i != last {
b.WriteRune(',')
}
}
return b.String()
}

// Quoted formats a slice of strings to a quoted list of
// comma-separated strings, e.g. `"snap1", "snap2"`
func Quoted(names []string) string {
Expand Down
2 changes: 1 addition & 1 deletion wrappers/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func formatCpuGroupSlice(grp *quota.Group) string {
return ""
}

allowedCpusValue := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(grp.CPULimit.AllowedCPUs)), ","), "[]")
allowedCpusValue := strutil.SliceToCommaSeparatedString(grp.CPULimit.AllowedCPUs)
return fmt.Sprintf("AllowedCPUs=%s\n", allowedCpusValue)
}

Expand Down
4 changes: 2 additions & 2 deletions wrappers/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ TasksAccounting=true
TasksMax=%[5]d
`

allowedCpusValue := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(resourceLimits.CPU.AllowedCPUs)), ","), "[]")
allowedCpusValue := strutil.SliceToCommaSeparatedString(resourceLimits.CPU.AllowedCPUs)
sliceContent := fmt.Sprintf(sliceTempl, grp.Name,
resourceLimits.CPU.Count*resourceLimits.CPU.Percentage,
allowedCpusValue,
Expand Down Expand Up @@ -851,7 +851,7 @@ MemoryLimit=%[3]d
TasksAccounting=true
`

allowedCpusValue := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(resourceLimits.CPU.AllowedCPUs)), ","), "[]")
allowedCpusValue := strutil.SliceToCommaSeparatedString(resourceLimits.CPU.AllowedCPUs)
c.Assert(sliceFile, testutil.FileEquals, fmt.Sprintf(templ, "foogroup", allowedCpusValue, resourceLimits.Memory.Limit))
c.Assert(subSliceFile, testutil.FileEquals, fmt.Sprintf(templ, "subgroup", allowedCpusValue, resourceLimits.Memory.Limit))
}
Expand Down

0 comments on commit be20d29

Please sign in to comment.