From f0e2e388dd367396907b0e4e7d4702b796fda092 Mon Sep 17 00:00:00 2001 From: Om Saran Date: Sat, 11 Feb 2023 15:33:11 -0600 Subject: [PATCH] Add ability to get json for "minikube service list" --- cmd/minikube/cmd/service_list.go | 46 ++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/cmd/minikube/cmd/service_list.go b/cmd/minikube/cmd/service_list.go index 97df14d1564b..23a6cdcb84f0 100644 --- a/cmd/minikube/cmd/service_list.go +++ b/cmd/minikube/cmd/service_list.go @@ -17,6 +17,7 @@ limitations under the License. package cmd import ( + "encoding/json" "os" "runtime" "strings" @@ -32,6 +33,7 @@ import ( ) var serviceListNamespace string +var profileOutput string // serviceListCmd represents the service list command var serviceListCmd = &cobra.Command{ @@ -40,6 +42,7 @@ var serviceListCmd = &cobra.Command{ Long: `Lists the URLs for the services in your local cluster`, Run: func(cmd *cobra.Command, args []string) { co := mustload.Healthy(ClusterFlagValue()) + output := strings.ToLower(profileOutput) serviceURLs, err := service.GetServiceURLs(co.API, co.Config.Name, serviceListNamespace, serviceURLTemplate) if err != nil { @@ -48,28 +51,43 @@ var serviceListCmd = &cobra.Command{ os.Exit(reason.ExSvcUnavailable) } - var data [][]string - for _, serviceURL := range serviceURLs { - if len(serviceURL.URLs) == 0 { - data = append(data, []string{serviceURL.Namespace, serviceURL.Name, "No node port"}) - } else { - servicePortNames := strings.Join(serviceURL.PortNames, "\n") - serviceURLs := strings.Join(serviceURL.URLs, "\n") + switch output { + case "table": + printServicesTable(serviceURLs, co) + case "json": + printServicesJSON(serviceURLs) + } + }, +} - // if we are running Docker on OSX we empty the internal service URLs - if runtime.GOOS == "darwin" && co.Config.Driver == oci.Docker { - serviceURLs = "" - } +func printServicesTable(serviceURLs service.URLs, co mustload.ClusterController) { + var data [][]string + for _, serviceURL := range serviceURLs { + if len(serviceURL.URLs) == 0 { + data = append(data, []string{serviceURL.Namespace, serviceURL.Name, "No node port"}) + } else { + servicePortNames := strings.Join(serviceURL.PortNames, "\n") + serviceURLs := strings.Join(serviceURL.URLs, "\n") - data = append(data, []string{serviceURL.Namespace, serviceURL.Name, servicePortNames, serviceURLs}) + // if we are running Docker on OSX we empty the internal service URLs + if runtime.GOOS == "darwin" && co.Config.Driver == oci.Docker { + serviceURLs = "" } + + data = append(data, []string{serviceURL.Namespace, serviceURL.Name, servicePortNames, serviceURLs}) } + } - service.PrintServiceList(os.Stdout, data) - }, + service.PrintServiceList(os.Stdout, data) +} + +func printServicesJSON(serviceURLs service.URLs) { + jsonString, _ := json.Marshal(serviceURLs) + os.Stdout.Write(jsonString) } func init() { + serviceListCmd.Flags().StringVarP(&profileOutput, "output", "o", "table", "The output format. One of 'json', 'table'") serviceListCmd.Flags().StringVarP(&serviceListNamespace, "namespace", "n", core.NamespaceAll, "The services namespace") serviceCmd.AddCommand(serviceListCmd) }