-
Notifications
You must be signed in to change notification settings - Fork 779
layout: implement unpacking and validation #82
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| code-of-conduct.md | ||
| oci-image-tool | ||
| oci-validate-examples | ||
| /oci-image-tool | ||
| /oci-validate-examples | ||
| output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Copyright 2016 The Linux Foundation | ||
| // | ||
| // 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 main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "io" | ||
| "io/ioutil" | ||
| "net/http" | ||
| "os" | ||
|
|
||
| "github.com/opencontainers/image-spec/schema" | ||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| // supported autodetection types | ||
| const ( | ||
| typeImageLayout = "imageLayout" | ||
| typeImage = "image" | ||
| typeManifest = "manifest" | ||
| typeManifestList = "manifestList" | ||
| typeConfig = "config" | ||
| ) | ||
|
|
||
| // autodetect detects the validation type for the given path | ||
| // or an error if the validation type could not be resolved. | ||
| func autodetect(path string) (string, error) { | ||
| fi, err := os.Stat(path) | ||
| if err != nil { | ||
| return "", errors.Wrapf(err, "unable to access path") // err from os.Stat includes path name | ||
| } | ||
|
|
||
| if fi.IsDir() { | ||
| return typeImageLayout, nil | ||
| } | ||
|
|
||
| f, err := os.Open(path) | ||
| if err != nil { | ||
| return "", errors.Wrap(err, "unable to open file") // os.Open includes the filename | ||
| } | ||
| defer f.Close() | ||
|
|
||
| buf, err := ioutil.ReadAll(io.LimitReader(f, 512)) // read some initial bytes to detect content | ||
| if err != nil { | ||
| return "", errors.Wrap(err, "unable to read") | ||
| } | ||
|
|
||
| mimeType := http.DetectContentType(buf) | ||
|
|
||
| switch mimeType { | ||
| case "application/x-gzip": | ||
| return typeImage, nil | ||
|
|
||
| case "application/octet-stream": | ||
| return typeImage, nil | ||
|
|
||
| case "text/plain; charset=utf-8": | ||
| // might be a JSON file, will be handled below | ||
|
|
||
| default: | ||
| return "", errors.New("unknown file type") | ||
| } | ||
|
|
||
| if _, err := f.Seek(0, os.SEEK_SET); err != nil { | ||
| return "", errors.Wrap(err, "unable to seek") | ||
| } | ||
|
|
||
| header := struct { | ||
| SchemaVersion int `json:"schemaVersion"` | ||
| MediaType string `json:"mediaType"` | ||
| Config interface{} `json:"config"` | ||
| }{} | ||
|
|
||
| if err := json.NewDecoder(f).Decode(&header); err != nil { | ||
| return "", errors.Wrap(err, "unable to parse JSON") | ||
| } | ||
|
|
||
| switch { | ||
| case header.MediaType == string(schema.MediaTypeManifest): | ||
| return typeManifest, nil | ||
|
|
||
| case header.MediaType == string(schema.MediaTypeManifestList): | ||
| return typeManifestList, nil | ||
|
|
||
| case header.MediaType == "" && header.SchemaVersion == 0 && header.Config != nil: | ||
| // config files don't have mediaType/schemaVersion header | ||
| return typeConfig, nil | ||
| } | ||
|
|
||
| return "", errors.New("unknown media type") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Copyright 2016 The Linux Foundation | ||
| // | ||
| // 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 main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/opencontainers/image-spec/image" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // supported unpack types | ||
| var unpackTypes = []string{ | ||
| typeImageLayout, | ||
| typeImage, | ||
| } | ||
|
|
||
| type unpackCmd struct { | ||
| stdout *log.Logger | ||
| stderr *log.Logger | ||
| typ string // the type to validate, can be empty string | ||
| ref string | ||
| } | ||
|
|
||
| func newUnpackCmd(stdout, stderr *log.Logger) *cobra.Command { | ||
| v := &unpackCmd{ | ||
| stdout: stdout, | ||
| stderr: stderr, | ||
| } | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "unpack [src] [dest]", | ||
| Short: "Unpack an image or image source layout", | ||
| Long: `Unpack the OCI image .tar file or OCI image layout directory present at [src] to the destination directory [dest].`, | ||
| Run: v.Run, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar( | ||
| &v.typ, "type", "", | ||
| fmt.Sprintf( | ||
| `Type of the file to unpack. If unset, oci-image-tool will try to auto-detect the type. One of "%s"`, | ||
| strings.Join(unpackTypes, ","), | ||
| ), | ||
| ) | ||
|
|
||
| cmd.Flags().StringVar( | ||
| &v.ref, "ref", "v1.0", | ||
| `The ref pointing to the manifest to be unpacked. This must be present in the "refs" subdirectory of the image.`, | ||
| ) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func (v *unpackCmd) Run(cmd *cobra.Command, args []string) { | ||
| if len(args) != 2 { | ||
| v.stderr.Print("both src and dest must be provided") | ||
| if err := cmd.Usage(); err != nil { | ||
| v.stderr.Println(err) | ||
| } | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| if v.typ == "" { | ||
| typ, err := autodetect(args[0]) | ||
| if err != nil { | ||
| v.stderr.Printf("%q: autodetection failed: %v", args[0], err) | ||
| os.Exit(1) | ||
| } | ||
| v.typ = typ | ||
| } | ||
|
|
||
| var err error | ||
| switch v.typ { | ||
| case typeImageLayout: | ||
| err = image.UnpackLayout(args[0], args[1], v.ref) | ||
|
|
||
| case typeImage: | ||
| err = image.Unpack(args[0], args[1], v.ref) | ||
| } | ||
|
|
||
| if err != nil { | ||
| v.stderr.Printf("unpacking failed: %v", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| os.Exit(0) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use the mediatypes directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this gives me finer grained control, i.e.
typeImageLayout, andtypeImagedon't have media types at all and must be treated differently.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course!