Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring: replace github.com/pkg/errors by standard errors #434

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions cmd/kubectx/current.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
package main

import (
"errors"
"fmt"
"io"

"github.com/pkg/errors"

"github.com/ahmetb/kubectx/internal/kubeconfig"
)

Expand All @@ -30,13 +29,16 @@ func (_op CurrentOp) Run(stdout, _ io.Writer) error {
kc := new(kubeconfig.Kubeconfig).WithLoader(kubeconfig.DefaultLoader)
defer kc.Close()
if err := kc.Parse(); err != nil {
return errors.Wrap(err, "kubeconfig error")
return fmt.Errorf("kubeconfig error: %w", err)
}

v := kc.GetCurrentContext()
if v == "" {
return errors.New("current-context is not set")
}
_, err := fmt.Fprintln(stdout, v)
return errors.Wrap(err, "write error")
if err != nil {
return fmt.Errorf("write error: %w", err)
}
return nil
}
12 changes: 6 additions & 6 deletions cmd/kubectx/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
package main

import (
"errors"
"fmt"
"io"

"github.com/pkg/errors"

"github.com/ahmetb/kubectx/internal/kubeconfig"
"github.com/ahmetb/kubectx/internal/printer"
)
Expand All @@ -34,7 +34,7 @@ func (op DeleteOp) Run(_, stderr io.Writer) error {
// TODO inefficiency here. we open/write/close the same file many times.
deletedName, wasActiveContext, err := deleteContext(ctx)
if err != nil {
return errors.Wrapf(err, "error deleting context \"%s\"", deletedName)
return fmt.Errorf("error deleting context \"%s\": %w", deletedName, err)
}
if wasActiveContext {
printer.Warning(stderr, "You deleted the current context. Use \"%s\" to select a new context.",
Expand All @@ -52,7 +52,7 @@ func deleteContext(name string) (deleteName string, wasActiveContext bool, err e
kc := new(kubeconfig.Kubeconfig).WithLoader(kubeconfig.DefaultLoader)
defer kc.Close()
if err := kc.Parse(); err != nil {
return deleteName, false, errors.Wrap(err, "kubeconfig error")
return deleteName, false, fmt.Errorf("kubeconfig error: %w", err)
}

cur := kc.GetCurrentContext()
Expand All @@ -70,7 +70,7 @@ func deleteContext(name string) (deleteName string, wasActiveContext bool, err e
}

if err := kc.DeleteContextEntry(name); err != nil {
return name, false, errors.Wrap(err, "failed to modify yaml doc")
return name, false, fmt.Errorf("failed to modify yaml doc: %w", err)
}
return name, wasActiveContext, errors.Wrap(kc.Save(), "failed to save modified kubeconfig file")
return name, wasActiveContext, fmt.Errorf("failed to save modified kubeconfig file: %w", kc.Save())
}
17 changes: 8 additions & 9 deletions cmd/kubectx/fzf.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ package main

import (
"bytes"
"errors"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"strings"

"github.com/pkg/errors"

"github.com/ahmetb/kubectx/internal/cmdutil"
"github.com/ahmetb/kubectx/internal/env"
"github.com/ahmetb/kubectx/internal/kubeconfig"
"github.com/ahmetb/kubectx/internal/printer"
Expand All @@ -42,11 +41,11 @@ func (op InteractiveSwitchOp) Run(_, stderr io.Writer) error {
// parse kubeconfig just to see if it can be loaded
kc := new(kubeconfig.Kubeconfig).WithLoader(kubeconfig.DefaultLoader)
if err := kc.Parse(); err != nil {
if cmdutil.IsNotFoundErr(err) {
if errors.Is(err, fs.ErrNotExist) {
printer.Warning(stderr, "kubeconfig file not found")
return nil
}
return errors.Wrap(err, "kubeconfig error")
return fmt.Errorf("kubeconfig error: %w", err)
}
kc.Close()

Expand All @@ -70,7 +69,7 @@ func (op InteractiveSwitchOp) Run(_, stderr io.Writer) error {
}
name, err := switchContext(choice)
if err != nil {
return errors.Wrap(err, "failed to switch context")
return fmt.Errorf("failed to switch context: %w", err)
}
printer.Success(stderr, "Switched to context \"%s\".", printer.SuccessColor.Sprint(name))
return nil
Expand All @@ -80,11 +79,11 @@ func (op InteractiveDeleteOp) Run(_, stderr io.Writer) error {
// parse kubeconfig just to see if it can be loaded
kc := new(kubeconfig.Kubeconfig).WithLoader(kubeconfig.DefaultLoader)
if err := kc.Parse(); err != nil {
if cmdutil.IsNotFoundErr(err) {
if errors.Is(err, fs.ErrNotExist) {
printer.Warning(stderr, "kubeconfig file not found")
return nil
}
return errors.Wrap(err, "kubeconfig error")
return fmt.Errorf("kubeconfig error: %w", err)
}
kc.Close()

Expand Down Expand Up @@ -114,7 +113,7 @@ func (op InteractiveDeleteOp) Run(_, stderr io.Writer) error {

name, wasActiveContext, err := deleteContext(choice)
if err != nil {
return errors.Wrap(err, "failed to delete context")
return fmt.Errorf("failed to delete context: %w", err)
}

if wasActiveContext {
Expand Down
7 changes: 4 additions & 3 deletions cmd/kubectx/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"os"
"path/filepath"
"strings"

"github.com/pkg/errors"
)

// HelpOp describes printing help.
Expand Down Expand Up @@ -49,7 +47,10 @@ func printUsage(out io.Writer) error {
help = strings.ReplaceAll(help, "%SPAC%", strings.Repeat(" ", len(selfName())))

_, err := fmt.Fprintf(out, "%s\n", help)
return errors.Wrap(err, "write error")
if err != nil {
return fmt.Errorf("write error: %w", err)
}
return nil
}

// selfName guesses how the user invoked the program.
Expand Down
8 changes: 4 additions & 4 deletions cmd/kubectx/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
package main

import (
"errors"
"fmt"
"io"
"io/fs"

"facette.io/natsort"
"github.com/pkg/errors"

"github.com/ahmetb/kubectx/internal/cmdutil"
"github.com/ahmetb/kubectx/internal/kubeconfig"
"github.com/ahmetb/kubectx/internal/printer"
)
Expand All @@ -33,11 +33,11 @@ func (_ ListOp) Run(stdout, stderr io.Writer) error {
kc := new(kubeconfig.Kubeconfig).WithLoader(kubeconfig.DefaultLoader)
defer kc.Close()
if err := kc.Parse(); err != nil {
if cmdutil.IsNotFoundErr(err) {
if errors.Is(err, fs.ErrNotExist) {
printer.Warning(stderr, "kubeconfig file not found")
return nil
}
return errors.Wrap(err, "kubeconfig error")
return fmt.Errorf("kubeconfig error: %w", err)
}

ctxs := kc.ContextNames()
Expand Down
15 changes: 7 additions & 8 deletions cmd/kubectx/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
package main

import (
"fmt"
"io"
"strings"

"github.com/pkg/errors"

"github.com/ahmetb/kubectx/internal/kubeconfig"
"github.com/ahmetb/kubectx/internal/printer"
)
Expand Down Expand Up @@ -51,7 +50,7 @@ func (op RenameOp) Run(_, stderr io.Writer) error {
kc := new(kubeconfig.Kubeconfig).WithLoader(kubeconfig.DefaultLoader)
defer kc.Close()
if err := kc.Parse(); err != nil {
return errors.Wrap(err, "kubeconfig error")
return fmt.Errorf("kubeconfig error: %w", err)
}

cur := kc.GetCurrentContext()
Expand All @@ -60,26 +59,26 @@ func (op RenameOp) Run(_, stderr io.Writer) error {
}

if !kc.ContextExists(op.Old) {
return errors.Errorf("context \"%s\" not found, can't rename it", op.Old)
return fmt.Errorf("context \"%s\" not found, can't rename it", op.Old)
}

if kc.ContextExists(op.New) {
printer.Warning(stderr, "context \"%s\" exists, overwriting it.", op.New)
if err := kc.DeleteContextEntry(op.New); err != nil {
return errors.Wrap(err, "failed to delete new context to overwrite it")
return fmt.Errorf("failed to delete new context to overwrite it: %w", err)
}
}

if err := kc.ModifyContextName(op.Old, op.New); err != nil {
return errors.Wrap(err, "failed to change context name")
return fmt.Errorf("failed to change context name: %w", err)
}
if op.Old == cur {
if err := kc.ModifyCurrentContext(op.New); err != nil {
return errors.Wrap(err, "failed to set current-context to new name")
return fmt.Errorf("failed to set current-context to new name: %w", err)
}
}
if err := kc.Save(); err != nil {
return errors.Wrap(err, "failed to save modified kubeconfig")
return fmt.Errorf("failed to save modified kubeconfig: %w", err)
}
printer.Success(stderr, "Context %s renamed to %s.",
printer.SuccessColor.Sprint(op.Old),
Expand Down
11 changes: 5 additions & 6 deletions cmd/kubectx/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
package main

import (
"io/ioutil"
"errors"
"fmt"
"os"
"path/filepath"

"github.com/pkg/errors"

"github.com/ahmetb/kubectx/internal/cmdutil"
)

Expand All @@ -35,7 +34,7 @@ func kubectxPrevCtxFile() (string, error) {
// readLastContext returns the saved previous context
// if the state file exists, otherwise returns "".
func readLastContext(path string) (string, error) {
b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
if os.IsNotExist(err) {
return "", nil
}
Expand All @@ -47,7 +46,7 @@ func readLastContext(path string) (string, error) {
func writeLastContext(path, value string) error {
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0755); err != nil {
return errors.Wrap(err, "failed to create parent directories")
return fmt.Errorf("failed to create parent directories: %w", err)
}
return ioutil.WriteFile(path, []byte(value), 0644)
return os.WriteFile(path, []byte(value), 0644)
}
25 changes: 14 additions & 11 deletions cmd/kubectx/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
package main

import (
"errors"
"fmt"
"io"

"github.com/pkg/errors"

"github.com/ahmetb/kubectx/internal/kubeconfig"
"github.com/ahmetb/kubectx/internal/printer"
)
Expand All @@ -37,39 +37,42 @@ func (op SwitchOp) Run(_, stderr io.Writer) error {
newCtx, err = switchContext(op.Target)
}
if err != nil {
return errors.Wrap(err, "failed to switch context")
return fmt.Errorf("failed to switch context: %w", err)
}
err = printer.Success(stderr, "Switched to context \"%s\".", printer.SuccessColor.Sprint(newCtx))
return errors.Wrap(err, "print error")
if err != nil {
return fmt.Errorf("print error: %w", err)
}
return nil
}

// switchContext switches to specified context name.
func switchContext(name string) (string, error) {
prevCtxFile, err := kubectxPrevCtxFile()
if err != nil {
return "", errors.Wrap(err, "failed to determine state file")
return "", fmt.Errorf("failed to determine state file: %w", err)
}

kc := new(kubeconfig.Kubeconfig).WithLoader(kubeconfig.DefaultLoader)
defer kc.Close()
if err := kc.Parse(); err != nil {
return "", errors.Wrap(err, "kubeconfig error")
return "", fmt.Errorf("kubeconfig error: %w", err)
}

prev := kc.GetCurrentContext()
if !kc.ContextExists(name) {
return "", errors.Errorf("no context exists with the name: \"%s\"", name)
return "", fmt.Errorf("no context exists with the name: \"%s\"", name)
}
if err := kc.ModifyCurrentContext(name); err != nil {
return "", err
}
if err := kc.Save(); err != nil {
return "", errors.Wrap(err, "failed to save kubeconfig")
return "", fmt.Errorf("failed to save kubeconfig: %w", err)
}

if prev != name {
if err := writeLastContext(prevCtxFile, prev); err != nil {
return "", errors.Wrap(err, "failed to save previous context name")
return "", fmt.Errorf("failed to save previous context name: %w", err)
}
}
return name, nil
Expand All @@ -79,11 +82,11 @@ func switchContext(name string) (string, error) {
func swapContext() (string, error) {
prevCtxFile, err := kubectxPrevCtxFile()
if err != nil {
return "", errors.Wrap(err, "failed to determine state file")
return "", fmt.Errorf("failed to determine state file: %w", err)
}
prev, err := readLastContext(prevCtxFile)
if err != nil {
return "", errors.Wrap(err, "failed to read previous context file")
return "", fmt.Errorf("failed to read previous context file: %w", err)
}
if prev == "" {
return "", errors.New("no previous context found")
Expand Down
14 changes: 8 additions & 6 deletions cmd/kubectx/unset.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
package main

import (
"fmt"
"io"

"github.com/pkg/errors"

"github.com/ahmetb/kubectx/internal/kubeconfig"
"github.com/ahmetb/kubectx/internal/printer"
)
Expand All @@ -30,16 +29,19 @@ func (_ UnsetOp) Run(_, stderr io.Writer) error {
kc := new(kubeconfig.Kubeconfig).WithLoader(kubeconfig.DefaultLoader)
defer kc.Close()
if err := kc.Parse(); err != nil {
return errors.Wrap(err, "kubeconfig error")
return fmt.Errorf("kubeconfig error: %w", err)
}

if err := kc.UnsetCurrentContext(); err != nil {
return errors.Wrap(err, "error while modifying current-context")
return fmt.Errorf("error while modifying current-context: %w", err)
}
if err := kc.Save(); err != nil {
return errors.Wrap(err, "failed to save kubeconfig file after modification")
return fmt.Errorf("failed to save kubeconfig file after modification: %w", err)
}

err := printer.Success(stderr, "Active context unset for kubectl.")
return errors.Wrap(err, "write error")
if err != nil {
return fmt.Errorf("write error: %w", err)
}
return nil
}
Loading