<commit_before>/*
Copyright 2016 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
	"fmt"
	"os"
	"text/template"

	"github.com/docker/machine/libmachine/state"
	"github.com/golang/glog"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	cmdcfg "k8s.io/minikube/cmd/minikube/cmd/config"
	cmdUtil "k8s.io/minikube/cmd/util"
	"k8s.io/minikube/pkg/minikube/cluster"
	"k8s.io/minikube/pkg/minikube/config"
	"k8s.io/minikube/pkg/minikube/constants"
	"k8s.io/minikube/pkg/minikube/machine"
	"k8s.io/minikube/pkg/util/kubeconfig"
)

var statusFormat string

type Status struct {
	Host       string
	Kubelet    string
	ApiServer  string
	Kubeconfig string
}

const internalErrorCode = -1

const (
	minikubeNotRunningStatusFlag = 1 << 0
	clusterNotRunningStatusFlag  = 1 << 1
	k8sNotRunningStatusFlag      = 1 << 2
)

// statusCmd represents the status command
var statusCmd = &cobra.Command{
	Use:   "status",
	Short: "Gets the status of a local kubernetes cluster",
	Long: `Gets the status of a local kubernetes cluster.
	Exit status contains the status of minikube's VM, cluster and kubernetes encoded on it's bits in this order from right to left.
	Eg: 7 meaning: 1 (for minikube NOK) + 2 (for cluster NOK) + 4 (for kubernetes NOK)`,
	Run: func(cmd *cobra.Command, args []string) {
		var returnCode = 0
		api, err := machine.NewAPIClient()
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error getting client: %v\n", err)
			os.Exit(internalErrorCode)
		}
		defer api.Close()

		hostSt, err := cluster.GetHostStatus(api)
		if err != nil {
			glog.Errorln("Error getting machine status:", err)
			cmdUtil.MaybeReportErrorAndExitWithCode(err, internalErrorCode)
		}

		kubeletSt := state.None.String()
		kubeconfigSt := state.None.String()
		apiserverSt := state.None.String()

		if hostSt == state.Running.String() {
			clusterBootstrapper, err := GetClusterBootstrapper(api, viper.GetString(cmdcfg.Bootstrapper))
			if err != nil {
				glog.Errorf("Error getting cluster bootstrapper: %v", err)
				cmdUtil.MaybeReportErrorAndExitWithCode(err, internalErrorCode)
			}
			kubeletSt, err = clusterBootstrapper.GetKubeletStatus()
			if err != nil {
				glog.Errorln("Error kubelet status:", err)
				cmdUtil.MaybeReportErrorAndExitWithCode(err, internalErrorCode)
			} else if kubeletSt != state.Running.String() {
				returnCode |= clusterNotRunningStatusFlag
			}

			ip, err := cluster.GetHostDriverIP(api, config.GetMachineName())
			if err != nil {
				glog.Errorln("Error host driver ip status:", err)
				cmdUtil.MaybeReportErrorAndExitWithCode(err, internalErrorCode)
			}

			apiserverSt, err = clusterBootstrapper.GetApiServerStatus(ip)
			if err != nil {
				glog.Errorln("Error apiserver status:", err)
				cmdUtil.MaybeReportErrorAndExitWithCode(err, internalErrorCode)
			} else if apiserverSt != state.Running.String() {
				returnCode |= clusterNotRunningStatusFlag
			}

			ks, err := kubeconfig.GetKubeConfigStatus(ip, cmdUtil.GetKubeConfigPath(), config.GetMachineName())
			if err != nil {
				glog.Errorln("Error kubeconfig status:", err)
				cmdUtil.MaybeReportErrorAndExitWithCode(err, internalErrorCode)
			}
			if ks {
				kubeconfigSt = "Correctly Configured: pointing to minikube-vm at " + ip.String()
			} else {
				kubeconfigSt = "Misconfigured: pointing to stale minikube-vm." +
					"\nTo fix the kubectl context, run minikube update-context"
				returnCode |= k8sNotRunningStatusFlag
			}
		} else {
			returnCode |= minikubeNotRunningStatusFlag
		}

		status := Status{
			Host:       hostSt,
			Kubelet:    kubeletSt,
			ApiServer:  apiserverSt,
			Kubeconfig: kubeconfigSt,
		}
		tmpl, err := template.New("status").Parse(statusFormat)
		if err != nil {
			glog.Errorln("Error creating status template:", err)
			os.Exit(internalErrorCode)
		}
		err = tmpl.Execute(os.Stdout, status)
		if err != nil {
			glog.Errorln("Error executing status template:", err)
			os.Exit(internalErrorCode)
		}

		os.Exit(returnCode)
	},
}

func init() {
	statusCmd.Flags().StringVar(&statusFormat, "format", constants.DefaultStatusFormat,
		`Go template format string for the status output.  The format for Go templates can be found here: https://golang.org/pkg/text/template/
For the list accessible variables for the template, see the struct values here: https://godoc.org/k8s.io/minikube/cmd/minikube/cmd#Status`)
	RootCmd.AddCommand(statusCmd)
}
<commit_msg>issue# 3499: minikube status missing newline at end of output<commit_after>/*
Copyright 2016 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
	"fmt"
	"os"
	"text/template"

	"github.com/docker/machine/libmachine/state"
	"github.com/golang/glog"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	cmdcfg "k8s.io/minikube/cmd/minikube/cmd/config"
	cmdUtil "k8s.io/minikube/cmd/util"
	"k8s.io/minikube/pkg/minikube/cluster"
	"k8s.io/minikube/pkg/minikube/config"
	"k8s.io/minikube/pkg/minikube/constants"
	"k8s.io/minikube/pkg/minikube/machine"
	"k8s.io/minikube/pkg/util/kubeconfig"
)

var statusFormat string

type Status struct {
	Host       string
	Kubelet    string
	ApiServer  string
	Kubeconfig string
}

const internalErrorCode = -1

const (
	minikubeNotRunningStatusFlag = 1 << 0
	clusterNotRunningStatusFlag  = 1 << 1
	k8sNotRunningStatusFlag      = 1 << 2
)

// statusCmd represents the status command
var statusCmd = &cobra.Command{
	Use:   "status",
	Short: "Gets the status of a local kubernetes cluster",
	Long: `Gets the status of a local kubernetes cluster.
	Exit status contains the status of minikube's VM, cluster and kubernetes encoded on it's bits in this order from right to left.
	Eg: 7 meaning: 1 (for minikube NOK) + 2 (for cluster NOK) + 4 (for kubernetes NOK)`,
	Run: func(cmd *cobra.Command, args []string) {
		var returnCode = 0
		api, err := machine.NewAPIClient()
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error getting client: %v\n", err)
			os.Exit(internalErrorCode)
		}
		defer api.Close()

		hostSt, err := cluster.GetHostStatus(api)
		if err != nil {
			glog.Errorln("Error getting machine status:", err)
			cmdUtil.MaybeReportErrorAndExitWithCode(err, internalErrorCode)
		}

		kubeletSt := state.None.String()
		kubeconfigSt := state.None.String()
		apiserverSt := state.None.String()

		if hostSt == state.Running.String() {
			clusterBootstrapper, err := GetClusterBootstrapper(api, viper.GetString(cmdcfg.Bootstrapper))
			if err != nil {
				glog.Errorf("Error getting cluster bootstrapper: %v", err)
				cmdUtil.MaybeReportErrorAndExitWithCode(err, internalErrorCode)
			}
			kubeletSt, err = clusterBootstrapper.GetKubeletStatus()
			if err != nil {
				glog.Errorln("Error kubelet status:", err)
				cmdUtil.MaybeReportErrorAndExitWithCode(err, internalErrorCode)
			} else if kubeletSt != state.Running.String() {
				returnCode |= clusterNotRunningStatusFlag
			}

			ip, err := cluster.GetHostDriverIP(api, config.GetMachineName())
			if err != nil {
				glog.Errorln("Error host driver ip status:", err)
				cmdUtil.MaybeReportErrorAndExitWithCode(err, internalErrorCode)
			}

			apiserverSt, err = clusterBootstrapper.GetApiServerStatus(ip)
			if err != nil {
				glog.Errorln("Error apiserver status:", err)
				cmdUtil.MaybeReportErrorAndExitWithCode(err, internalErrorCode)
			} else if apiserverSt != state.Running.String() {
				returnCode |= clusterNotRunningStatusFlag
			}

			ks, err := kubeconfig.GetKubeConfigStatus(ip, cmdUtil.GetKubeConfigPath(), config.GetMachineName())
			if err != nil {
				glog.Errorln("Error kubeconfig status:", err)
				cmdUtil.MaybeReportErrorAndExitWithCode(err, internalErrorCode)
			}
			if ks {
				kubeconfigSt = "Correctly Configured: pointing to minikube-vm at " + ip.String() + "\n"
			} else {
				kubeconfigSt = "Misconfigured: pointing to stale minikube-vm." +
					"\nTo fix the kubectl context, run minikube update-context"
				returnCode |= k8sNotRunningStatusFlag
			}
		} else {
			returnCode |= minikubeNotRunningStatusFlag
		}

		status := Status{
			Host:       hostSt,
			Kubelet:    kubeletSt,
			ApiServer:  apiserverSt,
			Kubeconfig: kubeconfigSt,
		}
		tmpl, err := template.New("status").Parse(statusFormat)
		if err != nil {
			glog.Errorln("Error creating status template:", err)
			os.Exit(internalErrorCode)
		}
		err = tmpl.Execute(os.Stdout, status)
		if err != nil {
			glog.Errorln("Error executing status template:", err)
			os.Exit(internalErrorCode)
		}

		os.Exit(returnCode)
	},
}

func init() {
	statusCmd.Flags().StringVar(&statusFormat, "format", constants.DefaultStatusFormat,
		`Go template format string for the status output.  The format for Go templates can be found here: https://golang.org/pkg/text/template/
For the list accessible variables for the template, see the struct values here: https://godoc.org/k8s.io/minikube/cmd/minikube/cmd#Status`)
	RootCmd.AddCommand(statusCmd)
}

{'fraction_non_alphanumeric': 0.10640234651562658, 'fraction_numerical': 0.007282289875594214, 'mean_word_length': 4.12597200622084, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1175756', 'n_tokens_mistral': 3579, 'n_tokens_neox': 3239, 'n_words': 862}