Skip to content

Commit

Permalink
chore(go): replace github.com/pkg/errors by standard errors
Browse files Browse the repository at this point in the history
  • Loading branch information
RouxAntoine committed Dec 28, 2024
1 parent 561793c commit 93a14bd
Show file tree
Hide file tree
Showing 24 changed files with 123 additions and 108 deletions.
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())
}
11 changes: 5 additions & 6 deletions cmd/kubectx/fzf.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ package main

import (
"bytes"
"errors"
"fmt"
"io"
"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"
Expand All @@ -46,7 +45,7 @@ func (op InteractiveSwitchOp) Run(_, stderr io.Writer) error {
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 @@ -84,7 +83,7 @@ func (op InteractiveDeleteOp) Run(_, stderr io.Writer) error {
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
3 changes: 1 addition & 2 deletions cmd/kubectx/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"io"

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

"github.com/ahmetb/kubectx/internal/cmdutil"
"github.com/ahmetb/kubectx/internal/kubeconfig"
Expand All @@ -37,7 +36,7 @@ func (_ ListOp) Run(stdout, stderr io.Writer) error {
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
6 changes: 3 additions & 3 deletions cmd/kubectx/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package main

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

"github.com/pkg/errors"

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

Expand All @@ -47,7 +47,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)
}
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
}
7 changes: 4 additions & 3 deletions cmd/kubectx/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package main
import (
"fmt"
"io"

"github.com/pkg/errors"
)

var (
Expand All @@ -16,5 +14,8 @@ type VersionOp struct{}

func (_ VersionOp) Run(stdout, _ io.Writer) error {
_, err := fmt.Fprintf(stdout, "%s\n", version)
return errors.Wrap(err, "write error")
if err != nil {
return fmt.Errorf("write error: %w", err)
}
return nil
}
Loading

0 comments on commit 93a14bd

Please sign in to comment.