feat(status): Optional output as JSON and YAML

parent 782f855a
......@@ -17,11 +17,13 @@ limitations under the License.
package main
import (
"encoding/json"
"fmt"
"io"
"regexp"
"text/tabwriter"
"github.com/ghodss/yaml"
"github.com/gosuri/uitable"
"github.com/gosuri/uitable/util/strutil"
"github.com/spf13/cobra"
......@@ -48,6 +50,7 @@ type statusCmd struct {
out io.Writer
client helm.Interface
version int32
outfmt string
}
func newStatusCmd(client helm.Interface, out io.Writer) *cobra.Command {
......@@ -74,6 +77,7 @@ func newStatusCmd(client helm.Interface, out io.Writer) *cobra.Command {
}
cmd.PersistentFlags().Int32Var(&status.version, "revision", 0, "if set, display the status of the named release with revision")
cmd.PersistentFlags().StringVarP(&status.outfmt, "output", "o", "", "output the status in the specified format (json or yaml)")
return cmd
}
......@@ -84,8 +88,27 @@ func (s *statusCmd) run() error {
return prettyError(err)
}
switch s.outfmt {
case "":
PrintStatus(s.out, res)
return nil
case "json":
data, err := json.Marshal(res)
if err != nil {
return fmt.Errorf("Failed to Marshal JSON output: %s", err)
}
s.out.Write(data)
return nil
case "yaml":
data, err := yaml.Marshal(res)
if err != nil {
return fmt.Errorf("Failed to Marshal YAML output: %s", err)
}
s.out.Write(data)
return nil
}
return fmt.Errorf("Unknown output format %q", s.outfmt)
}
// PrintStatus prints out the status of a release. Shared because also used by
......
......@@ -65,6 +65,16 @@ func TestStatusCmd(t *testing.T) {
Notes: "release notes",
}),
},
{
name: "get status of a deployed release with notes in json",
args: []string{"flummoxed-chickadee"},
flags: []string{"-o", "json"},
expected: `{"name":"flummoxed-chickadee","info":{"status":{"code":1,"notes":"release notes"},"first_deployed":{"seconds":242085845},"last_deployed":{"seconds":242085845}}}`,
rel: releaseMockWithStatus(&release.Status{
Code: release.Status_DEPLOYED,
Notes: "release notes",
}),
},
{
name: "get status of a deployed release with resources",
args: []string{"flummoxed-chickadee"},
......@@ -74,6 +84,16 @@ func TestStatusCmd(t *testing.T) {
Resources: "resource A\nresource B\n",
}),
},
{
name: "get status of a deployed release with resources in YAML",
args: []string{"flummoxed-chickadee"},
flags: []string{"-o", "yaml"},
expected: "info:\nfirst_deployed:\nseconds:242085845\nlast_deployed:\nseconds:242085845\nstatus:\ncode:1\nresources:|\nresourceA\nresourceB\nname:flummoxed-chickadee\n",
rel: releaseMockWithStatus(&release.Status{
Code: release.Status_DEPLOYED,
Resources: "resource A\nresource B\n",
}),
},
{
name: "get status of a deployed release with test suite",
args: []string{"flummoxed-chickadee"},
......
......@@ -23,6 +23,7 @@ helm status [flags] RELEASE_NAME
### Options
```
-o, --output string output the status in the specified format (json or yaml)
--revision int32 if set, display the status of the named release with revision
--tls enable TLS for request
--tls-ca-cert string path to TLS CA certificate file (default "$HELM_HOME/ca.pem")
......@@ -45,4 +46,4 @@ helm status [flags] RELEASE_NAME
### SEE ALSO
* [helm](helm.md) - The Helm package manager for Kubernetes.
###### Auto generated by spf13/cobra on 7-Nov-2017
###### Auto generated by spf13/cobra on 12-Dec-2017
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment