get.porter.sh/porter@v1.3.0/pkg/cnab/dependencies_v2.go (about)

     1  package cnab
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  
     8  	v2 "get.porter.sh/porter/pkg/cnab/extensions/dependencies/v2"
     9  )
    10  
    11  const (
    12  	// DependenciesV2ExtensionShortHand is the short suffix of the DependenciesV2ExtensionKey
    13  	DependenciesV2ExtensionShortHand = "dependencies@v2"
    14  
    15  	// DependenciesV2ExtensionKey represents the full key for the DependenciesV2Extension.
    16  	DependenciesV2ExtensionKey = "org.getporter." + DependenciesV2ExtensionShortHand
    17  
    18  	// DependenciesV2Schema represents the schema for the DependenciesV2 Extension
    19  	DependenciesV2Schema = "https://porter.sh/extensions/dependencies/v2/schema.json"
    20  )
    21  
    22  // DependenciesV2Extension represents the required extension to enable dependencies
    23  var DependenciesV2Extension = RequiredExtension{
    24  	Shorthand: DependenciesV2ExtensionShortHand,
    25  	Key:       DependenciesV2ExtensionKey,
    26  	Schema:    DependenciesV2Schema,
    27  	Reader: func(b ExtendedBundle) (interface{}, error) {
    28  		return b.DependencyV2Reader()
    29  	},
    30  }
    31  
    32  // ReadDependenciesV2 is a convenience method for returning a bonafide
    33  // DependenciesV2 reference after reading from the applicable section from
    34  // the provided bundle
    35  func (b ExtendedBundle) ReadDependenciesV2() (v2.Dependencies, error) {
    36  	raw, err := b.DependencyV2Reader()
    37  	if err != nil {
    38  		return v2.Dependencies{}, err
    39  	}
    40  
    41  	deps, ok := raw.(v2.Dependencies)
    42  	if !ok {
    43  		return v2.Dependencies{}, errors.New("unable to read dependencies v2 extension data")
    44  	}
    45  
    46  	// Return the dependencies
    47  	return deps, nil
    48  }
    49  
    50  // DependencyV2Reader is a Reader for the DependenciesV2Extension, which reads
    51  // from the applicable section in the provided bundle and returns the raw
    52  // data in the form of an interface
    53  func (b ExtendedBundle) DependencyV2Reader() (interface{}, error) {
    54  	data, ok := b.Custom[DependenciesV2ExtensionKey]
    55  	if !ok {
    56  		return nil, fmt.Errorf("attempted to read %s extension data from bundle but none are defined", DependenciesV2ExtensionKey)
    57  	}
    58  
    59  	dataB, err := json.Marshal(data)
    60  	if err != nil {
    61  		return nil, fmt.Errorf("could not marshal the untyped %s extension data %q: %w", DependenciesV2ExtensionKey, string(dataB), err)
    62  	}
    63  
    64  	//Note: For dependency.Name to be set properly ReadDependencyV2
    65  	// *must* be called.
    66  	//todo: make it so that ReadDependencyV2 is only able to be exported.
    67  	deps := v2.Dependencies{}
    68  	err = json.Unmarshal(dataB, &deps)
    69  	if err != nil {
    70  		return nil, fmt.Errorf("could not unmarshal the %s extension %q: %w", DependenciesV2ExtensionKey, string(dataB), err)
    71  	}
    72  
    73  	return deps, nil
    74  }
    75  
    76  // SupportsDependenciesV2 checks if the bundle supports dependencies
    77  func (b ExtendedBundle) SupportsDependenciesV2() bool {
    78  	return b.SupportsExtension(DependenciesV2ExtensionKey)
    79  }
    80  
    81  // HasDependenciesV2 returns whether the bundle has v2 dependencies defined.
    82  func (b ExtendedBundle) HasDependenciesV2() bool {
    83  	_, ok := b.Custom[DependenciesV2ExtensionKey]
    84  	return ok
    85  }