move helper funcs to util/deployment.go from util.go

This commit is contained in:
mqliang
2016-02-11 14:19:31 +08:00
parent b1dedc0900
commit d96cdb93c4
3 changed files with 27 additions and 31 deletions

View File

@@ -19,7 +19,9 @@ package intstr
import (
"encoding/json"
"fmt"
"math"
"strconv"
"strings"
"github.com/google/gofuzz"
)
@@ -113,3 +115,22 @@ func (intstr *IntOrString) Fuzz(c fuzz.Continue) {
c.Fuzz(&intstr.StrVal)
}
}
func GetIntOrPercentValue(intOrStr *IntOrString) (int, bool, error) {
switch intOrStr.Type {
case Int:
return intOrStr.IntValue(), false, nil
case String:
s := strings.Replace(intOrStr.StrVal, "%", "", -1)
v, err := strconv.Atoi(s)
if err != nil {
return 0, false, fmt.Errorf("invalid value %q: %v", intOrStr.StrVal, err)
}
return int(v), true, nil
}
return 0, false, fmt.Errorf("invalid value: neither int nor percentage")
}
func GetValueFromPercent(percent int, value int) int {
return int(math.Ceil(float64(percent) * (float64(value)) / 100))
}