Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow dependencies to be pinned to a git ref #447

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ IMPROVEMENTS:
* cli: `registry list` command now shows git refs to repositories present in the cache [[GH-318](https://github.com/hashicorp/nomad-pack/pull/318)]
* cli: `registry list` command now shows only registries, and a new command `list` shows packs [[GH-337](https://github.com/hashicorp/nomad-pack/pull/337)], [[GH-373](https://github.com/hashicorp/nomad-pack/pull/373)]
* cli: `deps vendor` command [[GH-367](https://github.com/hashicorp/nomad-pack/pull/367)]
* cli: `deps vendor` command now allows dependencies to be pinned [[GH-447](https://github.com/hashicorp/nomad-pack/pull/447)]
* cli: `generate pack` command now supports `--overwrite` flag [[GH-380](https://github.com/hashicorp/nomad-pack/pull/380)]
* cli: `registry add` command now uses shallow cloning [[GH-444](https://github.com/hashicorp/nomad-pack/pull/444)]
* deps: Update the Nomad OpenAPI dependency; require Go 1.18 as a build dependency [[GH-288](https://github.com/hashicorp/nomad-pack/pull/288)]
Expand Down
10 changes: 9 additions & 1 deletion internal/pkg/deps/vendor.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@ func Vendor(ctx context.Context, ui terminal.UI, targetPath string) error {

for _, d := range metadata.Dependencies {
targetDir := path.Join(targetPath, "deps", d.Name)
url := d.Source

if !d.IsLatest() {
url = fmt.Sprintf("%s?ref=%s", url, d.Ref)
} else {
// Attempt to shallow clone the constructed url
url = fmt.Sprintf("%s?depth=1", url)
}

// download each dependency
ui.Info(fmt.Sprintf("downloading %v pack to %v...", d.Name, targetDir))
if err := gg.Get(targetDir, fmt.Sprintf(d.Source), gg.WithContext(ctx)); err != nil {
if err := gg.Get(targetDir, url, gg.WithContext(ctx)); err != nil {
return fmt.Errorf("error downloading dependency: %v", err)
}
ui.Success("...success!")
Expand Down
9 changes: 9 additions & 0 deletions sdk/pack/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type Dependency struct {
// variable values.
Alias string `hcl:"alias,optional"`

// Ref is the git reference of the pack at which to add. Ignored if not
// specifying a git source. Defaults to latest.
Ref string `hcl:"ref,optional"`

// Source is the remote source where the pack can be fetched. This string
// can follow any format as supported by go-getter or be a local path
// indicating the pack has already been downloaded.
Expand All @@ -45,6 +49,11 @@ func (d *Dependency) ID() ID {
return ID(d.AliasOrName())
}

// IsLatest works out if the user requested the HEAD of the dependency
func (d *Dependency) IsLatest() bool {
return d.Ref == "" || d.Ref == "latest"
}

// validate the Dependency object to ensure it meets requirements and doesn't
// contain invalid or incorrect data.
func (d *Dependency) validate() error {
Expand Down