Skip to content
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

fix: pushing based on temporary file store path #996

Merged
merged 16 commits into from
Jul 4, 2023
29 changes: 19 additions & 10 deletions cmd/oras/root/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package root
import (
"context"
"fmt"
"os"
"path/filepath"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
Expand All @@ -27,26 +28,24 @@ import (

func loadFiles(ctx context.Context, store *file.Store, annotations map[string]map[string]string, fileRefs []string, verbose bool) ([]ocispec.Descriptor, error) {
var files []ocispec.Descriptor
wd, err := os.Getwd()
if err != nil {
return nil, err
}
for _, fileRef := range fileRefs {
filename, mediaType, err := fileref.Parse(fileRef, "")
path, mediaType, err := fileref.Parse(fileRef, "")
if err != nil {
return nil, err
}

// get shortest absolute path as unique name
name := filepath.Clean(filename)
if !filepath.IsAbs(name) {
name = filepath.ToSlash(name)
}

path, name := getPathName(path, wd)
if verbose {
fmt.Println("Preparing", name)
}
file, err := store.Add(ctx, name, mediaType, filename)
file, err := store.Add(ctx, name, mediaType, path)
if err != nil {
return nil, err
}
if value, ok := annotations[filename]; ok {
if value, ok := annotations[name]; ok {
if file.Annotations == nil {
file.Annotations = value
} else {
Expand All @@ -62,3 +61,13 @@ func loadFiles(ctx context.Context, store *file.Store, annotations map[string]ma
}
return files, nil
}

func getPathName(path string, root string) (string, string) {
qweeah marked this conversation as resolved.
Show resolved Hide resolved
// get shortest relative path as unique name
name := filepath.Clean(path)
if !filepath.IsAbs(name) {
name = filepath.ToSlash(name)
path = filepath.Join(root, path)
}
return path, name
}
14 changes: 13 additions & 1 deletion cmd/oras/root/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"errors"
"fmt"
"os"
"strings"
"sync"

Expand Down Expand Up @@ -134,7 +135,13 @@ func runPush(ctx context.Context, opts pushOptions) error {
ConfigAnnotations: annotations[option.AnnotationConfig],
ManifestAnnotations: annotations[option.AnnotationManifest],
}
store, err := file.New("")

tmp, err := os.MkdirTemp("", "oras-push")
qweeah marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
defer os.RemoveAll(tmp)
qweeah marked this conversation as resolved.
Show resolved Hide resolved
store, err := file.New(tmp)
if err != nil {
return err
}
Expand All @@ -144,6 +151,11 @@ func runPush(ctx context.Context, opts pushOptions) error {
if err != nil {
return err
}
wd, err := os.Getwd()
if err != nil {
return err
}
path, _ = getPathName(path, wd)
desc, err := store.Add(ctx, option.AnnotationConfig, cfgMediaType, path)
if err != nil {
return err
Expand Down