Skip to content

Commit

Permalink
Add IsNotExist() method to the config package, so that we can have me…
Browse files Browse the repository at this point in the history
…aningful error messages and make them comparable
  • Loading branch information
tstromberg committed Jan 31, 2020
1 parent a7ac033 commit c3aafae
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 17 deletions.
3 changes: 2 additions & 1 deletion cmd/minikube/cmd/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
pkgConfig "k8s.io/minikube/pkg/minikube/config"
pkg_config "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/kubeconfig"
Expand Down Expand Up @@ -78,7 +79,7 @@ var ProfileCmd = &cobra.Command{
}
cc, err := pkgConfig.Load(profile)
// might err when loading older version of cfg file that doesn't have KeepContext field
if err != nil && !os.IsNotExist(err) {
if err != nil && !pkg_config.IsNotExist(err) {
out.ErrT(out.Sad, `Error loading profile config: {{.error}}`, out.V{"error": err})
}
if err == nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/config/profile_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ var printProfilesJSON = func() {

var body = map[string]interface{}{}

if err == nil || os.IsNotExist(err) {
if err == nil || config.IsNotExist(err) {
body["valid"] = valid
body["invalid"] = invalid
jsonString, _ := json.Marshal(body)
Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var dashboardCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
profileName := viper.GetString(pkg_config.MachineProfile)
cc, err := pkg_config.Load(profileName)
if err != nil && !os.IsNotExist(err) {
if err != nil && !pkg_config.IsNotExist(err) {
exit.WithError("Error loading profile config", err)
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/minikube/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func deleteProfile(profile *pkg_config.Profile) error {
}
defer api.Close()
cc, err := pkg_config.Load(profile.Name)
if err != nil && !os.IsNotExist(err) {
if err != nil && !pkg_config.IsNotExist(err) {
delErr := profileDeletionErr(profile.Name, fmt.Sprintf("error loading profile config: %v", err))
return DeletionError{Err: delErr, Errtype: MissingProfile}
}
Expand Down Expand Up @@ -223,7 +223,7 @@ func deleteProfile(profile *pkg_config.Profile) error {
deleteProfileDirectory(profile.Name)

if err := pkg_config.DeleteProfile(profile.Name); err != nil {
if os.IsNotExist(err) {
if pkg_config.IsNotExist(err) {
delErr := profileDeletionErr(profile.Name, fmt.Sprintf("\"%s\" profile does not exist", profile.Name))
return DeletionError{Err: delErr, Errtype: MissingProfile}
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/minikube/cmd/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/minikube/pkg/minikube/config"
pkg_config "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/machine"
Expand All @@ -51,8 +50,8 @@ minikube kubectl -- get pods --namespace kube-system`,
}
defer api.Close()

cc, err := pkg_config.Load(viper.GetString(config.MachineProfile))
if err != nil && !os.IsNotExist(err) {
cc, err := config.Load(viper.GetString(config.MachineProfile))
if err != nil && !config.IsNotExist(err) {
out.ErrLn("Error loading profile config: %v", err)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func runPause(cmd *cobra.Command, args []string) {
defer api.Close()
cc, err := config.Load(cname)

if err != nil && !os.IsNotExist(err) {
if err != nil && !config.IsNotExist(err) {
exit.WithError("Error loading profile config", err)
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func runStart(cmd *cobra.Command, args []string) {
}

existing, err := config.Load(viper.GetString(config.MachineProfile))
if err != nil && !os.IsNotExist(err) {
if err != nil && !config.IsNotExist(err) {
exit.WithCodeT(exit.Data, "Unable to load config: {{.error}}", out.V{"error": err})
}

Expand Down Expand Up @@ -751,7 +751,7 @@ func validateUser(drvName string) {
os.Exit(exit.Permissions)
}
_, err = config.Load(viper.GetString(config.MachineProfile))
if err == nil || !os.IsNotExist(err) {
if err == nil || !config.IsNotExist(err) {
out.T(out.Tip, "Tip: To remove this root owned cluster, run: sudo {{.cmd}} delete", out.V{"cmd": minikubeCmd()})
}
if !useForce {
Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/unpause.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var unpauseCmd = &cobra.Command{
defer api.Close()
cc, err := config.Load(cname)

if err != nil && !os.IsNotExist(err) {
if err != nil && !config.IsNotExist(err) {
exit.WithError("Error loading profile config", err)
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/addons/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package addons

import (
"fmt"
"os"
"path"
"strconv"
"strings"
Expand Down Expand Up @@ -140,7 +139,7 @@ func enableOrDisableAddon(name, val, profile string) error {
defer api.Close()

cfg, err := config.Load(profile)
if err != nil && !os.IsNotExist(err) {
if err != nil && !config.IsNotExist(err) {
exit.WithCodeT(exit.Data, "Unable to load config: {{.error}}", out.V{"error": err})
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/addons/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package addons

import (
"os"
"os/exec"
"path"

Expand Down Expand Up @@ -53,7 +52,7 @@ func kubectlCommand(profile string, files []string, enable bool) (*exec.Cmd, err

func kubernetesVersion(profile string) (string, error) {
cc, err := config.Load(profile)
if err != nil && !os.IsNotExist(err) {
if err != nil && !config.IsNotExist(err) {
return "", err
}
version := constants.DefaultKubernetesVersion
Expand Down
19 changes: 18 additions & 1 deletion pkg/minikube/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ var (
ErrKeyNotFound = errors.New("specified key could not be found in config")
)

type ErrNotExist struct {
s string
}

func (e *ErrNotExist) Error() string {
return e.s
}

// IsNotExist returns whether the error means a nonexistent configuration
func IsNotExist(err error) bool {
switch err.(type) {
case *ErrNotExist:
return true
}
return false
}

// MinikubeConfig represents minikube config
type MinikubeConfig map[string]interface{}

Expand Down Expand Up @@ -151,7 +168,7 @@ func (c *simpleConfigLoader) LoadConfigFromFile(profileName string, miniHome ...

if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("cluster %q does not exist", profileName)
return nil, &ErrNotExist{fmt.Sprintf("cluster %q does not exist", profileName)}
}
return nil, errors.Wrap(err, "stat")
}
Expand Down

0 comments on commit c3aafae

Please sign in to comment.