github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/utils/ram.go (about)

     1  package utils
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/docker/go-units"
     8  )
     9  
    10  // ParseRAMInHuman returns int value in bytes of a human readable string
    11  // e.g. 100KB -> 102400
    12  func ParseRAMInHuman(ram string) (int64, error) {
    13  	if ram == "" {
    14  		return 0, nil
    15  	}
    16  	ramInBytes, err := strconv.ParseInt(ram, 10, 64)
    17  	if err == nil {
    18  		return ramInBytes, nil
    19  	}
    20  
    21  	flag := int64(1)
    22  	if strings.HasPrefix(ram, "-") {
    23  		flag = int64(-1)
    24  		ram = strings.TrimLeft(ram, "-")
    25  	}
    26  	ramInBytes, err = units.RAMInBytes(ram)
    27  	if err != nil {
    28  		return 0, err
    29  	}
    30  	return ramInBytes * flag, nil
    31  }