Skip to content

Commit

Permalink
Cross-platform support on path (oras-project#65)
Browse files Browse the repository at this point in the history
- Parse paths wisely for unix and win
    - Support disk letter `C:\` on win
- Paths are auto-cleaned on CLI
  • Loading branch information
shizhMSFT authored Apr 15, 2019
1 parent c71f94e commit 8f6cd04
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
21 changes: 8 additions & 13 deletions cmd/oras/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"os"
"strings"
"path/filepath"

"github.com/deislabs/oras/pkg/content"
"github.com/deislabs/oras/pkg/oras"
Expand Down Expand Up @@ -92,12 +92,7 @@ func runPush(opts pushOptions) error {
}
}
if opts.manifestConfigRef != "" {
ref := strings.SplitN(opts.manifestConfigRef, ":", 2)
filename := ref[0]
mediaType := ocispec.MediaTypeImageConfig
if len(ref) == 2 {
mediaType = ref[1]
}
filename, mediaType := parseFileRef(opts.manifestConfigRef, ocispec.MediaTypeImageConfig)
file, err := store.Add(annotationConfig, mediaType, filename)
if err != nil {
return err
Expand All @@ -106,13 +101,13 @@ func runPush(opts pushOptions) error {
pushOpts = append(pushOpts, oras.WithConfig(file))
}
for _, fileRef := range opts.fileRefs {
ref := strings.SplitN(fileRef, ":", 2)
filename := ref[0]
var mediaType string
if len(ref) == 2 {
mediaType = ref[1]
filename, mediaType := parseFileRef(fileRef, "")
name := filepath.Clean(filename)
if !filepath.IsAbs(name) {
// convert to slash-separated path unless it is absolute path
name = filepath.ToSlash(name)
}
file, err := store.Add(filename, mediaType, "")
file, err := store.Add(name, mediaType, filename)
if err != nil {
return err
}
Expand Down
13 changes: 13 additions & 0 deletions cmd/oras/push_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// +build !windows

package main

import "strings"

func parseFileRef(ref string, mediaType string) (string, string) {
i := strings.LastIndex(ref, ":")
if i < 0 {
return ref, mediaType
}
return ref[:i], ref[i+1:]
}
26 changes: 26 additions & 0 deletions cmd/oras/push_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"strings"
"unicode"
)

// parseFileRef parse file reference on windows.
// Windows systems does not allow ':' in the file path except for drive letter.
func parseFileRef(ref string, mediaType string) (string, string) {
i := strings.Index(ref, ":")
if i < 0 {
return ref, mediaType
}

// In case it is C:\
if i == 1 && len(ref) > 2 && ref[2] == '\\' && unicode.IsLetter(rune(ref[0])) {
i = strings.Index(ref[3:], ":")
if i < 0 {
return ref, mediaType
}
i += 3
}

return ref[:i], ref[i+1:]
}

0 comments on commit 8f6cd04

Please sign in to comment.