-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: status, metadata and content handlers for
manifest index
…
…commands (#1509) Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
- Loading branch information
1 parent
b4be9a6
commit e6a3eee
Showing
21 changed files
with
927 additions
and
144 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
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,58 @@ | ||
/* | ||
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" | ||
|
||
"oras.land/oras/cmd/oras/internal/output" | ||
) | ||
|
||
// manifestIndexCreate handles raw content output. | ||
type manifestIndexCreate struct { | ||
pretty bool | ||
stdout io.Writer | ||
outputPath string | ||
} | ||
|
||
// NewManifestIndexCreateHandler creates a new handler. | ||
func NewManifestIndexCreateHandler(out io.Writer, pretty bool, outputPath string) ManifestIndexCreateHandler { | ||
// ignore --pretty when output to a file | ||
if outputPath != "" && outputPath != "-" { | ||
pretty = false | ||
} | ||
return &manifestIndexCreate{ | ||
pretty: pretty, | ||
stdout: out, | ||
outputPath: outputPath, | ||
} | ||
} | ||
|
||
// OnContentCreated is called after index content is created. | ||
func (h *manifestIndexCreate) OnContentCreated(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 output.PrintJSON(out, manifest, h.pretty) | ||
} |
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 content | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func Test_manifestIndexCreate_OnContentCreated(t *testing.T) { | ||
testHandler := NewManifestIndexCreateHandler(os.Stdout, false, "invalid/path") | ||
if err := testHandler.OnContentCreated([]byte("test content")); err == nil { | ||
t.Errorf("manifestIndexCreate.OnContentCreated() error = %v, wantErr non-nil error", err) | ||
} | ||
} |
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,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 metadata | ||
|
||
import ( | ||
"testing" | ||
|
||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
) | ||
|
||
func TestDiscard_OnTagged(t *testing.T) { | ||
testDiscard := NewDiscardHandler() | ||
if err := testDiscard.OnTagged(ocispec.Descriptor{}, "test"); err != nil { | ||
t.Errorf("testDiscard.OnTagged() error = %v, want nil", err) | ||
} | ||
} |
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
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,43 @@ | ||
/* | ||
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 status | ||
|
||
import ( | ||
"testing" | ||
|
||
v1 "github.com/opencontainers/image-spec/specs-go/v1" | ||
) | ||
|
||
func TestDiscardHandler_OnManifestRemoved(t *testing.T) { | ||
testDiscard := NewDiscardHandler() | ||
if err := testDiscard.OnManifestRemoved("sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a"); err != nil { | ||
t.Errorf("DiscardHandler.OnManifestRemoved() error = %v, wantErr nil", err) | ||
} | ||
} | ||
|
||
func TestDiscardHandler_OnIndexMerged(t *testing.T) { | ||
testDiscard := NewDiscardHandler() | ||
if err := testDiscard.OnIndexMerged("test", v1.Descriptor{}); err != nil { | ||
t.Errorf("DiscardHandler.OnIndexMerged() error = %v, wantErr nil", err) | ||
} | ||
} | ||
|
||
func TestDiscardHandler_OnIndexPushed(t *testing.T) { | ||
testDiscard := NewDiscardHandler() | ||
if err := testDiscard.OnIndexPushed("test"); err != nil { | ||
t.Errorf("DiscardHandler.OnIndexPushed() error = %v, wantErr nil", err) | ||
} | ||
} |
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.