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
10440e9
commit e5ac9d9
Showing
16 changed files
with
444 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
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 content | ||
|
||
import ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
|
||
type discardHandler struct{} | ||
|
||
// OnContentFetched implements ManifestFetchHandler. | ||
func (discardHandler) OnContentFetched(ocispec.Descriptor, []byte) error { | ||
return nil | ||
} | ||
|
||
// NewDiscardHandler returns a new discard handler. | ||
func NewDiscardHandler() ManifestFetchHandler { | ||
return discardHandler{} | ||
} |
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,26 @@ | ||
/* | ||
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 content | ||
|
||
import ( | ||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
) | ||
|
||
// ManifestFetchHandler handles raw output for manifest fetch events. | ||
type ManifestFetchHandler interface { | ||
// OnContentFetched is called after the manifest content is fetched. | ||
OnContentFetched(desc ocispec.Descriptor, content []byte) 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,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 content | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
"oras.land/oras/cmd/oras/internal/display/utils" | ||
) | ||
|
||
// manifestFetch handles raw content output. | ||
type manifestFetch struct { | ||
pretty bool | ||
stdout io.Writer | ||
outputPath string | ||
} | ||
|
||
func (h *manifestFetch) OnContentFetched(desc ocispec.Descriptor, manifest []byte) error { | ||
out := h.stdout | ||
if h.outputPath != "-" && h.outputPath != "" { | ||
f, err := os.Create(h.outputPath) | ||
if err != nil { | ||
return fmt.Errorf("failed to open %q: %w", h.outputPath, err) | ||
} | ||
defer f.Close() | ||
out = f | ||
} | ||
return utils.PrintJSON(out, manifest, h.pretty) | ||
} | ||
|
||
// NewManifestFetchHandler creates a new handler. | ||
func NewManifestFetchHandler(out io.Writer, pretty bool, outputPath string) ManifestFetchHandler { | ||
return &manifestFetch{ | ||
pretty: pretty, | ||
stdout: out, | ||
outputPath: outputPath, | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
cmd/oras/internal/display/metadata/descriptor/manifest_fetch.go
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,49 @@ | ||
/* | ||
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 descriptor | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"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/utils" | ||
) | ||
|
||
// manifestFetchHandler handles metadata descriptor output. | ||
type manifestFetchHandler struct { | ||
pretty bool | ||
out io.Writer | ||
} | ||
|
||
// OnFetched implements ManifestFetchHandler. | ||
func (h *manifestFetchHandler) OnFetched(_ string, desc ocispec.Descriptor, _ []byte) error { | ||
descBytes, err := json.Marshal(desc) | ||
if err != nil { | ||
return fmt.Errorf("invalid descriptor: %w", err) | ||
} | ||
return utils.PrintJSON(h.out, descBytes, h.pretty) | ||
} | ||
|
||
// NewManifestFetchHandler creates a new handler. | ||
func NewManifestFetchHandler(out io.Writer, pretty bool) metadata.ManifestFetchHandler { | ||
return &manifestFetchHandler{ | ||
pretty: pretty, | ||
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,30 @@ | ||
/* | ||
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(string, ocispec.Descriptor, []byte) 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,46 @@ | ||
/* | ||
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" | ||
"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" | ||
) | ||
|
||
// 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(path string, desc ocispec.Descriptor, content []byte) error { | ||
var manifest map[string]any | ||
if err := json.Unmarshal(content, &manifest); err != nil { | ||
manifest = nil | ||
} | ||
return printJSON(h.out, model.NewFetched(path, desc, manifest)) | ||
} |
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,31 @@ | ||
/* | ||
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 ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
|
||
type fetched struct { | ||
Descriptor | ||
Content any | ||
} | ||
|
||
// NewFetched creates a new fetched metadata. | ||
func NewFetched(path string, desc ocispec.Descriptor, content any) any { | ||
return &fetched{ | ||
Descriptor: FromDescriptor(path, desc), | ||
Content: content, | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
cmd/oras/internal/display/metadata/template/manifest_fetch.go
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,48 @@ | ||
/* | ||
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 ( | ||
"encoding/json" | ||
"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" | ||
) | ||
|
||
// manifestFetchHandler handles JSON metadata output for manifest fetch events. | ||
type manifestFetchHandler struct { | ||
template string | ||
out io.Writer | ||
} | ||
|
||
// NewManifestFetchHandler creates a new handler for manifest fetch events. | ||
func NewManifestFetchHandler(out io.Writer, template string) metadata.ManifestFetchHandler { | ||
return &manifestFetchHandler{ | ||
template: template, | ||
out: out, | ||
} | ||
} | ||
|
||
// OnFetched is called after the manifest fetch is completed. | ||
func (h *manifestFetchHandler) OnFetched(path string, desc ocispec.Descriptor, content []byte) error { | ||
var manifest map[string]any | ||
if err := json.Unmarshal(content, &manifest); err != nil { | ||
manifest = nil | ||
} | ||
return parseAndWrite(h.out, model.NewFetched(path, desc, manifest), h.template) | ||
} |
Oops, something went wrong.