mirror of
https://github.com/lxc/incus
synced 2026-08-02 05:26:46 +00:00
global: Use strings.CutSuffix instead of HasSuffix/TrimSuffix
Signed-off-by: Stéphane Graber <stgraber@stgraber.org>
This commit is contained in:
parent
b7a3c767f8
commit
be3bb47d39
@ -226,8 +226,9 @@ var InstanceConfigKeysAny = map[string]func(value string) error{
|
||||
return nil
|
||||
}
|
||||
|
||||
if strings.HasSuffix(value, "%") {
|
||||
num, err := strconv.ParseInt(strings.TrimSuffix(value, "%"), 10, 64)
|
||||
before, ok := strings.CutSuffix(value, "%")
|
||||
if ok {
|
||||
num, err := strconv.ParseInt(before, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -478,9 +479,10 @@ var InstanceConfigKeysContainer = map[string]func(value string) error{
|
||||
return nil
|
||||
}
|
||||
|
||||
if strings.HasSuffix(value, "%") {
|
||||
before, ok := strings.CutSuffix(value, "%")
|
||||
if ok {
|
||||
// Percentage based allocation
|
||||
_, err := strconv.Atoi(strings.TrimSuffix(value, "%"))
|
||||
_, err := strconv.Atoi(before)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -41,9 +41,10 @@ func ParseCPU(cpuAllowance string, cpuPriority string) (int64, int64, int64, err
|
||||
cpuCfsPeriod := int64(-1)
|
||||
|
||||
if cpuAllowance != "" {
|
||||
if strings.HasSuffix(cpuAllowance, "%") {
|
||||
before, ok := strings.CutSuffix(cpuAllowance, "%")
|
||||
if ok {
|
||||
// Percentage based allocation
|
||||
percent, err := strconv.Atoi(strings.TrimSuffix(cpuAllowance, "%"))
|
||||
percent, err := strconv.Atoi(before)
|
||||
if err != nil {
|
||||
return -1, -1, -1, err
|
||||
}
|
||||
|
||||
@ -2951,8 +2951,9 @@ func (d *disk) parseLimit(dev deviceConfig.Device) (int64, int64, int64, int64,
|
||||
return bps, iops, nil
|
||||
}
|
||||
|
||||
if strings.HasSuffix(value, "iops") {
|
||||
iops, err = strconv.ParseInt(strings.TrimSuffix(value, "iops"), 10, 64)
|
||||
before, ok := strings.CutSuffix(value, "iops")
|
||||
if ok {
|
||||
iops, err = strconv.ParseInt(before, 10, 64)
|
||||
if err != nil {
|
||||
return -1, -1, err
|
||||
}
|
||||
|
||||
@ -936,8 +936,9 @@ func (d *ceph) parseParent(parent string) (Volume, string, error) {
|
||||
name = strings.SplitN(name, "image_", 2)[1]
|
||||
|
||||
// Check for block indicator.
|
||||
if strings.HasSuffix(name, ".block") {
|
||||
name = strings.TrimSuffix(name, ".block")
|
||||
before, ok := strings.CutSuffix(name, ".block")
|
||||
if ok {
|
||||
name = before
|
||||
vol.contentType = ContentTypeBlock
|
||||
} else {
|
||||
vol.contentType = ContentTypeFS
|
||||
@ -978,11 +979,12 @@ func (d *ceph) parseParent(parent string) (Volume, string, error) {
|
||||
name = strings.SplitN(name, "custom_", 2)[1]
|
||||
|
||||
// Check for block or ISO indicator.
|
||||
if strings.HasSuffix(name, ".block") {
|
||||
name = strings.TrimSuffix(name, ".block")
|
||||
before, ok := strings.CutSuffix(name, ".block")
|
||||
if ok {
|
||||
name = before
|
||||
vol.contentType = ContentTypeBlock
|
||||
} else if strings.HasSuffix(name, ".iso") {
|
||||
name = strings.TrimSuffix(name, ".iso")
|
||||
} else if before, ok := strings.CutSuffix(name, ".iso"); ok {
|
||||
name = before
|
||||
vol.contentType = ContentTypeISO
|
||||
} else {
|
||||
vol.contentType = ContentTypeFS
|
||||
@ -1012,8 +1014,9 @@ func (d *ceph) parseParent(parent string) (Volume, string, error) {
|
||||
name = strings.SplitN(name, "container_", 2)[1]
|
||||
|
||||
// Check for block indicator.
|
||||
if strings.HasSuffix(name, ".block") {
|
||||
name = strings.TrimSuffix(name, ".block")
|
||||
before, ok := strings.CutSuffix(name, ".block")
|
||||
if ok {
|
||||
name = before
|
||||
vol.contentType = ContentTypeBlock
|
||||
} else {
|
||||
vol.contentType = ContentTypeFS
|
||||
@ -1043,8 +1046,9 @@ func (d *ceph) parseParent(parent string) (Volume, string, error) {
|
||||
name = strings.SplitN(name, "virtual-machine_", 2)[1]
|
||||
|
||||
// Check for block indicator.
|
||||
if strings.HasSuffix(name, ".block") {
|
||||
name = strings.TrimSuffix(name, ".block")
|
||||
before, ok := strings.CutSuffix(name, ".block")
|
||||
if ok {
|
||||
name = before
|
||||
vol.contentType = ContentTypeBlock
|
||||
} else {
|
||||
vol.contentType = ContentTypeFS
|
||||
|
||||
Loading…
Reference in New Issue
Block a user