Skip to content

Commit

Permalink
Use copy options in push and pull commands (oras-project#400)
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah authored Jun 21, 2022
1 parent 6a93b10 commit 4eabef7
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 1,531 deletions.
113 changes: 0 additions & 113 deletions cmd/oras/cache.go

This file was deleted.

32 changes: 32 additions & 0 deletions cmd/oras/internal/display/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package display

import (
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

// ShortDigest converts the digest of the descriptor to a short form for displaying.
func ShortDigest(desc ocispec.Descriptor) (digestString string) {
digestString = desc.Digest.String()
if err := desc.Digest.Validate(); err == nil {
if algo := desc.Digest.Algorithm(); algo == digest.SHA256 {
digestString = desc.Digest.Encoded()[:12]
}
}
return digestString
}
31 changes: 31 additions & 0 deletions cmd/oras/internal/display/print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package display

import (
"fmt"
"sync"
)

var printLock sync.Mutex

// Print objects to display concurrent-safely
func Print(a ...any) error {
printLock.Lock()
defer printLock.Unlock()
_, err := fmt.Println(a...)
return err
}
15 changes: 12 additions & 3 deletions cmd/oras/internal/option/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -131,7 +132,7 @@ func (opts *Remote) NewRegistry(hostname string, common Common) (reg *remote.Reg
if err != nil {
return nil, err
}
reg.PlainHTTP = opts.PlainHTTP
reg.PlainHTTP = opts.isPlainHttp(reg.Reference.Registry)
if reg.Client, err = opts.authClient(common.Debug); err != nil {
return nil, err
}
Expand All @@ -144,10 +145,18 @@ func (opts *Remote) NewRepository(reference string, common Common) (repo *remote
if err != nil {
return nil, err
}

repo.PlainHTTP = opts.PlainHTTP
repo.PlainHTTP = opts.isPlainHttp(repo.Reference.Registry)
if repo.Client, err = opts.authClient(common.Debug); err != nil {
return nil, err
}
return
}

// isPlainHttp returns the plain http flag for a given regsitry.
func (opts *Remote) isPlainHttp(registry string) bool {
host, _, _ := net.SplitHostPort(registry)
if host == "localhost" || registry == "localhost" {
return true
}
return opts.PlainHTTP
}
15 changes: 15 additions & 0 deletions cmd/oras/internal/option/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,18 @@ func TestRemote_NewRepository(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
}

func TestRemote_isPlainHttp_localhost(t *testing.T) {
opts := Remote{PlainHTTP: false}
got := opts.isPlainHttp("localhost")
if got != true {
t.Fatalf("tls should be disabled when domain is localhost")

}

got = opts.isPlainHttp("localhost:9090")
if got != true {
t.Fatalf("tls should be disabled when domain is localhost")

}
}
Loading

0 comments on commit 4eabef7

Please sign in to comment.