package utils import ( "encoding/json" "errors" "fmt" "sigs.k8s.io/yaml" ) // Converts data from bytes to desired unit of size (i.e. GB, KB, etc.) func ConvertFromByte(b int64) *Unit { const unit = 1000 div, exp := int64(unit), 0 for n := b / unit; n >= unit; n /= unit { div *= unit exp++ } unitLabel := fmt.Sprintf("%cB", "kMGTPE"[exp]) converedUnit := Unit{ Label: unitLabel, Value: fmt.Sprintf("%.1f", float64(b)/float64(div)), } return &converedUnit } func YAMLToJSON(yamlStr *string) ([]byte, error) { if IsNullOrEmpty(yamlStr) { return nil, nil } js, err := yaml.YAMLToJSON([]byte(*yamlStr)) if err != nil { return nil, err } if IsValidJSONObject(js) { return js, nil } return nil, errors.New("not valid JSON") } func JSONToYAML(jsonStr *string) ([]byte, error) { if IsNullOrEmpty(jsonStr) { return nil, nil } return yaml.JSONToYAML([]byte(*jsonStr)) } func ConvertStructToBase64(s interface{}) (string, error) { structBytes, err := json.Marshal(s) if err != nil { return "", err } return ToBase64(structBytes), nil }