diff --git a/cmd/minikube/cmd/config/profiles.go b/cmd/minikube/cmd/config/profiles.go new file mode 100644 index 000000000000..ba47fdf64b65 --- /dev/null +++ b/cmd/minikube/cmd/config/profiles.go @@ -0,0 +1,52 @@ +/* +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 config + +import ( + "os" + "github.com/spf13/cobra" + "k8s.io/minikube/pkg/minikube/console" + "k8s.io/minikube/pkg/minikube/exit" + pkgutil "k8s.io/minikube/pkg/util" + "k8s.io/minikube/pkg/minikube/constants" +) + +// ProfileCmd represents the profile command +var ProfilesCmd = &cobra.Command{ + Use: "profiles", + Short: "profiles gets the list of all the present profiles", + Long: "profiles displays a list of all the profiles which have been created earlier.", + Run: func(cmd *cobra.Command, args []string) { + if len(args) > 1 { + exit.Usage("usage: minikube profile [MINIKUBE_PROFILE_NAME]") + } + profiles, err := pkgutil.GetProfiles(constants.KubeconfigPath) + if err != nil { + exit.WithError("Failed to fetch profiles", err) + } + + // check length og profiles, if zero then print suitable message + if len(profiles) == 0 { + console.OutLn("No profiles created yet") + os.Exit(0) + } + + for _, profile := range profiles { + console.OutLn(profile) + } + }, +} diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go index c8094aa03372..468662ac9fd7 100644 --- a/cmd/minikube/cmd/root.go +++ b/cmd/minikube/cmd/root.go @@ -132,6 +132,7 @@ func init() { RootCmd.AddCommand(configCmd.ConfigCmd) RootCmd.AddCommand(configCmd.AddonsCmd) RootCmd.AddCommand(configCmd.ProfileCmd) + RootCmd.AddCommand(configCmd.ProfilesCmd) pflag.CommandLine.AddGoFlagSet(goflag.CommandLine) if err := viper.BindPFlags(RootCmd.PersistentFlags()); err != nil { exit.WithError("Unable to bind flags", err) diff --git a/pkg/util/kubeconfig.go b/pkg/util/kubeconfig.go index 887c7a884111..35d0f824916d 100644 --- a/pkg/util/kubeconfig.go +++ b/pkg/util/kubeconfig.go @@ -325,3 +325,16 @@ func UnsetCurrentContext(filename, machineName string) error { } return nil } + +//Fetch all the profiles and return +func GetProfiles(filename string) ([]string, error) { + config, err := ReadConfigOrNew(filename) + if err != nil { + return nil, errors.Wrap(err, "Error getting kubeconfig status") + } + profiles := make([]string, 0, 4) + for context, _ := range config.Contexts { + profiles = append(profiles, context) + } + return profiles, nil +}