github.com/kubernetes-incubator/kube-aws@v0.16.4/provisioner/content.go (about)

     1  package provisioner
     2  
     3  import (
     4  	"encoding/base64"
     5  	"fmt"
     6  	"github.com/kubernetes-incubator/kube-aws/gzipcompressor"
     7  )
     8  
     9  func (c Content) String() string {
    10  	if len(c.str) == 0 && len(c.bytes) > 0 {
    11  		return string(c.bytes)
    12  	}
    13  	return c.str
    14  }
    15  
    16  func (c *Content) UnmarshalYAML(unmarshal func(interface{}) error) error {
    17  	var tmp string
    18  	if err := unmarshal(&tmp); err != nil {
    19  		return err
    20  	}
    21  	*c = NewStringContent(tmp)
    22  	return nil
    23  }
    24  
    25  func (c Content) MarshalYAML() (interface{}, error) {
    26  	return c.String(), nil
    27  }
    28  
    29  func NewStringContent(str string) Content {
    30  	return Content{
    31  		bytes: []byte(str),
    32  		str:   str,
    33  	}
    34  }
    35  
    36  func NewBinaryContent(bytes []byte) Content {
    37  	return Content{
    38  		bytes: bytes,
    39  	}
    40  }
    41  
    42  func (c Content) ToBase64() Content {
    43  	bytes := []byte(base64.StdEncoding.EncodeToString(c.bytes))
    44  	return Content{
    45  		bytes: bytes,
    46  	}
    47  }
    48  
    49  func (c Content) ToGzip() Content {
    50  	bytes, err := gzipcompressor.BytesToGzippedBytes(c.bytes)
    51  	if err != nil {
    52  		panic(fmt.Errorf("Unexpected error in ToGzip: %v", err))
    53  	}
    54  	return Content{
    55  		bytes: bytes,
    56  	}
    57  }
    58  
    59  func (c Content) GzippedBase64Content() string {
    60  	out, err := gzipcompressor.BytesToGzippedBase64String(c.bytes)
    61  	if err != nil {
    62  		return ""
    63  	}
    64  	return out
    65  }