github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/v2_formatted_resource.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"strconv"
     7  
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     9  )
    10  
    11  // V2FormattedResource represents a Cloud Controller Resource that still has the same shape as the V2 Resource.
    12  // The v3 package upload endpoint understands both the V2 shape and the new V3 shape.
    13  // The v3 resource matching endpoint only understands the new V3 shape.
    14  //
    15  // Deprecated: Use Resource going forward. We anticipate that this struct will only be used
    16  // by the v6 cli's v3-push command, which is experimental.
    17  type V2FormattedResource struct {
    18  
    19  	// Filename is the name of the resource.
    20  	Filename string `json:"fn"`
    21  
    22  	// Mode is the operating system file mode (aka file permissions) of the
    23  	// resource.
    24  	Mode os.FileMode `json:"mode"`
    25  
    26  	// SHA1 represents the SHA-1 hash of the resource.
    27  	SHA1 string `json:"sha1"`
    28  
    29  	// Size represents the file size of the resource.
    30  	Size int64 `json:"size"`
    31  }
    32  
    33  // MarshalJSON converts a resource into a Cloud Controller V2FormattedResource.
    34  func (r V2FormattedResource) MarshalJSON() ([]byte, error) {
    35  	var ccResource struct {
    36  		Filename string `json:"fn,omitempty"`
    37  		Mode     string `json:"mode,omitempty"`
    38  		SHA1     string `json:"sha1"`
    39  		Size     int64  `json:"size"`
    40  	}
    41  
    42  	ccResource.Filename = r.Filename
    43  	ccResource.Size = r.Size
    44  	ccResource.SHA1 = r.SHA1
    45  	ccResource.Mode = strconv.FormatUint(uint64(r.Mode), 8)
    46  	return json.Marshal(ccResource)
    47  }
    48  
    49  // UnmarshalJSON helps unmarshal a Cloud Controller V2FormattedResource response.
    50  func (r *V2FormattedResource) UnmarshalJSON(data []byte) error {
    51  	var ccResource struct {
    52  		Filename string `json:"fn,omitempty"`
    53  		Mode     string `json:"mode,omitempty"`
    54  		SHA1     string `json:"sha1"`
    55  		Size     int64  `json:"size"`
    56  	}
    57  
    58  	err := cloudcontroller.DecodeJSON(data, &ccResource)
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	r.Filename = ccResource.Filename
    64  	r.Size = ccResource.Size
    65  	r.SHA1 = ccResource.SHA1
    66  	mode, err := strconv.ParseUint(ccResource.Mode, 8, 32)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	r.Mode = os.FileMode(mode)
    72  	return nil
    73  }