Skip to content

Commit

Permalink
Merge pull request #166 from AkihiroSuda/remove-pkg-errors
Browse files Browse the repository at this point in the history
remove direct dependency on github.com/pkg/errors
  • Loading branch information
AkihiroSuda authored Aug 22, 2021
2 parents b499494 + 34bb35b commit 8092199
Show file tree
Hide file tree
Showing 27 changed files with 156 additions and 160 deletions.
2 changes: 1 addition & 1 deletion cmd/lima-guestagent/daemon_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"path/filepath"
"time"

"github.com/gorilla/mux"
"github.com/lima-vm/lima/pkg/guestagent"
"github.com/lima-vm/lima/pkg/guestagent/api/server"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
Expand Down
10 changes: 5 additions & 5 deletions cmd/limactl/copy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"os"
"os/exec"
Expand All @@ -9,7 +10,6 @@ import (

"github.com/lima-vm/lima/pkg/sshutil"
"github.com/lima-vm/lima/pkg/store"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
Expand All @@ -25,7 +25,7 @@ var copyCommand = &cli.Command{

func copyAction(clicontext *cli.Context) error {
if clicontext.NArg() < 2 {
return errors.Errorf("requires at least 2 arguments: SOURCE DEST")
return fmt.Errorf("requires at least 2 arguments: SOURCE DEST")
}
arg0, err := exec.LookPath("scp")
if err != nil {
Expand All @@ -52,16 +52,16 @@ func copyAction(clicontext *cli.Context) error {
inst, err := store.Inspect(instName)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return errors.Errorf("instance %q does not exist, run `limactl start %s` to create a new instance", instName, instName)
return fmt.Errorf("instance %q does not exist, run `limactl start %s` to create a new instance", instName, instName)
}
return err
}
if inst.Status == store.StatusStopped {
return errors.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
}
args = append(args, fmt.Sprintf("scp://%s@127.0.0.1:%d/%s", u.Username, inst.SSHLocalPort, path[1]))
default:
return errors.Errorf("Path %q contains multiple colons", arg)
return fmt.Errorf("Path %q contains multiple colons", arg)
}
}
cmd := exec.Command(arg0, args...)
Expand Down
11 changes: 6 additions & 5 deletions cmd/limactl/delete.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
"errors"
"fmt"
"os"

"github.com/lima-vm/lima/pkg/store"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
Expand All @@ -27,7 +28,7 @@ var deleteCommand = &cli.Command{

func deleteAction(clicontext *cli.Context) error {
if clicontext.NArg() == 0 {
return errors.Errorf("requires at least 1 argument")
return fmt.Errorf("requires at least 1 argument")
}
force := clicontext.Bool("force")
for _, instName := range clicontext.Args().Slice() {
Expand All @@ -40,7 +41,7 @@ func deleteAction(clicontext *cli.Context) error {
return err
}
if err := deleteInstance(inst, force); err != nil {
return errors.Wrapf(err, "failed to delete instance %q", instName)
return fmt.Errorf("failed to delete instance %q: %w", instName, err)
}
logrus.Infof("Deleted %q (%q)", instName, inst.Dir)
}
Expand All @@ -49,13 +50,13 @@ func deleteAction(clicontext *cli.Context) error {

func deleteInstance(inst *store.Instance, force bool) error {
if !force && inst.Status != store.StatusStopped {
return errors.Errorf("expected status %q, got %q", store.StatusStopped, inst.Status)
return fmt.Errorf("expected status %q, got %q", store.StatusStopped, inst.Status)
}

stopInstanceForcibly(inst)

if err := os.RemoveAll(inst.Dir); err != nil {
return errors.Wrapf(err, "failed to remove %q", inst.Dir)
return fmt.Errorf("failed to remove %q: %w", inst.Dir, err)
}
return nil
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/limactl/hostagent.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package main

import (
"errors"
"fmt"
"io"
"os"
"os/signal"
"strconv"

"github.com/lima-vm/lima/pkg/hostagent"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)

Expand All @@ -29,7 +30,7 @@ var hostagentCommand = &cli.Command{
func hostagentAction(clicontext *cli.Context) error {
if pidfile := clicontext.String("pidfile"); pidfile != "" {
if _, err := os.Stat(pidfile); !errors.Is(err, os.ErrNotExist) {
return errors.Errorf("pidfile %q already exists", pidfile)
return fmt.Errorf("pidfile %q already exists", pidfile)
}
if err := os.WriteFile(pidfile, []byte(strconv.Itoa(os.Getpid())+"\n"), 0644); err != nil {
return err
Expand All @@ -38,7 +39,7 @@ func hostagentAction(clicontext *cli.Context) error {
}

if clicontext.NArg() != 1 {
return errors.Errorf("requires exactly 1 argument")
return fmt.Errorf("requires exactly 1 argument")
}

instName := clicontext.Args().First()
Expand Down
2 changes: 1 addition & 1 deletion cmd/limactl/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"text/tabwriter"

"github.com/lima-vm/lima/pkg/store"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
Expand Down
8 changes: 4 additions & 4 deletions cmd/limactl/shell.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"os"
"os/exec"
Expand All @@ -11,7 +12,6 @@ import (
"github.com/lima-vm/lima/pkg/sshutil"
"github.com/lima-vm/lima/pkg/store"
"github.com/mattn/go-isatty"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
Expand All @@ -34,7 +34,7 @@ var shellCommand = &cli.Command{

func shellAction(clicontext *cli.Context) error {
if clicontext.NArg() == 0 {
return errors.Errorf("requires at least 1 argument")
return fmt.Errorf("requires at least 1 argument")
}
instName := clicontext.Args().First()

Expand All @@ -50,12 +50,12 @@ func shellAction(clicontext *cli.Context) error {
inst, err := store.Inspect(instName)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return errors.Errorf("instance %q does not exist, run `limactl start %s` to create a new instance", instName, instName)
return fmt.Errorf("instance %q does not exist, run `limactl start %s` to create a new instance", instName, instName)
}
return err
}
if inst.Status == store.StatusStopped {
return errors.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
}
y, err := inst.LoadYAML()
if err != nil {
Expand Down
20 changes: 10 additions & 10 deletions cmd/limactl/start.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"io/ioutil"
"os"
Expand All @@ -17,7 +18,6 @@ import (
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/mattn/go-isatty"
"github.com/norouter/norouter/cmd/norouter/editorcmd"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
Expand All @@ -39,7 +39,7 @@ var startCommand = &cli.Command{

func loadOrCreateInstance(clicontext *cli.Context) (*store.Instance, error) {
if clicontext.NArg() > 1 {
return nil, errors.Errorf("too many arguments")
return nil, fmt.Errorf("too many arguments")
}

arg := clicontext.Args().First()
Expand Down Expand Up @@ -67,7 +67,7 @@ func loadOrCreateInstance(clicontext *cli.Context) (*store.Instance, error) {
instName = arg
logrus.Debugf("interpreting argument %q as an instance name %q", arg, instName)
if err := identifiers.Validate(instName); err != nil {
return nil, errors.Wrapf(err, "argument must be either an instance name or a YAML file path, got %q", instName)
return nil, fmt.Errorf("argument must be either an instance name or a YAML file path, got %q: %w", instName, err)
}
if inst, err := store.Inspect(instName); err == nil {
logrus.Infof("Using the existing instance %q", instName)
Expand All @@ -87,11 +87,11 @@ func loadOrCreateInstance(clicontext *cli.Context) (*store.Instance, error) {
// the full path of the socket name must be less than UNIX_PATH_MAX chars.
maxSockName := filepath.Join(instDir, filenames.LongestSock)
if len(maxSockName) >= osutil.UnixPathMax {
return nil, errors.Errorf("instance name %q too long: %q must be less than UNIX_PATH_MAX=%d characers, but is %d",
return nil, fmt.Errorf("instance name %q too long: %q must be less than UNIX_PATH_MAX=%d characers, but is %d",
instName, maxSockName, osutil.UnixPathMax, len(maxSockName))
}
if _, err := os.Stat(instDir); !errors.Is(err, os.ErrNotExist) {
return nil, errors.Errorf("instance %q already exists (%q)", instName, instDir)
return nil, fmt.Errorf("instance %q already exists (%q)", instName, instDir)
}

if clicontext.Bool("tty") {
Expand Down Expand Up @@ -126,9 +126,9 @@ func loadOrCreateInstance(clicontext *cli.Context) (*store.Instance, error) {
}
rejectedYAML := "lima.REJECTED.yaml"
if writeErr := os.WriteFile(rejectedYAML, yBytes, 0644); writeErr != nil {
return nil, errors.Wrapf(err, "the YAML is invalid, attempted to save the buffer as %q but failed: %v", rejectedYAML, writeErr)
return nil, fmt.Errorf("the YAML is invalid, attempted to save the buffer as %q but failed: %v: %w", rejectedYAML, writeErr, err)
}
return nil, errors.Wrapf(err, "the YAML is invalid, saved the buffer as %q", rejectedYAML)
return nil, fmt.Errorf("the YAML is invalid, saved the buffer as %q: %w", rejectedYAML, err)
}
if err := os.MkdirAll(instDir, 0700); err != nil {
return nil, err
Expand Down Expand Up @@ -161,7 +161,7 @@ func askWhetherToOpenEditor(name string) (bool, error) {
os.Exit(0)
return false, errors.New("should not reach here")
default:
return false, errors.Errorf("unexpected answer %q", ans)
return false, fmt.Errorf("unexpected answer %q", ans)
}
}

Expand Down Expand Up @@ -198,7 +198,7 @@ func openEditor(clicontext *cli.Context, name string, initialContent []byte) ([]
editorCmd.Stderr = os.Stderr
logrus.Debugf("opening editor %q for a file %q", editor, tmpYAMLPath)
if err := editorCmd.Run(); err != nil {
return nil, errors.Wrapf(err, "could not execute editor %q for a file %q", editor, tmpYAMLPath)
return nil, fmt.Errorf("could not execute editor %q for a file %q: %w", editor, tmpYAMLPath, err)
}
b, err := os.ReadFile(tmpYAMLPath)
if err != nil {
Expand Down Expand Up @@ -245,7 +245,7 @@ func instNameFromYAMLPath(yamlPath string) (string, error) {
s = strings.TrimSuffix(strings.TrimSuffix(s, ".yml"), ".yaml")
s = strings.ReplaceAll(s, ".", "-")
if err := identifiers.Validate(s); err != nil {
return "", errors.Wrapf(err, "filename %q is invalid", yamlPath)
return "", fmt.Errorf("filename %q is invalid: %w", yamlPath, err)
}
return s, nil
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/limactl/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
Expand All @@ -11,7 +13,6 @@ import (
hostagentapi "github.com/lima-vm/lima/pkg/hostagent/api"
"github.com/lima-vm/lima/pkg/store"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
Expand All @@ -33,7 +34,7 @@ var stopCommand = &cli.Command{

func stopAction(clicontext *cli.Context) error {
if clicontext.NArg() > 1 {
return errors.Errorf("too many arguments")
return fmt.Errorf("too many arguments")
}

instName := clicontext.Args().First()
Expand All @@ -56,7 +57,7 @@ func stopAction(clicontext *cli.Context) error {

func stopInstanceGracefully(inst *store.Instance) error {
if inst.Status != store.StatusRunning {
return errors.Errorf("expected status %q, got %q", store.StatusRunning, inst.Status)
return fmt.Errorf("expected status %q, got %q", store.StatusRunning, inst.Status)
}

begin := time.Now() // used for logrus propagation
Expand Down
8 changes: 5 additions & 3 deletions cmd/limactl/validate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"fmt"

"github.com/lima-vm/lima/pkg/store"
"github.com/pkg/errors"

"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
Expand All @@ -16,13 +18,13 @@ var validateCommand = &cli.Command{

func validateAction(clicontext *cli.Context) error {
if clicontext.NArg() == 0 {
return errors.Errorf("requires at least 1 argument")
return fmt.Errorf("requires at least 1 argument")
}

for _, f := range clicontext.Args().Slice() {
_, err := store.LoadYAMLByFilePath(f)
if err != nil {
return errors.Wrapf(err, "failed to load YAML file %q", f)
return fmt.Errorf("failed to load YAML file %q: %w", f, err)
}
if _, err := instNameFromYAMLPath(f); err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ require (
github.com/norouter/norouter v0.6.4
github.com/nxadm/tail v1.4.8
github.com/opencontainers/go-digest v1.0.0
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/urfave/cli/v2 v2.3.0
github.com/yalue/native_endian v1.0.1
Expand Down
10 changes: 5 additions & 5 deletions pkg/cidata/cidata.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cidata

import (
"errors"
"fmt"
"io"
"io/fs"
Expand All @@ -19,7 +20,6 @@ import (
"github.com/lima-vm/lima/pkg/sshutil"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -96,7 +96,7 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML) error {
Reader: strings.NewReader(f.Script),
})
default:
return errors.Errorf("unknown provision mode %q", f.Mode)
return fmt.Errorf("unknown provision mode %q", f.Mode)
}
}

Expand All @@ -118,7 +118,7 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML) error {
case limayaml.AARCH64:
nftgzBase = fmt.Sprintf("nerdctl-full-%s-linux-arm64.tar.gz", NerdctlVersion)
default:
return errors.Errorf("unexpected arch %q", y.Arch)
return fmt.Errorf("unexpected arch %q", y.Arch)
}
td, err := ioutil.TempDir("", "lima-download-nerdctl")
if err != nil {
Expand All @@ -132,7 +132,7 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML) error {
logrus.Infof("Downloading %q (%s)", nftgzURL, nftgzDigest)
res, err := downloader.Download(nftgzLocal, nftgzURL, downloader.WithCache(), downloader.WithExpectedDigest(nftgzDigest))
if err != nil {
return errors.Wrapf(err, "failed to download %q", nftgzURL)
return fmt.Errorf("failed to download %q: %w", nftgzURL, err)
}
logrus.Debugf("res.ValidatedDigest=%v", res.ValidatedDigest)
switch res.Status {
Expand Down Expand Up @@ -200,6 +200,6 @@ func GuestAgentBinary(arch string) (io.ReadCloser, error) {
}
}

return nil, errors.Errorf("failed to find \"lima-guestagent.Linux-%s\" binary for %q, attempted %v",
return nil, fmt.Errorf("failed to find \"lima-guestagent.Linux-%s\" binary for %q, attempted %v",
arch, self, candidates)
}
Loading

0 comments on commit 8092199

Please sign in to comment.