Skip to content

Commit

Permalink
JSON output for service list in experimental mode (#3186)
Browse files Browse the repository at this point in the history
* JSON output for service list in experimental mode

* Changes to tests as per PR feedback

#3186 (review)
  • Loading branch information
dharmit committed May 18, 2020
1 parent 898cd39 commit 1e24b53
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
17 changes: 11 additions & 6 deletions pkg/odo/cli/service/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,26 @@ func (o *ServiceListOptions) Run() (err error) {
return err
}

w := tabwriter.NewWriter(os.Stdout, 5, 2, 3, ' ', tabwriter.TabIndent)
if len(list) == 0 {
return fmt.Errorf("No operator backed services found in the namesapce")
}

if log.IsJSON() {
machineoutput.OutputSuccess(list)
return
} else {
w := tabwriter.NewWriter(os.Stdout, 5, 2, 3, ' ', tabwriter.TabIndent)

if len(list) > 0 {
fmt.Fprintln(w, "NAME", "\t", "TYPE", "\t", "AGE")

for _, item := range list {
duration := time.Since(item.GetCreationTimestamp().Time).Truncate(time.Second).String()
fmt.Fprintln(w, item.GetName(), "\t", item.GetKind(), "\t", duration)
}

} else {
fmt.Fprintln(w, "No operator backed services found in the namesapce")
}
w.Flush()

w.Flush()
}

return err
}
Expand Down
8 changes: 7 additions & 1 deletion tests/integration/operatorhub/cmd_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,20 @@ spec:
Expect(stdOut).To(ContainSubstring("example"))
Expect(stdOut).To(ContainSubstring("EtcdCluster"))

// now check for json output
jsonOut := helper.CmdShouldPass("odo", "service", "list", "-o", "json")
helper.MatchAllInOutput(jsonOut, []string{"\"apiVersion\": \"etcd.database.coreos.com/v1beta2\"", "\"kind\": \"EtcdCluster\"", "\"name\": \"example3\""})

// Delete the pods created. This should idealy be done by `odo
// service delete` but that's not implemented for operator backed
// services yet.
helper.CmdShouldPass("oc", "delete", "EtcdCluster", "example3")

// Now let's check the output again to ensure expected behaviour
stdOut = helper.CmdShouldPass("odo", "service", "list")
stdOut = helper.CmdShouldFail("odo", "service", "list")
jsonOut = helper.CmdShouldFail("odo", "service", "list", "-o", "json")
Expect(stdOut).To(ContainSubstring("No operator backed services found in the namesapce"))
helper.MatchAllInOutput(jsonOut, []string{"No operator backed services found in the namesapce", "\"message\": \"No operator backed services found in the namesapce\""})
})
})
})

0 comments on commit 1e24b53

Please sign in to comment.