forked from oras-project/oras
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Billy Zha <jinzha1@microsoft.com>
- Loading branch information
1 parent
8151f9c
commit 416feed
Showing
18 changed files
with
684 additions
and
181 deletions.
There are no files selected for viewing
This file contains 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 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 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,55 @@ | ||
/* | ||
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 json | ||
|
||
import ( | ||
"io" | ||
|
||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
"oras.land/oras/cmd/oras/internal/display/metadata" | ||
"oras.land/oras/cmd/oras/internal/display/metadata/model" | ||
"oras.land/oras/cmd/oras/internal/option" | ||
) | ||
|
||
// PullHandler handles JSON metadata output for pull events. | ||
type PullHandler struct { | ||
path string | ||
pulled model.Pulled | ||
out io.Writer | ||
} | ||
|
||
// OnLayerSkipped implements metadata.PullHandler. | ||
func (ph *PullHandler) OnLayerSkipped(ocispec.Descriptor) error { | ||
return nil | ||
} | ||
|
||
// NewPullHandler returns a new handler for Pull events. | ||
func NewPullHandler(out io.Writer, path string) metadata.PullHandler { | ||
return &PullHandler{ | ||
out: out, | ||
path: path, | ||
} | ||
} | ||
|
||
// OnFilePulled implements metadata.PullHandler. | ||
func (ph *PullHandler) OnFilePulled(name string, outputDir string, desc ocispec.Descriptor, descPath string) error { | ||
return ph.pulled.Add(name, outputDir, desc, descPath) | ||
} | ||
|
||
// OnCompleted implements metadata.PullHandler. | ||
func (ph *PullHandler) OnCompleted(opts *option.Target, desc ocispec.Descriptor) error { | ||
return printJSON(ph.out, model.NewPull(ph.path+"@"+desc.Digest.String(), ph.pulled.Files())) | ||
} |
This file contains 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,95 @@ | ||
/* | ||
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 model | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"slices" | ||
"sync" | ||
|
||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
"oras.land/oras-go/v2/content/file" | ||
) | ||
|
||
// File records metadata of a pulled file. | ||
type File struct { | ||
// Path is the absolute path of the pulled file. | ||
Path string | ||
Descriptor | ||
} | ||
|
||
// newFile creates a new file metadata. | ||
func newFile(name string, outputDir string, desc ocispec.Descriptor, descPath string) (File, error) { | ||
path := name | ||
if !filepath.IsAbs(name) { | ||
var err error | ||
path, err = filepath.Abs(filepath.Join(outputDir, name)) | ||
// not likely to go wrong since the file has already be written to file store | ||
if err != nil { | ||
return File{}, fmt.Errorf("failed to get absolute path of pulled file %s: %w", name, err) | ||
} | ||
} else { | ||
path = filepath.Clean(path) | ||
} | ||
if desc.Annotations[file.AnnotationUnpack] == "true" { | ||
path += string(filepath.Separator) | ||
} | ||
return File{ | ||
Path: path, | ||
Descriptor: FromDescriptor(descPath, desc), | ||
}, nil | ||
} | ||
|
||
type pull struct { | ||
DigestReference | ||
Files []File `json:"Files"` | ||
} | ||
|
||
// NewPull creates a new metadata struct for pull command. | ||
func NewPull(digestReference string, files []File) any { | ||
return pull{ | ||
DigestReference: DigestReference{ | ||
Ref: digestReference, | ||
}, | ||
Files: files, | ||
} | ||
} | ||
|
||
// Pulled records all pulled files. | ||
type Pulled struct { | ||
lock sync.Mutex | ||
files []File | ||
} | ||
|
||
// Files returns all pulled files. | ||
func (p *Pulled) Files() []File { | ||
p.lock.Lock() | ||
defer p.lock.Unlock() | ||
return slices.Clone(p.files) | ||
} | ||
|
||
// Add adds a pulled file. | ||
func (p *Pulled) Add(name string, outputDir string, desc ocispec.Descriptor, descPath string) error { | ||
p.lock.Lock() | ||
defer p.lock.Unlock() | ||
file, err := newFile(name, outputDir, desc, descPath) | ||
if err != nil { | ||
return err | ||
} | ||
p.files = append(p.files, file) | ||
return nil | ||
} |
This file contains 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,57 @@ | ||
/* | ||
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 template | ||
|
||
import ( | ||
"io" | ||
|
||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
"oras.land/oras/cmd/oras/internal/display/metadata" | ||
"oras.land/oras/cmd/oras/internal/display/metadata/model" | ||
"oras.land/oras/cmd/oras/internal/option" | ||
) | ||
|
||
// PullHandler handles text metadata output for pull events. | ||
type PullHandler struct { | ||
template string | ||
path string | ||
out io.Writer | ||
pulled model.Pulled | ||
} | ||
|
||
// OnCompleted implements metadata.PullHandler. | ||
func (ph *PullHandler) OnCompleted(opts *option.Target, desc ocispec.Descriptor) error { | ||
return parseAndWrite(ph.out, model.NewPull(ph.path+"@"+desc.Digest.String(), ph.pulled.Files()), ph.template) | ||
} | ||
|
||
// OnFilePulled implements metadata.PullHandler. | ||
func (ph *PullHandler) OnFilePulled(name string, outputDir string, desc ocispec.Descriptor, descPath string) error { | ||
return ph.pulled.Add(name, outputDir, desc, descPath) | ||
} | ||
|
||
// OnLayerSkipped implements metadata.PullHandler. | ||
func (ph *PullHandler) OnLayerSkipped(ocispec.Descriptor) error { | ||
return nil | ||
} | ||
|
||
// NewPullHandler returns a new handler for pull events. | ||
func NewPullHandler(out io.Writer, path string, template string) metadata.PullHandler { | ||
return &PullHandler{ | ||
path: path, | ||
template: template, | ||
out: out, | ||
} | ||
} |
This file contains 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,61 @@ | ||
/* | ||
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 text | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"sync/atomic" | ||
|
||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
"oras.land/oras/cmd/oras/internal/display/metadata" | ||
"oras.land/oras/cmd/oras/internal/option" | ||
) | ||
|
||
// PullHandler handles text metadata output for pull events. | ||
type PullHandler struct { | ||
out io.Writer | ||
layerSkipped atomic.Bool | ||
} | ||
|
||
// OnCompleted implements metadata.PullHandler. | ||
func (p *PullHandler) OnCompleted(opts *option.Target, desc ocispec.Descriptor) error { | ||
if p.layerSkipped.Load() { | ||
_, _ = fmt.Fprintf(p.out, "Skipped pulling layers without file name in %q\n", ocispec.AnnotationTitle) | ||
_, _ = fmt.Fprintf(p.out, "Use 'oras copy %s --to-oci-layout <layout-dir>' to pull all layers.\n", opts.RawReference) | ||
} else { | ||
_, _ = fmt.Fprintln(p.out, "Pulled", opts.AnnotatedReference()) | ||
_, _ = fmt.Fprintln(p.out, "Digest:", desc.Digest) | ||
} | ||
return nil | ||
} | ||
|
||
func (p *PullHandler) OnFilePulled(name string, outputDir string, desc ocispec.Descriptor, descPath string) error { | ||
return nil | ||
} | ||
|
||
// OnLayerSkipped implements metadata.PullHandler. | ||
func (ph *PullHandler) OnLayerSkipped(ocispec.Descriptor) error { | ||
ph.layerSkipped.Store(true) | ||
return nil | ||
} | ||
|
||
// NewPullHandler returns a new handler for Pull events. | ||
func NewPullHandler(out io.Writer) metadata.PullHandler { | ||
return &PullHandler{ | ||
out: out, | ||
} | ||
} |
This file contains 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
Oops, something went wrong.