Skip to content

Commit

Permalink
Add ability to get json for "minikube service list"
Browse files Browse the repository at this point in the history
  • Loading branch information
OmSaran committed Feb 11, 2023
1 parent 5c0c9e8 commit f0e2e38
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions cmd/minikube/cmd/service_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cmd

import (
"encoding/json"
"os"
"runtime"
"strings"
Expand All @@ -32,6 +33,7 @@ import (
)

var serviceListNamespace string
var profileOutput string

// serviceListCmd represents the service list command
var serviceListCmd = &cobra.Command{
Expand All @@ -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 {
Expand All @@ -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)
}

0 comments on commit f0e2e38

Please sign in to comment.