mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-03 02:26:37 +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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@ -328,7 +329,34 @@ func getEnvInfo(configFile string, config oci.RuntimeConfig) (env EnvInfo, err e
|
|||||||
return env, nil
|
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)
|
encoder := toml.NewEncoder(file)
|
||||||
|
|
||||||
err := encoder.Encode(env)
|
err := encoder.Encode(env)
|
||||||
@ -339,33 +367,30 @@ func showSettings(env EnvInfo, file *os.File) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleSettings(file *os.File, metadata map[string]interface{}) error {
|
func writeJSONSettings(env EnvInfo, file *os.File) error {
|
||||||
if file == nil {
|
encoder := json.NewEncoder(file)
|
||||||
return errors.New("Invalid output file specified")
|
|
||||||
}
|
|
||||||
|
|
||||||
configFile, ok := metadata["configFile"].(string)
|
// Make it more human readable
|
||||||
if !ok {
|
encoder.SetIndent("", " ")
|
||||||
return errors.New("cannot determine config file")
|
|
||||||
}
|
|
||||||
|
|
||||||
runtimeConfig, ok := metadata["runtimeConfig"].(oci.RuntimeConfig)
|
err := encoder.Encode(env)
|
||||||
if !ok {
|
|
||||||
return errors.New("cannot determine runtime config")
|
|
||||||
}
|
|
||||||
|
|
||||||
env, err := getEnvInfo(configFile, runtimeConfig)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return showSettings(env, file)
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var kataEnvCLICommand = cli.Command{
|
var kataEnvCLICommand = cli.Command{
|
||||||
Name: envCmd,
|
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 {
|
Action: func(context *cli.Context) error {
|
||||||
return handleSettings(defaultOutputFile, context.App.Metadata)
|
return handleSettings(defaultOutputFile, context)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
@ -717,7 +718,7 @@ func testEnvShowSettings(t *testing.T, tmpdir string, tmpfile *os.File) error {
|
|||||||
Host: expectedHostDetails,
|
Host: expectedHostDetails,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = showSettings(env, tmpfile)
|
err = writeTOMLSettings(env, tmpfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -906,6 +907,13 @@ func TestEnvCLIFunction(t *testing.T) {
|
|||||||
|
|
||||||
err = fn(ctx)
|
err = fn(ctx)
|
||||||
assert.NoError(t, err)
|
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) {
|
func TestEnvCLIFunctionFail(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user