Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON output for service list in experimental mode #3186

Merged
merged 2 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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\""})
})
})
})