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.
feat: support JSON output for attach and push
Signed-off-by: Billy Zha <[email protected]>
- Loading branch information
Showing
8 changed files
with
202 additions
and
41 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,53 @@ | ||
/* | ||
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 option | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/spf13/pflag" | ||
"oras.land/oras/cmd/oras/internal/output/format" | ||
) | ||
|
||
// Format option struct. | ||
type Format struct { | ||
format.Flag | ||
} | ||
|
||
func (opts *Format) ApplyFlags(fs *pflag.FlagSet) { | ||
fs.Var(&opts.Flag, "format-stdout", fmt.Sprintf("[Preview] summary output to stdout in the specified format (default %q)", format.Plain)) | ||
} | ||
|
||
// Print prints the Output as JSON. | ||
func (opts *Format) Print(data interface{}, prettify bool) error { | ||
var err error | ||
switch opts.Flag { | ||
case format.Json: | ||
var content []byte | ||
if prettify { | ||
content, err = json.MarshalIndent(data, "", " ") | ||
} else { | ||
content, err = json.Marshal(data) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
_, err = fmt.Println(string(content)) | ||
return err | ||
|
||
} | ||
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,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 format | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
) | ||
|
||
const ( | ||
Plain = "plain" | ||
Json = "json" | ||
) | ||
|
||
// Printer is the interface that wraps the basic String method. | ||
type Printer interface { | ||
Print(prettify bool) error | ||
} | ||
|
||
// Define a custom value type that implements the pflag.Value interface. | ||
type Flag string | ||
|
||
// Set must have pointer receiver so it doesn't change the value of a copy. | ||
func (f *Flag) Set(v string) error { | ||
switch v { | ||
case "": | ||
// default | ||
*f = Plain | ||
case Plain, Json: | ||
*f = Json | ||
default: | ||
return errors.New("invalid format flag, expecting " + f.Type()) | ||
} | ||
return nil | ||
} | ||
|
||
// String is used by pflag to print the default value of a flag. | ||
func (f *Flag) String() string { | ||
return string(*f) | ||
} | ||
|
||
// Type provides optional value used in help text. | ||
func (c *Flag) Type() string { | ||
return strings.Join([]string{Plain, Json}, "|") | ||
} |
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,47 @@ | ||
/* | ||
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 format | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
) | ||
|
||
// JSON is a Output that prints the data as JSON. | ||
type JSON struct { | ||
Data interface{} | ||
} | ||
|
||
// NewJSON creates a new Output with the given data | ||
func NewJSON(data interface{}) *JSON { | ||
return &JSON{Data: data} | ||
} | ||
|
||
// Print prints the Output as JSON. | ||
func Print(data interface{}, prettify bool) error { | ||
var content []byte | ||
var err error | ||
if prettify { | ||
content, err = json.MarshalIndent(data, "", " ") | ||
} else { | ||
content, err = json.Marshal(data) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
_, err = os.Stdout.Write(content) | ||
return err | ||
} |
This file was deleted.
Oops, something went wrong.
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