Skip to content

Commit

Permalink
return error instead of bool
Browse files Browse the repository at this point in the history
  • Loading branch information
enrichman committed Aug 22, 2024
1 parent ec53199 commit 22746bb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
18 changes: 9 additions & 9 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ func getKubeConfigForUser(ctx *cli.Context, user string) (*api.Config, error) {
return nil, err
}

focusedServer, found := cf.FocusedServer()
if !found {
return nil, errors.New("no focused server")
focusedServer, err := cf.FocusedServer()
if err != nil {
return nil, err
}

kubeConfig := focusedServer.KubeConfigs[fmt.Sprintf(kubeConfigKeyFormat, user, focusedServer.FocusedCluster())]
Expand All @@ -163,9 +163,9 @@ func setKubeConfigForUser(ctx *cli.Context, user string, kubeConfig *api.Config)
return err
}

focusedServer, found := cf.FocusedServer()
if !found {
return errors.New("no focused server")
focusedServer, err := cf.FocusedServer()
if err != nil {
return err
}

if focusedServer.KubeConfigs == nil {
Expand Down Expand Up @@ -282,9 +282,9 @@ func lookupConfig(ctx *cli.Context) (*config.ServerConfig, error) {
return nil, err
}

cs, found := cf.FocusedServer()
if !found {
return nil, errors.New("no configuration found, run `login`")
cs, err := cf.FocusedServer()
if err != nil {
return nil, err
}

return cs, nil
Expand Down
8 changes: 3 additions & 5 deletions cmd/context.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"errors"

"github.com/rancher/cli/cliclient"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
Expand Down Expand Up @@ -41,9 +39,9 @@ func contextSwitch(ctx *cli.Context) error {
return err
}

server, found := cf.FocusedServer()
if !found {
return errors.New("no focused server")
server, err := cf.FocusedServer()
if err != nil {
return err
}

c, err := cliclient.NewManagementClient(server)
Expand Down
7 changes: 3 additions & 4 deletions cmd/kubectl.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"errors"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -47,9 +46,9 @@ func runKubectl(ctx *cli.Context) error {
return err
}

currentRancherServer, found := config.FocusedServer()
if !found {
return errors.New("no focused server")
currentRancherServer, err := config.FocusedServer()
if err != nil {
return err
}

currentToken := currentRancherServer.AccessKey
Expand Down
10 changes: 8 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
Expand All @@ -12,6 +13,8 @@ import (
"k8s.io/client-go/tools/clientcmd/api"
)

var ErrNoServerSelected = errors.New("no server selected")

// Config holds the main config for the user
type Config struct {
Servers map[string]*ServerConfig
Expand Down Expand Up @@ -100,9 +103,12 @@ func (c Config) Write() error {
return json.NewEncoder(output).Encode(c)
}

func (c Config) FocusedServer() (*ServerConfig, bool) {
func (c Config) FocusedServer() (*ServerConfig, error) {
currentServer, found := c.Servers[c.CurrentServer]
return currentServer, found
if !found || currentServer == nil {
return nil, ErrNoServerSelected
}
return currentServer, nil
}

func (c ServerConfig) FocusedCluster() string {
Expand Down

0 comments on commit 22746bb

Please sign in to comment.