Skip to content

Commit

Permalink
fix: update error messaging for docker and kubernetes specific failur…
Browse files Browse the repository at this point in the history
…es (#13)
  • Loading branch information
colesnodgrass authored Apr 23, 2024
1 parent 205330f commit 947a061
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
20 changes: 16 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@ import (
"github.com/spf13/cobra"
)

// dockerHelp is displayed if ErrDocker is ever returned
const dockerHelp = `An error occurred while communicating with the Docker daemon.
Ensure that Docker is running and is accessible. You may need to upgrade to a newer version of Docker.`
// Help messages to display for specific error situations.
const (
// helpDocker is displayed if ErrDocker is ever returned
helpDocker = `An error occurred while communicating with the Docker daemon.
Ensure that Docker is running and is accessible. You may need to upgrade to a newer version of Docker.
For additional help please visit https://docs.docker.com/get-docker/`

// helpKubernetes is displayed if ErrKubernetes is ever returned
helpKubernetes = `An error occurred while communicating with the Kubernetes cluster.
If using Docker Desktop, ensure that Kubernetes is enabled.
For additional help please visit https://docs.docker.com/desktop/kubernetes/`
)

var (
// flagDNT indicates if the do-not-track flag was specified
Expand All @@ -39,7 +48,10 @@ func Execute() {

if errors.Is(err, localcmd.ErrDocker) {
pterm.Println()
pterm.Info.Println(dockerHelp)
pterm.Info.Println(helpDocker)
} else if errors.Is(err, localcmd.ErrKubernetes) {
pterm.Println()
pterm.Info.Println(helpKubernetes)
}
os.Exit(1)
}
Expand Down
23 changes: 15 additions & 8 deletions internal/local/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,17 @@ type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}

// BrowserLauncher for
// BrowserLauncher primarily for testing purposes.
type BrowserLauncher func(url string) error

// ErrDocker is returned anytime an error specific to docker occurs.
var ErrDocker = errors.New("error communicating with docker")
// Errors related to specific systems that this code integrates with.
var (
// ErrDocker is returned anytime an error occurs when attempting to communicate with docker.
ErrDocker = errors.New("error communicating with docker")

// ErrKubernetes is returned anytime an error occurs when attempting to communicate with the kubernetes cluster
ErrKubernetes = errors.New("error communicating with kubernetes")
)

// DockerClient primarily for testing purposes
type DockerClient interface {
Expand Down Expand Up @@ -175,16 +181,17 @@ func New(opts ...Option) (*Command, error) {
if c.k8s == nil {
k8sCfg, err := k8sClientConfig(c.userHome)
if err != nil {
return nil, fmt.Errorf("could not create k8s client config: %w", err)
return nil, fmt.Errorf("%w: %w", ErrKubernetes, err)
}

restCfg, err := k8sCfg.ClientConfig()
if err != nil {
return nil, fmt.Errorf("could not create k8s config client: %w", err)
return nil, fmt.Errorf("%w: could not create rest config: %w", ErrKubernetes, err)
}

k8s, err := kubernetes.NewForConfig(restCfg)
if err != nil {
return nil, fmt.Errorf("could not create k8s client: %w", err)
return nil, fmt.Errorf("%w: could not create clientset: %w", ErrKubernetes, err)
}

c.k8s = &defaultK8sClient{k8s: k8s}
Expand All @@ -207,7 +214,7 @@ func New(opts ...Option) (*Command, error) {
RestConfig: restCfg,
})
if err != nil {
return nil, fmt.Errorf("coud not create helm client: %w", err)
return nil, fmt.Errorf("could not create helm client: %w", err)
}

c.helm = helm
Expand Down Expand Up @@ -237,7 +244,7 @@ func New(opts ...Option) (*Command, error) {
{
k8sVersion, err := c.k8s.GetServerVersion()
if err != nil {
return nil, fmt.Errorf("could not fetch k8s server version: %w", err)
return nil, fmt.Errorf("%w: could not fetch kubernetes server version: %w", ErrKubernetes, err)
}
c.tel.Attr("k8s_version", k8sVersion)
}
Expand Down

0 comments on commit 947a061

Please sign in to comment.