-
Notifications
You must be signed in to change notification settings - Fork 184
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 <[email protected]>
- Loading branch information
Showing
11 changed files
with
290 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
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 metadata | ||
|
||
import ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
|
||
type discard struct{} | ||
|
||
// NewDiscardHandler creates a new handler that discards output for all events. | ||
func NewDiscardHandler() ManifestFetchHandler { | ||
return discard{} | ||
} | ||
|
||
// OnFetched implements ManifestFetchHandler. | ||
func (discard) OnFetched([]byte, ocispec.Descriptor) error { 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
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,54 @@ | ||
/* | ||
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 ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"reflect" | ||
|
||
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/internal/docker" | ||
) | ||
|
||
// ManifestFetchHandler handles JSON metadata output for manifest fetch events. | ||
type ManifestFetchHandler struct { | ||
out io.Writer | ||
} | ||
|
||
// NewManifestFetchHandler creates a new handler for manifest fetch events. | ||
func NewManifestFetchHandler(out io.Writer) metadata.ManifestFetchHandler { | ||
return &ManifestFetchHandler{ | ||
out: out, | ||
} | ||
} | ||
|
||
// OnFetched is called after the manifest fetch is completed. | ||
func (h *ManifestFetchHandler) OnFetched(content []byte, desc ocispec.Descriptor) error { | ||
switch desc.MediaType { | ||
case ocispec.MediaTypeImageManifest, docker.MediaTypeManifest: | ||
var manifest ocispec.Manifest | ||
if err := json.Unmarshal(content, &manifest); err != nil { | ||
return err | ||
} | ||
return printJSON(h.out, model.ToMappable(reflect.ValueOf(manifest))) | ||
default: | ||
return fmt.Errorf("cannot apply template: unsupported media type %s", desc.MediaType) | ||
} | ||
} |
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,33 @@ | ||
/* | ||
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 raw | ||
|
||
import ( | ||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
) | ||
|
||
type discard struct{} | ||
|
||
// OnContentFetched implements ManifestFetchHandler. | ||
func (discard) OnContentFetched(string, []byte) error { return nil } | ||
|
||
// OnDescriptorFetched implements ManifestFetchHandler. | ||
func (discard) OnDescriptorFetched(desc ocispec.Descriptor) error { return nil } | ||
|
||
// NewManifestFetchHandler creates a new handler. | ||
func NewDiscardHandler() ManifestFetchHandler { | ||
return discard{} | ||
} |
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,29 @@ | ||
/* | ||
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 raw | ||
|
||
import ( | ||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
) | ||
|
||
// ManifestFetchHandler handles raw output for manifest fetch events. | ||
type ManifestFetchHandler interface { | ||
// OnFetched is called after the manifest content is fetched. | ||
OnContentFetched(outputPath string, content []byte) error | ||
// OnDescriptorFetched is called after the manifest descriptor is | ||
// fetched. | ||
OnDescriptorFetched(desc ocispec.Descriptor) error | ||
} |
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,76 @@ | ||
/* | ||
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 raw | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
) | ||
|
||
// RawManifestFetch handles raw content output. | ||
type RawManifestFetch struct { | ||
pretty bool | ||
stdout io.Writer | ||
} | ||
|
||
// OnContentFetched implements ManifestFetchHandler. | ||
func (h *RawManifestFetch) OnContentFetched(outputPath string, manifest []byte) error { | ||
out := h.stdout | ||
if outputPath != "-" && outputPath != "" { | ||
f, err := os.OpenFile(outputPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) | ||
if err != nil { | ||
return fmt.Errorf("failed to open %q: %w", outputPath, err) | ||
} | ||
defer f.Close() | ||
} | ||
return h.output(out, manifest) | ||
} | ||
|
||
// OnDescriptorFetched implements ManifestFetchHandler. | ||
func (h *RawManifestFetch) OnDescriptorFetched(desc ocispec.Descriptor) error { | ||
descBytes, err := json.Marshal(desc) | ||
if err != nil { | ||
return fmt.Errorf("invalid descriptor: %w", err) | ||
} | ||
return h.output(h.stdout, descBytes) | ||
} | ||
|
||
// NewManifestFetchHandler creates a new handler. | ||
func NewManifestFetchHandler(out io.Writer, pretty bool) ManifestFetchHandler { | ||
return &RawManifestFetch{ | ||
pretty: pretty, | ||
stdout: out, | ||
} | ||
} | ||
|
||
// OnFetched is called after the content is fetched. | ||
func (h *RawManifestFetch) output(out io.Writer, data []byte) error { | ||
if h.pretty { | ||
buf := bytes.NewBuffer(nil) | ||
if err := json.Indent(buf, data, "", " "); err != nil { | ||
return fmt.Errorf("failed to prettify: %w", err) | ||
} | ||
buf.WriteByte('\n') | ||
data = buf.Bytes() | ||
} | ||
_, err := out.Write(data) | ||
return err | ||
} |
Oops, something went wrong.