mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-01 17:52:40 +00:00
kata-env: Add ability to output as JSON
Having a direct JSON output for kata-env will help record results in our CIs in some instances. Add that ability with a kata-env command line extension. Fixes: #474 Signed-off-by: Graham Whaley <graham.whaley@intel.com>
This commit is contained in:
parent
b2bec3362b
commit
63c06bee70
@ -6,6 +6,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
@ -328,7 +329,34 @@ func getEnvInfo(configFile string, config oci.RuntimeConfig) (env EnvInfo, err e
|
||||
return env, nil
|
||||
}
|
||||
|
||||
func showSettings(env EnvInfo, file *os.File) error {
|
||||
func handleSettings(file *os.File, c *cli.Context) error {
|
||||
if file == nil {
|
||||
return errors.New("Invalid output file specified")
|
||||
}
|
||||
|
||||
configFile, ok := c.App.Metadata["configFile"].(string)
|
||||
if !ok {
|
||||
return errors.New("cannot determine config file")
|
||||
}
|
||||
|
||||
runtimeConfig, ok := c.App.Metadata["runtimeConfig"].(oci.RuntimeConfig)
|
||||
if !ok {
|
||||
return errors.New("cannot determine runtime config")
|
||||
}
|
||||
|
||||
env, err := getEnvInfo(configFile, runtimeConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if c.Bool("json") {
|
||||
return writeJSONSettings(env, file)
|
||||
}
|
||||
|
||||
return writeTOMLSettings(env, file)
|
||||
}
|
||||
|
||||
func writeTOMLSettings(env EnvInfo, file *os.File) error {
|
||||
encoder := toml.NewEncoder(file)
|
||||
|
||||
err := encoder.Encode(env)
|
||||
@ -339,33 +367,30 @@ func showSettings(env EnvInfo, file *os.File) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleSettings(file *os.File, metadata map[string]interface{}) error {
|
||||
if file == nil {
|
||||
return errors.New("Invalid output file specified")
|
||||
}
|
||||
func writeJSONSettings(env EnvInfo, file *os.File) error {
|
||||
encoder := json.NewEncoder(file)
|
||||
|
||||
configFile, ok := metadata["configFile"].(string)
|
||||
if !ok {
|
||||
return errors.New("cannot determine config file")
|
||||
}
|
||||
// Make it more human readable
|
||||
encoder.SetIndent("", " ")
|
||||
|
||||
runtimeConfig, ok := metadata["runtimeConfig"].(oci.RuntimeConfig)
|
||||
if !ok {
|
||||
return errors.New("cannot determine runtime config")
|
||||
}
|
||||
|
||||
env, err := getEnvInfo(configFile, runtimeConfig)
|
||||
err := encoder.Encode(env)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return showSettings(env, file)
|
||||
return nil
|
||||
}
|
||||
|
||||
var kataEnvCLICommand = cli.Command{
|
||||
Name: envCmd,
|
||||
Usage: "display settings",
|
||||
Usage: "display settings. Default to TOML",
|
||||
Flags: []cli.Flag{
|
||||
cli.BoolFlag{
|
||||
Name: "json",
|
||||
Usage: "Format output as JSON",
|
||||
},
|
||||
},
|
||||
Action: func(context *cli.Context) error {
|
||||
return handleSettings(defaultOutputFile, context.App.Metadata)
|
||||
return handleSettings(defaultOutputFile, context)
|
||||
},
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@ -717,7 +718,7 @@ func testEnvShowSettings(t *testing.T, tmpdir string, tmpfile *os.File) error {
|
||||
Host: expectedHostDetails,
|
||||
}
|
||||
|
||||
err = showSettings(env, tmpfile)
|
||||
err = writeTOMLSettings(env, tmpfile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -906,6 +907,13 @@ func TestEnvCLIFunction(t *testing.T) {
|
||||
|
||||
err = fn(ctx)
|
||||
assert.NoError(t, err)
|
||||
|
||||
set := flag.NewFlagSet("", 0)
|
||||
set.Bool("json", true, "")
|
||||
ctx = cli.NewContext(app, set, nil)
|
||||
|
||||
err = fn(ctx)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestEnvCLIFunctionFail(t *testing.T) {
|
||||
|
Loading…
Reference in New Issue
Block a user