Skip to content

Commit

Permalink
Modify the jam update-dependencies command to work with metadata file
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Casey <[email protected]>
  • Loading branch information
Sophie Wigmore authored and ForestEckhardt committed Aug 24, 2022
1 parent 1b81840 commit 51c924f
Show file tree
Hide file tree
Showing 28 changed files with 1,633 additions and 27 deletions.
98 changes: 71 additions & 27 deletions commands/update_dependencies.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package commands

import (
"encoding/json"
"fmt"
"os"
"reflect"

"github.com/paketo-buildpacks/jam/internal"
"github.com/paketo-buildpacks/packit/v2/cargo"
Expand All @@ -12,19 +14,21 @@ import (
type updateDependenciesFlags struct {
buildpackFile string
api string
metadataFile string
}

func updateDependencies() *cobra.Command {
flags := &updateDependenciesFlags{}
cmd := &cobra.Command{
Use: "update-dependencies",
Short: "updates all depdendencies in a buildpack.toml according to metadata.constraints",
Short: "updates all depdendencies in a buildpack.toml either with metadata from a file or from an API.",
RunE: func(cmd *cobra.Command, args []string) error {
return updateDependenciesRun(*flags)
},
}
cmd.Flags().StringVar(&flags.buildpackFile, "buildpack-file", "", "path to the buildpack.toml file (required)")
cmd.Flags().StringVar(&flags.api, "api", "https://api.deps.paketo.io", "api to query for dependencies")
cmd.Flags().StringVar(&flags.metadataFile, "metadata-file", "", "takes precedence over the API argument, metadata.json file with all entries to be added to the buildpack.toml")

err := cmd.MarkFlagRequired("buildpack-file")
if err != nil {
Expand All @@ -51,43 +55,83 @@ func updateDependenciesRun(flags updateDependenciesFlags) error {

api := flags.api

// All internal.Dependencies from the dep-server
allDependencies := map[string][]internal.Dependency{}
// All cargo.ConfigMetadataDependencies that match one of the given constraints
var matchingDependencies []cargo.ConfigMetadataDependency

for _, constraint := range config.Metadata.DependencyConstraints {
// Only query the API once per unique dependency
dependencies, ok := allDependencies[constraint.ID]
if !ok {
var err error
dependencies, err = internal.GetAllDependencies(api, constraint.ID)
// if a metadata file is provided, use that as the version and metadata
// source
if flags.metadataFile != "" {
var matchingDependencies []cargo.ConfigMetadataDependency

metadataFile, err := os.Open(flags.metadataFile)
if err != nil {
return fmt.Errorf("failed to open metadata.json file: %w", err)
}

newVersions := []cargo.ConfigMetadataDependency{}
err = json.NewDecoder(metadataFile).Decode(&newVersions)
if err != nil {
return fmt.Errorf("failed decode metadata.json: %w", err)
}
err = metadataFile.Close()
if err != nil {
//untested
return fmt.Errorf("failed close metadata.json: %w", err)
}

// combine buildpack.toml versions and new versions
allDependencies := append(config.Metadata.Dependencies, newVersions...)

for _, constraint := range config.Metadata.DependencyConstraints {
// Filter allDependencies for only those that match the constraint
// mds is a just the right number of deps for the constraint
mds, err := internal.GetCargoDependenciesWithinConstraint(allDependencies, constraint)
if err != nil {
return err
}
allDependencies[constraint.ID] = dependencies

matchingDependencies = append(matchingDependencies, mds...)
if len(matchingDependencies) > 0 {
config.Metadata.Dependencies = matchingDependencies
}
}
} else {
// All internal.Dependencies from the dep-server
allDependencies := map[string][]internal.Dependency{}
// All cargo.ConfigMetadataDependencies that match one of the given constraints
var matchingDependencies []cargo.ConfigMetadataDependency

for _, constraint := range config.Metadata.DependencyConstraints {
// Only query the API once per unique dependency
dependencies, ok := allDependencies[constraint.ID]
if !ok {
var err error
dependencies, err = internal.GetAllDependencies(api, constraint.ID)
if err != nil {
return err
}
allDependencies[constraint.ID] = dependencies
}

// Manually lookup the existent dependency name from the buildpack.toml since this
// isn't specified via the dep-server
dependencyName := internal.FindDependencyName(constraint.ID, config)
// Manually lookup the existent dependency name from the buildpack.toml since this
// isn't specified via the dep-server
dependencyName := internal.FindDependencyName(constraint.ID, config)

// Filter allDependencies for only those that match the constraint
mds, err := internal.GetDependenciesWithinConstraint(dependencies, constraint, dependencyName)
if err != nil {
return err
// Filter allDependencies for only those that match the constraint
// mds is a just the right number of deps for the constraint
mds, err := internal.GetDependenciesWithinConstraint(dependencies, constraint, dependencyName)
if err != nil {
return err
}
matchingDependencies = append(matchingDependencies, mds...)
}
matchingDependencies = append(matchingDependencies, mds...)
}

if len(matchingDependencies) > 0 {
config.Metadata.Dependencies = matchingDependencies
if len(matchingDependencies) > 0 {
config.Metadata.Dependencies = matchingDependencies
}
}

newVersions := []string{}
newVersions := map[string]string{}
for _, d := range config.Metadata.Dependencies {
if _, ok := originalVersions[d.Version]; !ok {
newVersions = append(newVersions, d.Version)
newVersions[d.Version] = ""
}
}

Expand All @@ -102,7 +146,7 @@ func updateDependenciesRun(flags updateDependenciesFlags) error {
return fmt.Errorf("failed to write buildpack config: %w", err)
}

fmt.Println("Updating buildpack.toml with new versions: ", newVersions)
fmt.Println("Updating buildpack.toml with new versions: ", reflect.ValueOf(newVersions).MapKeys())

return nil
}
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ require (
github.com/google/go-containerregistry v0.11.0
github.com/moby/buildkit v0.9.3
github.com/onsi/gomega v1.20.0
github.com/paketo-buildpacks/occam v0.12.1
github.com/paketo-buildpacks/packit/v2 v2.4.2
github.com/pelletier/go-toml v1.9.5
github.com/sclevine/spec v1.4.0
github.com/spf13/cobra v1.5.0
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
)

require (
Expand All @@ -31,6 +33,7 @@ require (
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/CycloneDX/cyclonedx-go v0.5.2 // indirect
github.com/ForestEckhardt/freezer v0.0.11 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
Expand Down Expand Up @@ -59,6 +62,7 @@ require (
github.com/bmatcuk/doublestar/v4 v4.0.2 // indirect
github.com/buildpacks/imgutil v0.0.0-20220527150729-7a271a852e31 // indirect
github.com/buildpacks/lifecycle v0.14.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/chrismellard/docker-credential-acr-env v0.0.0-20220327082430-c57b701bfc08 // indirect
github.com/containerd/cgroups v1.0.3 // indirect
github.com/containerd/console v1.0.3 // indirect
Expand Down Expand Up @@ -101,6 +105,7 @@ require (
github.com/klauspost/compress v1.15.8 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect
github.com/knqyf263/go-rpmdb v0.0.0-20220629110411-9a3bd2ebb923 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
Expand All @@ -118,6 +123,7 @@ require (
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/nwaples/rardecode v1.1.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.3-0.20220114050600-8b9d41f48198 // indirect
Expand All @@ -135,6 +141,7 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/sylabs/sif/v2 v2.7.0 // indirect
github.com/sylabs/squashfs v0.5.5-0.20220803150326-9393a0b4cef5 // indirect
github.com/testcontainers/testcontainers-go v0.13.0 // indirect
github.com/therootcompany/xz v1.0.1 // indirect
github.com/tonistiigi/fsutil v0.0.0-20210609172227-d72af97c0eaf // indirect
github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea // indirect
Expand Down
Loading

0 comments on commit 51c924f

Please sign in to comment.