github.com/drone/runner-go@v1.12.0/manifest/unit.go (about)

     1  // Copyright 2019 Drone.IO Inc. All rights reserved.
     2  // Use of this source code is governed by the Polyform License
     3  // that can be found in the LICENSE file.
     4  
     5  package manifest
     6  
     7  import (
     8  	"github.com/docker/go-units"
     9  )
    10  
    11  // BytesSize stores a human-readable size in bytes,
    12  // kibibytes, mebibytes, gibibytes, or tebibytes
    13  // (eg. "44kiB", "17MiB").
    14  type BytesSize int64
    15  
    16  // UnmarshalYAML implements yaml unmarshalling.
    17  func (b *BytesSize) UnmarshalYAML(unmarshal func(interface{}) error) error {
    18  	var intType int64
    19  	if err := unmarshal(&intType); err == nil {
    20  		*b = BytesSize(intType)
    21  		return nil
    22  	}
    23  
    24  	var stringType string
    25  	if err := unmarshal(&stringType); err != nil {
    26  		return err
    27  	}
    28  
    29  	intType, err := units.RAMInBytes(stringType)
    30  	if err == nil {
    31  		*b = BytesSize(intType)
    32  	}
    33  	return err
    34  }
    35  
    36  // String returns a human-readable size in bytes,
    37  // kibibytes, mebibytes, gibibytes, or tebibytes
    38  // (eg. "44kiB", "17MiB").
    39  func (b BytesSize) String() string {
    40  	return units.BytesSize(float64(b))
    41  }