-
Notifications
You must be signed in to change notification settings - Fork 347
added targets flag for buildpack new cli #1921
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d543d17
added targets flag for buildpack new cli
ep0ll 96b28c3
added format to add --targets from cli
ep0ll 057f53d
refactored code to receive flags from []string for --targets flag
ep0ll 16b8350
fix: bug of distro not being added to toml file
ep0ll e79226e
added tests & show error msg when unknow target is passed
ep0ll ca5d09f
fixed tests
ep0ll 90c2195
removed persistent flag
ep0ll 210757c
refactored code & still need to fix bugs
ep0ll 77dac7f
fix bugs
ep0ll 184f6c3
added warns to targets flag
ep0ll 9a5036c
fix tests
ep0ll e7902f8
lint fix
ep0ll cdd9417
will not be added to when is defined and not defined
ep0ll d84b81f
Merge branch 'main' into buildpack-new-targets-flag
jkutner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import ( | |
| "bytes" | ||
| "os" | ||
| "path/filepath" | ||
| "runtime" | ||
| "testing" | ||
|
|
||
| "github.com/buildpacks/pack/pkg/client" | ||
|
|
@@ -36,6 +37,10 @@ func testBuildpackNewCommand(t *testing.T, when spec.G, it spec.S) { | |
| mockClient *testmocks.MockPackClient | ||
| tmpDir string | ||
| ) | ||
| targets := []dist.Target{{ | ||
| OS: runtime.GOOS, | ||
| Arch: runtime.GOARCH, | ||
| }} | ||
|
|
||
| it.Before(func() { | ||
| var err error | ||
|
|
@@ -60,10 +65,7 @@ func testBuildpackNewCommand(t *testing.T, when spec.G, it spec.S) { | |
| ID: "example/some-cnb", | ||
| Path: filepath.Join(tmpDir, "some-cnb"), | ||
| Version: "1.0.0", | ||
| Stacks: []dist.Stack{{ | ||
| ID: "io.buildpacks.stacks.jammy", | ||
| Mixins: []string{}, | ||
| }}, | ||
| Targets: targets, | ||
| }).Return(nil).MaxTimes(1) | ||
|
|
||
| path := filepath.Join(tmpDir, "some-cnb") | ||
|
|
@@ -82,5 +84,122 @@ func testBuildpackNewCommand(t *testing.T, when spec.G, it spec.S) { | |
| h.AssertNotNil(t, err) | ||
| h.AssertContains(t, outBuf.String(), "ERROR: directory") | ||
| }) | ||
|
|
||
| when("target flag is specified, ", func() { | ||
| it("it uses target to generate artifacts", func() { | ||
| mockClient.EXPECT().NewBuildpack(gomock.Any(), client.NewBuildpackOptions{ | ||
| API: "0.8", | ||
| ID: "example/targets", | ||
| Path: filepath.Join(tmpDir, "targets"), | ||
| Version: "1.0.0", | ||
| Targets: []dist.Target{{ | ||
| OS: "linux", | ||
| Arch: "arm", | ||
| ArchVariant: "v6", | ||
| Distributions: []dist.Distribution{{ | ||
| Name: "ubuntu", | ||
| Versions: []string{"14.04", "16.04"}, | ||
| }}, | ||
| }}, | ||
| }).Return(nil).MaxTimes(1) | ||
|
|
||
| path := filepath.Join(tmpDir, "targets") | ||
| command.SetArgs([]string{"--path", path, "example/targets", "--targets", "linux/arm/v6:ubuntu@14.04@16.04"}) | ||
|
|
||
| err := command.Execute() | ||
| h.AssertNil(t, err) | ||
| }) | ||
| it("it should show error when invalid [os]/[arch] passed", func() { | ||
| mockClient.EXPECT().NewBuildpack(gomock.Any(), client.NewBuildpackOptions{ | ||
| API: "0.8", | ||
| ID: "example/targets", | ||
| Path: filepath.Join(tmpDir, "targets"), | ||
| Version: "1.0.0", | ||
| Targets: []dist.Target{{ | ||
| OS: "os", | ||
| Arch: "arm", | ||
| ArchVariant: "v6", | ||
| Distributions: []dist.Distribution{{ | ||
| Name: "ubuntu", | ||
| Versions: []string{"14.04", "16.04"}, | ||
| }}, | ||
| }}, | ||
| }).Return(nil).MaxTimes(1) | ||
|
|
||
| path := filepath.Join(tmpDir, "targets") | ||
| command.SetArgs([]string{"--path", path, "example/targets", "--targets", "os/arm/v6:ubuntu@14.04@16.04"}) | ||
|
|
||
| err := command.Execute() | ||
| h.AssertNotNil(t, err) | ||
| }) | ||
| when("it should", func() { | ||
| it("support format [os][/arch][/variant]:[name@version@version2];[some-name@version@version2]", func() { | ||
| mockClient.EXPECT().NewBuildpack(gomock.Any(), client.NewBuildpackOptions{ | ||
| API: "0.8", | ||
| ID: "example/targets", | ||
| Path: filepath.Join(tmpDir, "targets"), | ||
| Version: "1.0.0", | ||
| Targets: []dist.Target{ | ||
| { | ||
| OS: "linux", | ||
| Arch: "arm", | ||
| ArchVariant: "v6", | ||
| Distributions: []dist.Distribution{ | ||
| { | ||
| Name: "ubuntu", | ||
| Versions: []string{"14.04", "16.04"}, | ||
| }, | ||
| { | ||
| Name: "debian", | ||
| Versions: []string{"8.10", "10.9"}, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| OS: "windows", | ||
| Arch: "amd64", | ||
| Distributions: []dist.Distribution{ | ||
| { | ||
| Name: "windows-nano", | ||
| Versions: []string{"10.0.19041.1415"}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }).Return(nil).MaxTimes(1) | ||
|
|
||
| path := filepath.Join(tmpDir, "targets") | ||
| command.SetArgs([]string{"--path", path, "example/targets", "--targets", "linux/arm/v6:ubuntu@14.04@16.04;debian@8.10@10.9", "-t", "windows/amd64:windows-nano@10.0.19041.1415"}) | ||
|
|
||
| err := command.Execute() | ||
| h.AssertNil(t, err) | ||
| }) | ||
| }) | ||
| when("stacks ", func() { | ||
| it("flag should show deprecated message when used", func() { | ||
| mockClient.EXPECT().NewBuildpack(gomock.Any(), client.NewBuildpackOptions{ | ||
| API: "0.8", | ||
| ID: "example/stacks", | ||
| Path: filepath.Join(tmpDir, "stacks"), | ||
| Version: "1.0.0", | ||
| Stacks: []dist.Stack{{ | ||
| ID: "io.buildpacks.stacks.jammy", | ||
| Mixins: []string{}, | ||
| }}, | ||
| Targets: targets, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on my comment here, I will remove this
|
||
| }).Return(nil).MaxTimes(1) | ||
|
|
||
| path := filepath.Join(tmpDir, "stacks") | ||
| output := new(bytes.Buffer) | ||
| command.SetOut(output) | ||
| command.SetErr(output) | ||
| command.SetArgs([]string{"--path", path, "example/stacks", "--stacks", "io.buildpacks.stacks.jammy"}) | ||
|
|
||
| err := command.Execute() | ||
| h.AssertNil(t, err) | ||
| h.AssertContains(t, output.String(), "Flag --stacks has been deprecated,") | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package target | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/pkg/errors" | ||
|
|
||
| "github.com/buildpacks/pack/internal/style" | ||
| "github.com/buildpacks/pack/internal/warn" | ||
| "github.com/buildpacks/pack/pkg/dist" | ||
| ) | ||
|
|
||
| func ParseTargets(t []string) (targets []dist.Target, warn warn.Warn, err error) { | ||
| for _, v := range t { | ||
| target, w, err := ParseTarget(v) | ||
| warn.AddWarn(w) | ||
| if err != nil { | ||
| return nil, warn, err | ||
| } | ||
| targets = append(targets, target) | ||
| } | ||
| return targets, warn, nil | ||
| } | ||
|
|
||
| func ParseTarget(t string) (output dist.Target, warn warn.Warn, err error) { | ||
| nonDistro, distros, w, err := getTarget(t) | ||
| warn.AddWarn(w) | ||
| if v, _ := getSliceAt[string](nonDistro, 0); len(nonDistro) <= 1 && v == "" { | ||
| warn.Add(style.Error("os/arch must be defined")) | ||
| } | ||
| if err != nil { | ||
| return output, warn, err | ||
| } | ||
| os, arch, variant, w, err := getPlatform(nonDistro) | ||
| warn.AddWarn(w) | ||
| if err != nil { | ||
| return output, warn, err | ||
| } | ||
| v, w, err := ParseDistros(distros) | ||
| warn.AddWarn(w) | ||
| if err != nil { | ||
| return output, warn, err | ||
| } | ||
| output = dist.Target{ | ||
| OS: os, | ||
| Arch: arch, | ||
| ArchVariant: variant, | ||
| Distributions: v, | ||
| } | ||
| return output, warn, err | ||
| } | ||
|
|
||
| func ParseDistros(distroSlice string) (distros []dist.Distribution, warn warn.Warn, err error) { | ||
| distro := strings.Split(distroSlice, ";") | ||
| if l := len(distro); l == 1 && distro[0] == "" { | ||
| return nil, warn, err | ||
| } | ||
| for _, d := range distro { | ||
| v, w, err := ParseDistro(d) | ||
| warn.AddWarn(w) | ||
| if err != nil { | ||
| return nil, warn, err | ||
| } | ||
| distros = append(distros, v) | ||
| } | ||
| return distros, warn, nil | ||
| } | ||
|
|
||
| func ParseDistro(distroString string) (distro dist.Distribution, warn warn.Warn, err error) { | ||
| d := strings.Split(distroString, "@") | ||
| if d[0] == "" || len(d) == 0 { | ||
| return distro, warn, errors.Errorf("distro's versions %s cannot be specified without distro's name", style.Symbol("@"+strings.Join(d[1:], "@"))) | ||
| } | ||
| if len(d) <= 2 && d[0] == "" { | ||
| warn.Add(fmt.Sprintf("distro with name %s has no specific version!", style.Symbol(d[0]))) | ||
| } | ||
| distro.Name = d[0] | ||
| distro.Versions = d[1:] | ||
| return distro, warn, err | ||
| } | ||
|
|
||
| func getTarget(t string) (nonDistro []string, distros string, warn warn.Warn, err error) { | ||
| target := strings.Split(t, ":") | ||
| if _, err := getSliceAt[string](target, 0); err != nil { | ||
| return nonDistro, distros, warn, errors.Errorf("invalid target %s, atleast one of [os][/arch][/archVariant] must be specified", t) | ||
| } | ||
| if len(target) == 2 && target[0] == "" { | ||
| v, _ := getSliceAt[string](target, 1) | ||
| warn.Add(style.Warn("adding distros %s without [os][/arch][/variant]", v)) | ||
| } else { | ||
| i, _ := getSliceAt[string](target, 0) | ||
| nonDistro = strings.Split(i, "/") | ||
| } | ||
| if i, err := getSliceAt[string](target, 1); err == nil { | ||
| distros = i | ||
| } | ||
| return nonDistro, distros, warn, err | ||
| } | ||
|
|
||
| func getSliceAt[T interface{}](slice []T, index int) (value T, err error) { | ||
| if index < 0 || index >= len(slice) { | ||
| return value, errors.Errorf("index out of bound, cannot access item at index %d of slice with length %d", index, len(slice)) | ||
| } | ||
|
|
||
| return slice[index], err | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.