Skip to content

Commit

Permalink
Adds strip-components field to cargo
Browse files Browse the repository at this point in the history
- This means that the field will be perserved when running jam pack
  • Loading branch information
ForestEckhardt committed Aug 23, 2022
1 parent 5c8e862 commit 8bb254b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
35 changes: 35 additions & 0 deletions cargo/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type ConfigMetadataDependency struct {
Source string `toml:"source" json:"source,omitempty"`
SourceSHA256 string `toml:"source_sha256" json:"source_sha256,omitempty"`
Stacks []string `toml:"stacks" json:"stacks,omitempty"`
StripComponents int `toml:"strip-components" json:"strip-components,omitempty"`
URI string `toml:"uri" json:"uri,omitempty"`
Version string `toml:"version" json:"version,omitempty"`
}
Expand Down Expand Up @@ -96,6 +97,11 @@ func EncodeConfig(writer io.Writer, config Config) error {
return err
}

c, err = convertStripComponents(config.Metadata.Dependencies, c)
if err != nil {
return err
}

return toml.NewEncoder(writer).Encode(c)
}

Expand Down Expand Up @@ -240,3 +246,32 @@ func convertPatches(constraints []ConfigMetadataDependencyConstraint, c map[stri
}
return c, nil
}

// Accomplishes the same this as the convertPatches function but for strip components in the dependencies list.
func convertStripComponents(dependencies []ConfigMetadataDependency, c map[string]interface{}) (map[string]interface{}, error) {
if len(dependencies) > 0 {
metadata, ok := c["metadata"].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("failure to assert type: unexpected data in metadata")
}

dependencies, ok := metadata["dependencies"].([]interface{})
if !ok {
return nil, fmt.Errorf("failure to assert type: unexpected data in constraints")
}

for _, dependency := range dependencies {
stripComponents, ok := dependency.(map[string]interface{})["strip-components"]
if !ok {
return c, nil
}

floatStripComponents, ok := stripComponents.(float64)
if !ok {
return nil, fmt.Errorf("failure to assert type: unexpected data")
}
dependency.(map[string]interface{})["strip-components"] = int(floatStripComponents)
}
}
return c, nil
}
26 changes: 15 additions & 11 deletions cargo/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func testConfig(t *testing.T, context spec.G, it spec.S) {
Source: "source",
SourceSHA256: "source-shasum",
Stacks: []string{"io.buildpacks.stacks.bionic", "org.cloudfoundry.stacks.tiny"},
StripComponents: 1,
URI: "http://some-url",
Version: "1.2.3",
},
Expand Down Expand Up @@ -142,6 +143,7 @@ api = "0.6"
source = "source"
source_sha256 = "source-shasum"
stacks = ["io.buildpacks.stacks.bionic", "org.cloudfoundry.stacks.tiny"]
strip-components = 1
uri = "http://some-url"
version = "1.2.3"
Expand Down Expand Up @@ -402,6 +404,7 @@ api = "0.6"
source = "source"
source_sha256 = "source-shasum"
stacks = ["io.buildpacks.stacks.bionic", "org.cloudfoundry.stacks.tiny"]
strip-components = 1
uri = "http://some-url"
version = "1.2.3"
Expand Down Expand Up @@ -466,17 +469,18 @@ api = "0.6"
PrePackage: "some-pre-package-script.sh",
Dependencies: []cargo.ConfigMetadataDependency{
{
CPE: "some-cpe",
PURL: "some-purl",
ID: "some-dependency",
Licenses: []interface{}{"fancy-license", "fancy-license-2"},
Name: "Some Dependency",
SHA256: "shasum",
Source: "source",
SourceSHA256: "source-shasum",
Stacks: []string{"io.buildpacks.stacks.bionic", "org.cloudfoundry.stacks.tiny"},
URI: "http://some-url",
Version: "1.2.3",
CPE: "some-cpe",
PURL: "some-purl",
ID: "some-dependency",
Licenses: []interface{}{"fancy-license", "fancy-license-2"},
Name: "Some Dependency",
SHA256: "shasum",
Source: "source",
SourceSHA256: "source-shasum",
Stacks: []string{"io.buildpacks.stacks.bionic", "org.cloudfoundry.stacks.tiny"},
StripComponents: 1,
URI: "http://some-url",
Version: "1.2.3",
},
},
DependencyConstraints: []cargo.ConfigMetadataDependencyConstraint{
Expand Down

0 comments on commit 8bb254b

Please sign in to comment.