From d11b379d78c2d28f495606a6bdf4282aa568a77a Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Fri, 18 Aug 2023 08:21:27 +0000 Subject: [PATCH] deduplicated based on warning value Signed-off-by: Billy Zha --- cmd/oras/internal/option/remote.go | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/cmd/oras/internal/option/remote.go b/cmd/oras/internal/option/remote.go index 7d839f0cb..a2c75578c 100644 --- a/cmd/oras/internal/option/remote.go +++ b/cmd/oras/internal/option/remote.go @@ -54,6 +54,7 @@ type Remote struct { distributionSpec distributionSpec headerFlags []string headers http.Header + hasWarned sync.Map } // EnableDistributionSpecFlag set distribution specification flag as applicable. @@ -257,12 +258,9 @@ func (opts *Remote) NewRegistry(hostname string, warn func(...interface{}), comm hostname = reg.Reference.Registry reg.PlainHTTP = opts.isPlainHttp(hostname) - if opts.distributionSpec.referrersAPI == nil || *opts.distributionSpec.referrersAPI { - once := sync.Once{} - reg.HandleWarning = func(warning remote.Warning) { - once.Do(func() { - warn(warning.Text) - }) + reg.HandleWarning = func(warning remote.Warning) { + if _, ok := opts.hasWarned.LoadOrStore(warning.WarningValue, true); ok { + warn(warning.Text) } } if reg.Client, err = opts.authClient(hostname, common.Debug); err != nil { @@ -283,16 +281,11 @@ func (opts *Remote) NewRepository(reference string, warn func(...interface{}), c if repo.Client, err = opts.authClient(hostname, common.Debug); err != nil { return nil, err } - - if opts.distributionSpec.referrersAPI == nil || *opts.distributionSpec.referrersAPI { - once := sync.Once{} - repo.HandleWarning = func(warning remote.Warning) { - once.Do(func() { - warn(warning.Text) - }) + repo.HandleWarning = func(warning remote.Warning) { + if _, loaded := opts.hasWarned.LoadOrStore(warning.WarningValue, true); loaded { + warn(hostname, warning.Text) } } - if opts.distributionSpec.referrersAPI != nil { if err := repo.SetReferrersCapability(*opts.distributionSpec.referrersAPI); err != nil { return nil, err