Skip to content

Commit

Permalink
[PRFix modify command msg]
Browse files Browse the repository at this point in the history
Signed-off-by: Zoey Li <zoeyli@microsoft.com>
  • Loading branch information
lizMSFT committed Aug 23, 2022
1 parent a90612d commit 97c834b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 56 deletions.
37 changes: 3 additions & 34 deletions cmd/oras/blob/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package blob

import (
"github.com/spf13/cobra"
"oras.land/oras/cmd/oras/internal/option"
)

func Cmd() *cobra.Command {
Expand All @@ -26,38 +25,8 @@ func Cmd() *cobra.Command {
Short: "[Preview] Blob operations",
}

cmd.AddCommand(pushCmd())
return cmd
}

func pushCmd() *cobra.Command {
var opts pushBlobOptions
cmd := &cobra.Command{
Use: "push name[@digest] file [flags]",
Short: "[Preview] Push a blob to remote registry",
Long: `[Preview] Push a blob to remote registry
** This command is in preview and under development. **
Example - Push blob "hi.txt":
oras blob push localhost:5000/hello@sha256:9a201d228ebd966211f7d1131be19f152be428bd373a92071c71d8deaf83b3e5 hi.txt
Example - Push blob to the insecure registry:
oras blob push localhost:5000/hello@sha256:9a201d228ebd966211f7d1131be19f152be428bd373a92071c71d8deaf83b3e5 hi.txt --insecure
Example - Push blob to the HTTP registry:
oras blob push localhost:5000/hello@sha256:9a201d228ebd966211f7d1131be19f152be428bd373a92071c71d8deaf83b3e5 hi.txt --plain-http
`,
Args: cobra.ExactArgs(2),
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.ReadPassword()
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.targetRef = args[0]
opts.fileRef = args[1]
return pushBlob(opts)
},
}

option.ApplyFlags(&opts, cmd.Flags())
cmd.AddCommand(
pushCmd(),
)
return cmd
}
39 changes: 31 additions & 8 deletions cmd/oras/blob/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package blob

import (
"fmt"
"strings"

"github.com/spf13/cobra"
"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/file"
Expand All @@ -32,6 +32,35 @@ type pushBlobOptions struct {
targetRef string
}

func pushCmd() *cobra.Command {
var opts pushBlobOptions
cmd := &cobra.Command{
Use: "push <name> file [flags]",
Short: "[Preview] Push a blob to remote registry",
Long: `[Preview] Push a blob to remote registry
** This command is in preview and under development. **
Example - Push blob "hi.txt":
oras blob push localhost:5000/hello hi.txt
Example - Push blob to the insecure registry:
oras blob push localhost:5000/hello hi.txt --insecure
`,
Args: cobra.ExactArgs(2),
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.ReadPassword()
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.targetRef = args[0]
opts.fileRef = args[1]
return pushBlob(opts)
},
}

option.ApplyFlags(&opts, cmd.Flags())
return cmd
}

func pushBlob(opts pushBlobOptions) (err error) {
ctx, _ := opts.SetLoggerLevel()
repo, err := opts.NewRepository(opts.targetRef, opts.Common)
Expand All @@ -44,13 +73,7 @@ func pushBlob(opts pushBlobOptions) (err error) {
if err != nil {
return err
}
defer func() {
if closeErr := rc.Close(); err == nil {
if closeErr != nil && !strings.Contains(closeErr.Error(), "file already closed") {
err = closeErr
}
}
}()
defer rc.Close()

exists, err := repo.Exists(ctx, desc)
if err != nil {
Expand Down
47 changes: 33 additions & 14 deletions internal/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
package file_test

import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand All @@ -31,44 +30,64 @@ import (
func TestFile_PrepareContent(t *testing.T) {
// generate test content
tempDir := t.TempDir()
dirPath := filepath.Join(tempDir, "testdir")
if err := os.MkdirAll(dirPath, 0777); err != nil {
t.Fatal("error calling Mkdir(), error =", err)
}
content := []byte("hello world!")
fileName := "test.txt"
path := filepath.Join(dirPath, fileName)
if err := ioutil.WriteFile(path, content, 0444); err != nil {
path := filepath.Join(tempDir, fileName)
if err := os.WriteFile(path, content, 0444); err != nil {
t.Fatal("error calling WriteFile(), error =", err)
}

blobMediaType := "application/octet-stream"
wantDesc := ocispec.Descriptor{
want := ocispec.Descriptor{
MediaType: blobMediaType,
Digest: digest.FromBytes(content),
Size: int64(len(content)),
}

// test PrepareContent
gotDesc, rc, err := file.PrepareContent(path, blobMediaType)
got, rc, err := file.PrepareContent(path, blobMediaType)
defer rc.Close()
if err != nil {
t.Fatal("PrepareContent() error=", err)
}
if !reflect.DeepEqual(gotDesc, wantDesc) {
t.Errorf("PrepareContent() = %v, want %v", gotDesc, wantDesc)
if !reflect.DeepEqual(got, want) {
t.Errorf("PrepareContent() = %v, want %v", got, want)
}
}

func TestFile_PrepareContent_errMissingFileName(t *testing.T) {
// generate test content
tempDir := t.TempDir()
content := []byte("hello world!")
fileName := "test.txt"
path := filepath.Join(tempDir, fileName)
if err := os.WriteFile(path, content, 0444); err != nil {
t.Fatal("error calling WriteFile(), error =", err)
}
blobMediaType := "application/octet-stream"

// test PrepareContent with missing file name
_, _, err = file.PrepareContent("", blobMediaType)
_, _, err := file.PrepareContent("", blobMediaType)
expected := "missing file name"
if err.Error() != expected {
t.Fatalf("PrepareContent() error = %v, wantErr %v", err, expected)
}
}

func TestFile_PrepareContent_errOpenFile(t *testing.T) {
// generate test content
tempDir := t.TempDir()
content := []byte("hello world!")
fileName := "test.txt"
path := filepath.Join(tempDir, fileName)
if err := os.WriteFile(path, content, 0444); err != nil {
t.Fatal("error calling WriteFile(), error =", err)
}
blobMediaType := "application/octet-stream"

// test PrepareContent with nonexistent file
_, _, err = file.PrepareContent("nonexistent.txt", blobMediaType)
expected = "failed to open nonexistent.txt"
_, _, err := file.PrepareContent("nonexistent.txt", blobMediaType)
expected := "failed to open nonexistent.txt"
if !strings.Contains(err.Error(), expected) {
t.Fatalf("PrepareContent() error = %v, wantErr %v", err, expected)
}
Expand Down

0 comments on commit 97c834b

Please sign in to comment.