From 70ac79b4f44c9b84d64e1191a33f96c32a01f531 Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang <103478229+wangxiaoxuan273@users.noreply.github.com> Date: Mon, 9 Sep 2024 15:26:30 +0800 Subject: [PATCH] fix: reference in the formatted output of `oras pull` is invalid (#1496) Signed-off-by: Xiaoxuan Wang --- cmd/oras/internal/option/target.go | 22 ++++------------------ test/e2e/suite/command/pull.go | 7 +++++++ 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/cmd/oras/internal/option/target.go b/cmd/oras/internal/option/target.go index b81c52b26..489eeb820 100644 --- a/cmd/oras/internal/option/target.go +++ b/cmd/oras/internal/option/target.go @@ -113,6 +113,8 @@ func (opts *Target) Parse(cmd *cobra.Command) error { } } else { opts.Reference = ref.Reference + ref.Reference = "" + opts.Path = ref.String() } return opts.Remote.Parse(cmd) } @@ -145,15 +147,7 @@ func (opts *Target) newOCIStore() (*oci.Store, error) { } func (opts *Target) newRepository(common Common, logger logrus.FieldLogger) (*remote.Repository, error) { - repo, err := opts.NewRepository(opts.RawReference, common, logger) - if err != nil { - return nil, err - } - tmp := repo.Reference - tmp.Reference = "" - opts.Path = tmp.String() - opts.Reference = repo.Reference.Reference - return repo, nil + return opts.NewRepository(opts.RawReference, common, logger) } // NewTarget generates a new target based on opts. @@ -232,15 +226,7 @@ func (opts *Target) NewReadonlyTarget(ctx context.Context, common Common, logger } return store, nil case TargetTypeRemote: - repo, err := opts.NewRepository(opts.RawReference, common, logger) - if err != nil { - return nil, err - } - tmp := repo.Reference - tmp.Reference = "" - opts.Path = tmp.String() - opts.Reference = repo.Reference.Reference - return repo, nil + return opts.NewRepository(opts.RawReference, common, logger) } return nil, fmt.Errorf("unknown target type: %q", opts.Type) } diff --git a/test/e2e/suite/command/pull.go b/test/e2e/suite/command/pull.go index cb9e7a666..d463cf582 100644 --- a/test/e2e/suite/command/pull.go +++ b/test/e2e/suite/command/pull.go @@ -267,6 +267,13 @@ var _ = Describe("OCI spec 1.1 registry users:", func() { Expect(err).ShouldNot(HaveOccurred()) }) + It("should show correct reference", func() { + tempDir := PrepareTempFiles() + ref := RegistryRef(ZOTHost, ArtifactRepo, foobar.Tag) + out := ORAS("pull", ref, "--format", "go-template={{.reference}}").WithWorkDir(tempDir).Exec().Out.Contents() + Expect(out).To(Equal([]byte("localhost:7000/command/artifacts@sha256:fd6ed2f36b5465244d5dc86cb4e7df0ab8a9d24adc57825099f522fe009a22bb"))) + }) + It("should pull specific platform", func() { ORAS("pull", RegistryRef(ZOTHost, ImageRepo, "multi"), "--platform", "linux/amd64", "-v", "-o", GinkgoT().TempDir()). MatchStatus(multi_arch.LinuxAMD64StateKeys, true, len(multi_arch.LinuxAMD64StateKeys)).Exec()