Skip to content

Commit

Permalink
-j/--json flag added to exec command
Browse files Browse the repository at this point in the history
  • Loading branch information
steiler committed Jun 21, 2021
1 parent f92fadd commit a4ae983
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"strings"

Expand All @@ -15,7 +16,10 @@ import (
"github.com/srl-labs/containerlab/types"
)

var labels []string
var (
labels []string
jsonOutput bool
)

// execCmd represents the exec command
var execCmd = &cobra.Command{
Expand Down Expand Up @@ -59,7 +63,10 @@ var execCmd = &cobra.Command{
for _, a := range args {
cmds = append(cmds, strings.Split(a, " ")...)
}
jsonResult := make(map[string]map[string]interface{})

for _, cont := range containers {
var doc interface{}
if cont.State != "running" {
continue
}
Expand All @@ -68,17 +75,36 @@ var execCmd = &cobra.Command{
log.Errorf("%s: failed to execute cmd: %v", cont.Names, err)
continue
}
if len(stdout) > 0 {
log.Infof("%s: stdout:\n%s", cont.Names, string(stdout))
if jsonOutput {
jsonResult[cont.Names[0]] = make(map[string]interface{})
err := json.Unmarshal([]byte(stdout), &doc)
if err == nil {
jsonResult[cont.Names[0]]["stdout"] = doc
} else {
jsonResult[cont.Names[0]]["stdout"] = string(stdout)
}
jsonResult[cont.Names[0]]["stderr"] = string(stderr)
} else {
if len(stdout) > 0 {
log.Infof("%s: stdout:\n%s", cont.Names, string(stdout))
}
if len(stderr) > 0 {
log.Infof("%s: stderr:\n%s", cont.Names, string(stderr))
}
}
if len(stderr) > 0 {
log.Infof("%s: stderr:\n%s", cont.Names, string(stderr))
}
if jsonOutput {
result, err := json.Marshal(jsonResult)
if err != nil {
log.Debug("Issue converting to json %v", err)
}
fmt.Println(string(result))
}
},
}

func init() {
rootCmd.AddCommand(execCmd)
execCmd.Flags().StringSliceVarP(&labels, "label", "", []string{}, "labels to filter container subset")
execCmd.Flags().BoolVarP(&jsonOutput, "json", "j", false, "get output in json format")
}

0 comments on commit a4ae983

Please sign in to comment.