diff --git a/cmd/lima-guestagent/daemon_linux.go b/cmd/lima-guestagent/daemon_linux.go index 5769e2169dc..ef1e56121d8 100644 --- a/cmd/lima-guestagent/daemon_linux.go +++ b/cmd/lima-guestagent/daemon_linux.go @@ -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" ) diff --git a/cmd/limactl/copy.go b/cmd/limactl/copy.go index 0a1d421f6a4..07c7595282c 100644 --- a/cmd/limactl/copy.go +++ b/cmd/limactl/copy.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "os" "os/exec" @@ -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" ) @@ -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 { @@ -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...) diff --git a/cmd/limactl/delete.go b/cmd/limactl/delete.go index 19081d201c9..c5a23e906ca 100644 --- a/cmd/limactl/delete.go +++ b/cmd/limactl/delete.go @@ -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" ) @@ -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() { @@ -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) } @@ -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 } diff --git a/cmd/limactl/hostagent.go b/cmd/limactl/hostagent.go index c7aef5b8ad4..2ce1aecc436 100644 --- a/cmd/limactl/hostagent.go +++ b/cmd/limactl/hostagent.go @@ -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" ) @@ -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 @@ -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() diff --git a/cmd/limactl/list.go b/cmd/limactl/list.go index d37c84af3ac..e8a8bd88d70 100644 --- a/cmd/limactl/list.go +++ b/cmd/limactl/list.go @@ -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" ) diff --git a/cmd/limactl/shell.go b/cmd/limactl/shell.go index 69a49c5f52f..e2afc0e0fe6 100644 --- a/cmd/limactl/shell.go +++ b/cmd/limactl/shell.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "os" "os/exec" @@ -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" ) @@ -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() @@ -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 { diff --git a/cmd/limactl/start.go b/cmd/limactl/start.go index 5481d186033..d119dc20df9 100644 --- a/cmd/limactl/start.go +++ b/cmd/limactl/start.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "io/ioutil" "os" @@ -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" ) @@ -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() @@ -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) @@ -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") { @@ -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 @@ -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) } } @@ -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 { @@ -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 } diff --git a/cmd/limactl/stop.go b/cmd/limactl/stop.go index 31f54f89106..983e3ee4064 100644 --- a/cmd/limactl/stop.go +++ b/cmd/limactl/stop.go @@ -2,6 +2,8 @@ package main import ( "context" + "errors" + "fmt" "os" "path/filepath" "strings" @@ -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" ) @@ -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() @@ -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 diff --git a/cmd/limactl/validate.go b/cmd/limactl/validate.go index a2487945c9e..0ba5c8036e6 100644 --- a/cmd/limactl/validate.go +++ b/cmd/limactl/validate.go @@ -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" ) @@ -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 diff --git a/go.mod b/go.mod index 5dd3753b5d2..bdc8f3186e2 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/pkg/cidata/cidata.go b/pkg/cidata/cidata.go index 9b1dd3228fd..788bb0ac73d 100644 --- a/pkg/cidata/cidata.go +++ b/pkg/cidata/cidata.go @@ -1,6 +1,7 @@ package cidata import ( + "errors" "fmt" "io" "io/fs" @@ -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" ) @@ -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) } } @@ -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 { @@ -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 { @@ -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) } diff --git a/pkg/cidata/template.go b/pkg/cidata/template.go index 745ef52a6ce..f0484395672 100644 --- a/pkg/cidata/template.go +++ b/pkg/cidata/template.go @@ -3,7 +3,8 @@ package cidata import ( "bytes" "embed" - _ "embed" + "errors" + "fmt" "io/fs" "path/filepath" @@ -11,7 +12,6 @@ import ( "github.com/containerd/containerd/identifiers" "github.com/lima-vm/lima/pkg/templateutil" - "github.com/pkg/errors" ) //go:embed cidata.TEMPLATE.d @@ -56,7 +56,7 @@ func ValidateTemplateArgs(args TemplateArgs) error { } for i, f := range args.Mounts { if !filepath.IsAbs(f) { - return errors.Errorf("field mounts[%d] must be absolute, got %q", i, f) + return fmt.Errorf("field mounts[%d] must be absolute, got %q", i, f) } } return nil @@ -81,7 +81,7 @@ func ExecuteTemplate(args TemplateArgs) ([]iso9660util.Entry, error) { return nil } if !d.Type().IsRegular() { - return errors.Errorf("got non-regular file %q", path) + return fmt.Errorf("got non-regular file %q", path) } templateB, err := fs.ReadFile(fsys, path) if err != nil { diff --git a/pkg/downloader/downloader.go b/pkg/downloader/downloader.go index 65bcf502495..98f90bd9693 100644 --- a/pkg/downloader/downloader.go +++ b/pkg/downloader/downloader.go @@ -2,6 +2,7 @@ package downloader import ( "crypto/sha256" + "errors" "fmt" "io" "io/ioutil" @@ -16,7 +17,6 @@ import ( "github.com/lima-vm/lima/pkg/localpathutil" "github.com/mattn/go-isatty" "github.com/opencontainers/go-digest" - "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -76,7 +76,7 @@ func WithExpectedDigest(expectedDigest digest.Digest) Opt { return func(o *options) error { if expectedDigest != "" { if !expectedDigest.Algorithm().Available() { - return errors.Errorf("expected digest algorithm %q is not available", expectedDigest.Algorithm()) + return fmt.Errorf("expected digest algorithm %q is not available", expectedDigest.Algorithm()) } if err := expectedDigest.Validate(); err != nil { return err @@ -143,7 +143,7 @@ func Download(local, remote string, opts ...Opt) (*Result, error) { if o.expectedDigest != "" { algo := o.expectedDigest.Algorithm().String() if strings.Contains(algo, "/") || strings.Contains(algo, "\\") { - return nil, errors.Errorf("invalid digest algorithm %q", algo) + return nil, fmt.Errorf("invalid digest algorithm %q", algo) } shadDigest = filepath.Join(shad, algo+".digest") } @@ -154,7 +154,7 @@ func Download(local, remote string, opts ...Opt) (*Result, error) { o.expectedDigest, shadDigest, shadData) shadDigestS := strings.TrimSpace(string(shadDigestB)) if o.expectedDigest.String() != shadDigestS { - return nil, errors.Errorf("expected digest %q does not match the cached digest %q", o.expectedDigest.String(), shadDigestS) + return nil, fmt.Errorf("expected digest %q does not match the cached digest %q", o.expectedDigest.String(), shadDigestS) } if err := copyLocal(localPath, shadData, ""); err != nil { return nil, err @@ -207,12 +207,12 @@ func isLocal(s string) bool { func localPath(s string) (string, error) { if !isLocal(s) { - return "", errors.Errorf("got non-local path: %q", s) + return "", fmt.Errorf("got non-local path: %q", s) } if strings.HasPrefix(s, "file://") { res := strings.TrimPrefix(s, "file://") if !filepath.IsAbs(res) { - return "", errors.Errorf("got non-absolute path %q", res) + return "", fmt.Errorf("got non-absolute path %q", res) } return res, nil } @@ -241,7 +241,7 @@ func validateLocalFileDigest(localPath string, expectedDigest digest.Digest) err logrus.Debugf("verifying digest of local file %q (%s)", localPath, expectedDigest) algo := expectedDigest.Algorithm() if !algo.Available() { - return errors.Errorf("expected digest algorithm %q is not available", algo) + return fmt.Errorf("expected digest algorithm %q is not available", algo) } r, err := os.Open(localPath) if err != nil { @@ -253,7 +253,7 @@ func validateLocalFileDigest(localPath string, expectedDigest digest.Digest) err return err } if actualDigest != expectedDigest { - return errors.Errorf("expected digest %q, got %q", expectedDigest, actualDigest) + return fmt.Errorf("expected digest %q, got %q", expectedDigest, actualDigest) } return nil } @@ -297,7 +297,7 @@ func downloadHTTP(localPath, url string, expectedDigest digest.Digest) error { } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - return errors.Errorf("expected HTTP status %d, got %s", http.StatusOK, resp.Status) + return fmt.Errorf("expected HTTP status %d, got %s", http.StatusOK, resp.Status) } bar, err := createBar(resp.ContentLength) if err != nil { @@ -309,7 +309,7 @@ func downloadHTTP(localPath, url string, expectedDigest digest.Digest) error { if expectedDigest != "" { algo := expectedDigest.Algorithm() if !algo.Available() { - return errors.Errorf("unsupported digest algorithm %q", algo) + return fmt.Errorf("unsupported digest algorithm %q", algo) } digester = algo.Digester() hasher := digester.Hash() @@ -326,7 +326,7 @@ func downloadHTTP(localPath, url string, expectedDigest digest.Digest) error { if digester != nil { actualDigest := digester.Digest() if actualDigest != expectedDigest { - return errors.Errorf("expected digest %q, got %q", expectedDigest, actualDigest) + return fmt.Errorf("expected digest %q, got %q", expectedDigest, actualDigest) } } diff --git a/pkg/guestagent/procnettcp/procnettcp.go b/pkg/guestagent/procnettcp/procnettcp.go index 3403283b62f..b9ae0648d1f 100644 --- a/pkg/guestagent/procnettcp/procnettcp.go +++ b/pkg/guestagent/procnettcp/procnettcp.go @@ -3,12 +3,11 @@ package procnettcp import ( "bufio" "encoding/hex" + "fmt" "io" "net" "strconv" "strings" - - "github.com/pkg/errors" ) type Kind = string @@ -37,7 +36,7 @@ func Parse(r io.Reader, kind Kind) ([]Entry, error) { switch kind { case TCP, TCP6: default: - return nil, errors.Errorf("unexpected kind %q", kind) + return nil, fmt.Errorf("unexpected kind %q", kind) } var entries []Entry @@ -57,10 +56,10 @@ func Parse(r io.Reader, kind Kind) ([]Entry, error) { fieldNames[fields[j]] = j } if _, ok := fieldNames["local_address"]; !ok { - return nil, errors.Errorf("field \"local_address\" not found") + return nil, fmt.Errorf("field \"local_address\" not found") } if _, ok := fieldNames["st"]; !ok { - return nil, errors.Errorf("field \"st\" not found") + return nil, fmt.Errorf("field \"st\" not found") } default: @@ -106,12 +105,12 @@ func Parse(r io.Reader, kind Kind) ([]Entry, error) { func ParseAddress(s string) (net.IP, uint16, error) { split := strings.SplitN(s, ":", 2) if len(split) != 2 { - return nil, 0, errors.Errorf("unparsable address %q", s) + return nil, 0, fmt.Errorf("unparsable address %q", s) } switch l := len(split[0]); l { case 8, 32: default: - return nil, 0, errors.Errorf("unparsable address %q, expected length of %q to be 8 or 32, got %d", + return nil, 0, fmt.Errorf("unparsable address %q, expected length of %q to be 8 or 32, got %d", s, split[0], l) } @@ -120,7 +119,7 @@ func ParseAddress(s string) (net.IP, uint16, error) { quartet := split[0][8*i : 8*(i+1)] quartetLE, err := hex.DecodeString(quartet) // surprisingly little endian, per 4 bytes if err != nil { - return nil, 0, errors.Wrapf(err, "unparsable address %q: unparsable quartet %q", s, quartet) + return nil, 0, fmt.Errorf("unparsable address %q: unparsable quartet %q: %w", s, quartet, err) } for j := 0; j < len(quartetLE); j++ { ipBytes[4*i+len(quartetLE)-1-j] = quartetLE[j] @@ -130,7 +129,7 @@ func ParseAddress(s string) (net.IP, uint16, error) { port64, err := strconv.ParseUint(split[1], 16, 16) if err != nil { - return nil, 0, errors.Errorf("unparsable address %q: unparsable port %q", s, split[1]) + return nil, 0, fmt.Errorf("unparsable address %q: unparsable port %q", s, split[1]) } port := uint16(port64) diff --git a/pkg/guestagent/procnettcp/procnettcp_linux.go b/pkg/guestagent/procnettcp/procnettcp_linux.go index 2c6b8c91096..f2085195744 100644 --- a/pkg/guestagent/procnettcp/procnettcp_linux.go +++ b/pkg/guestagent/procnettcp/procnettcp_linux.go @@ -1,9 +1,8 @@ package procnettcp import ( + "errors" "os" - - "github.com/pkg/errors" ) // ParseFiles parses /proc/net/{tcp, tcp6} diff --git a/pkg/hostagent/hostagent.go b/pkg/hostagent/hostagent.go index 8229db86604..4c4c89b32dd 100644 --- a/pkg/hostagent/hostagent.go +++ b/pkg/hostagent/hostagent.go @@ -14,7 +14,6 @@ import ( "sync" "time" - "github.com/lima-vm/sshocker/pkg/ssh" "github.com/digitalocean/go-qemu/qmp" "github.com/digitalocean/go-qemu/qmp/raw" "github.com/hashicorp/go-multierror" @@ -26,7 +25,8 @@ import ( "github.com/lima-vm/lima/pkg/sshutil" "github.com/lima-vm/lima/pkg/store" "github.com/lima-vm/lima/pkg/store/filenames" - "github.com/pkg/errors" + "github.com/lima-vm/sshocker/pkg/ssh" + "github.com/sirupsen/logrus" ) @@ -167,7 +167,7 @@ func (a *HostAgent) Run(ctx context.Context) error { sshLocalPort := a.y.SSH.LocalPort // TODO: support dynamic port if sshLocalPort < 0 { - return errors.Errorf("invalid ssh local port %d", sshLocalPort) + return fmt.Errorf("invalid ssh local port %d", sshLocalPort) } stBase := hostagentapi.Status{ SSHLocalPort: sshLocalPort, @@ -375,7 +375,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, ) cmd := exec.CommandContext(ctx, sshConfig.Binary(), args...) if out, err := cmd.Output(); err != nil { - return errors.Wrapf(err, "failed to run %v: %q", cmd.Args, string(out)) + return fmt.Errorf("failed to run %v: %q: %w", cmd.Args, string(out), err) } return nil } diff --git a/pkg/hostagent/mount.go b/pkg/hostagent/mount.go index d4cd9155334..40222012a3c 100644 --- a/pkg/hostagent/mount.go +++ b/pkg/hostagent/mount.go @@ -2,13 +2,13 @@ package hostagent import ( "context" + "fmt" "os" - "github.com/lima-vm/sshocker/pkg/reversesshfs" "github.com/hashicorp/go-multierror" "github.com/lima-vm/lima/pkg/limayaml" "github.com/lima-vm/lima/pkg/localpathutil" - "github.com/pkg/errors" + "github.com/lima-vm/sshocker/pkg/reversesshfs" ) type mount struct { @@ -51,14 +51,14 @@ func (a *HostAgent) setupMount(ctx context.Context, m limayaml.Mount) (*mount, e SSHFSAdditionalArgs: []string{"-o", "allow_root"}, } if err := rsf.Prepare(); err != nil { - return nil, errors.Wrapf(err, "failed to prepare reverse sshfs for %q", expanded) + return nil, fmt.Errorf("failed to prepare reverse sshfs for %q: %w", expanded, err) } if err := rsf.Start(); err != nil { a.l.WithError(err).Warnf("failed to mount reverse sshfs for %q, retrying with `-o nonempty`", expanded) // NOTE: nonempty is not supported for libfuse3: https://github.com/canonical/multipass/issues/1381 rsf.SSHFSAdditionalArgs = []string{"-o", "nonempty"} if err := rsf.Start(); err != nil { - return nil, errors.Wrapf(err, "failed to mount reverse sshfs for %q", expanded) + return nil, fmt.Errorf("failed to mount reverse sshfs for %q: %w", expanded, err) } } @@ -66,7 +66,7 @@ func (a *HostAgent) setupMount(ctx context.Context, m limayaml.Mount) (*mount, e close: func() error { a.l.Infof("Unmounting %q", expanded) if closeErr := rsf.Close(); closeErr != nil { - return errors.Wrapf(err, "failed to unmount reverse sshfs for %q", expanded) + return fmt.Errorf("failed to unmount reverse sshfs for %q: %w", expanded, err) } return nil }, diff --git a/pkg/hostagent/port.go b/pkg/hostagent/port.go index 0b2070f9b22..14a329f147f 100644 --- a/pkg/hostagent/port.go +++ b/pkg/hostagent/port.go @@ -4,9 +4,9 @@ import ( "context" "net" - "github.com/lima-vm/sshocker/pkg/ssh" "github.com/lima-vm/lima/pkg/guestagent/api" "github.com/lima-vm/lima/pkg/limayaml" + "github.com/lima-vm/sshocker/pkg/ssh" "github.com/sirupsen/logrus" ) diff --git a/pkg/hostagent/requirements.go b/pkg/hostagent/requirements.go index 6419bcdd6f1..f866d015765 100644 --- a/pkg/hostagent/requirements.go +++ b/pkg/hostagent/requirements.go @@ -2,12 +2,12 @@ package hostagent import ( "context" + "fmt" "time" - "github.com/lima-vm/sshocker/pkg/ssh" "github.com/hashicorp/go-multierror" "github.com/lima-vm/lima/pkg/limayaml" - "github.com/pkg/errors" + "github.com/lima-vm/sshocker/pkg/ssh" ) func (a *HostAgent) waitForRequirements(ctx context.Context, label string, requirements []requirement) error { @@ -28,14 +28,10 @@ func (a *HostAgent) waitForRequirements(ctx context.Context, label string, requi } if req.fatal { a.l.Infof("No further %s requirements will be checked", label) - return multierror.Append(mErr, - errors.Wrapf(err, "failed to satisfy the %s requirement %d of %d %q: %s; skipping further checks", - label, i+1, len(requirements), req.description, req.debugHint)) + return multierror.Append(mErr, fmt.Errorf("failed to satisfy the %s requirement %d of %d %q: %s; skipping further checks: %w", label, i+1, len(requirements), req.description, req.debugHint, err)) } if j == retries-1 { - mErr = multierror.Append(mErr, - errors.Wrapf(err, "failed to satisfy the %s requirement %d of %d %q: %s", - label, i+1, len(requirements), req.description, req.debugHint)) + mErr = multierror.Append(mErr, fmt.Errorf("failed to satisfy the %s requirement %d of %d %q: %s: %w", label, i+1, len(requirements), req.description, req.debugHint, err)) break retryLoop } time.Sleep(10 * time.Second) @@ -49,7 +45,7 @@ func (a *HostAgent) waitForRequirement(ctx context.Context, r requirement) error stdout, stderr, err := ssh.ExecuteScript("127.0.0.1", a.y.SSH.LocalPort, a.sshConfig, r.script, r.description) a.l.Debugf("stdout=%q, stderr=%q, err=%v", stdout, stderr, err) if err != nil { - return errors.Wrapf(err, "stdout=%q, stderr=%q", stdout, stderr) + return fmt.Errorf("stdout=%q, stderr=%q: %w", stdout, stderr, err) } return nil } diff --git a/pkg/httpclientutil/httpclientutil.go b/pkg/httpclientutil/httpclientutil.go index b50f903fa57..5066d7fdf76 100644 --- a/pkg/httpclientutil/httpclientutil.go +++ b/pkg/httpclientutil/httpclientutil.go @@ -6,6 +6,7 @@ package httpclientutil import ( "context" "encoding/json" + "errors" "fmt" "io" "io/ioutil" @@ -14,7 +15,6 @@ import ( "os" "github.com/lima-vm/lima/pkg/guestagent/api" - "github.com/pkg/errors" ) // NewHTTPClientWithSocketPath creates a client. @@ -61,7 +61,7 @@ func readAtMost(r io.Reader, maxBytes int) ([]byte, error) { return b, err } if lr.N == 0 { - return b, errors.Errorf("expected at most %d bytes, got more", maxBytes) + return b, fmt.Errorf("expected at most %d bytes, got more", maxBytes) } return b, nil } diff --git a/pkg/limayaml/validate.go b/pkg/limayaml/validate.go index 73308be0572..669760ce8ac 100644 --- a/pkg/limayaml/validate.go +++ b/pkg/limayaml/validate.go @@ -9,10 +9,11 @@ import ( "runtime" "strings" + "errors" + "github.com/docker/go-units" "github.com/lima-vm/lima/pkg/localpathutil" "github.com/lima-vm/lima/pkg/qemu/qemuconst" - "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -20,7 +21,7 @@ func Validate(y LimaYAML) error { switch y.Arch { case X8664, AARCH64: default: - return errors.Errorf("field `arch` must be %q or %q , got %q", X8664, AARCH64, y.Arch) + return fmt.Errorf("field `arch` must be %q or %q , got %q", X8664, AARCH64, y.Arch) } if len(y.Images) == 0 { @@ -29,22 +30,21 @@ func Validate(y LimaYAML) error { for i, f := range y.Images { if !strings.Contains(f.Location, "://") { if _, err := localpathutil.Expand(f.Location); err != nil { - return errors.Wrapf(err, "field `images[%d].location` refers to an invalid local file path: %q", - i, f.Location) + return fmt.Errorf("field `images[%d].location` refers to an invalid local file path: %q: %w", i, f.Location, err) } // f.Location does NOT need to be accessible, so we do NOT check os.Stat(f.Location) } switch f.Arch { case X8664, AARCH64: default: - return errors.Errorf("field `images.arch` must be %q or %q, got %q", X8664, AARCH64, f.Arch) + return fmt.Errorf("field `images.arch` must be %q or %q, got %q", X8664, AARCH64, f.Arch) } if f.Digest != "" { if !f.Digest.Algorithm().Available() { - return errors.Errorf("field `images[%d].digest` refers to an unavailable digest algorithm", i) + return fmt.Errorf("field `images[%d].digest` refers to an unavailable digest algorithm", i) } if err := f.Digest.Validate(); err != nil { - return errors.Wrapf(err, "field `images[%d].digest` is invalid: %s", i, f.Digest.String()) + return fmt.Errorf("field `images[%d].digest` is invalid: %s: %w", i, f.Digest.String(), err) } } } @@ -54,46 +54,43 @@ func Validate(y LimaYAML) error { } if _, err := units.RAMInBytes(y.Memory); err != nil { - return errors.Wrapf(err, "field `memory` has an invalid value") + return fmt.Errorf("field `memory` has an invalid value: %w", err) } if _, err := units.RAMInBytes(y.Disk); err != nil { - return errors.Wrapf(err, "field `memory` has an invalid value") + return fmt.Errorf("field `memory` has an invalid value: %w", err) } u, err := user.Current() if err != nil { - return errors.Wrap(err, "internal error (not an error of YAML)") + return fmt.Errorf("internal error (not an error of YAML): %w", err) } // reservedHome is the home directory defined in "cidata.iso:/user-data" reservedHome := fmt.Sprintf("/home/%s.linux", u.Username) for i, f := range y.Mounts { if !filepath.IsAbs(f.Location) && !strings.HasPrefix(f.Location, "~") { - return errors.Errorf("field `mounts[%d].location` must be an absolute path, got %q", + return fmt.Errorf("field `mounts[%d].location` must be an absolute path, got %q", i, f.Location) } loc, err := localpathutil.Expand(f.Location) if err != nil { - return errors.Wrapf(err, "field `mounts[%d].location` refers to an unexpandable path: %q", - i, f.Location) + return fmt.Errorf("field `mounts[%d].location` refers to an unexpandable path: %q: %w", i, f.Location, err) } switch loc { case "/", "/bin", "/dev", "/etc", "/home", "/opt", "/sbin", "/tmp", "/usr", "/var": - return errors.Errorf("field `mounts[%d].location` must not be a system path such as /etc or /usr", i) + return fmt.Errorf("field `mounts[%d].location` must not be a system path such as /etc or /usr", i) case reservedHome: - return errors.Errorf("field `mounts[%d].location` is internally reserved", i) + return fmt.Errorf("field `mounts[%d].location` is internally reserved", i) } st, err := os.Stat(loc) if err != nil { if !errors.Is(err, os.ErrNotExist) { - return errors.Wrapf(err, "field `mounts[%d].location` refers to an inaccessible path: %q", - i, f.Location) + return fmt.Errorf("field `mounts[%d].location` refers to an inaccessible path: %q: %w", i, f.Location, err) } } else if !st.IsDir() { - return errors.Wrapf(err, "field `mounts[%d].location` refers to a non-directory path: %q", - i, f.Location) + return fmt.Errorf("field `mounts[%d].location` refers to a non-directory path: %q: %w", i, f.Location, err) } } @@ -107,7 +104,7 @@ func Validate(y LimaYAML) error { switch p.Mode { case ProvisionModeSystem, ProvisionModeUser: default: - return errors.Errorf("field `provision[%d].mode` must be either %q or %q", + return fmt.Errorf("field `provision[%d].mode` must be either %q or %q", i, ProvisionModeSystem, ProvisionModeUser) } } @@ -115,7 +112,7 @@ func Validate(y LimaYAML) error { switch p.Mode { case ProbeModeReadiness: default: - return errors.Errorf("field `probe[%d].mode` can only be %q", + return fmt.Errorf("field `probe[%d].mode` can only be %q", i, ProbeModeReadiness) } } @@ -123,7 +120,7 @@ func Validate(y LimaYAML) error { field := fmt.Sprintf("portForwards[%d]", i) if rule.GuestPort != 0 { if rule.GuestPort != rule.GuestPortRange[0] { - return errors.Errorf("field `%s.guestPort` must match field `%s.guestPortRange[0]`", field, field) + return fmt.Errorf("field `%s.guestPort` must match field `%s.guestPortRange[0]`", field, field) } // redundant validation to make sure the error contains the correct field name if err := validatePort(field+".guestPort", rule.GuestPort); err != nil { @@ -132,7 +129,7 @@ func Validate(y LimaYAML) error { } if rule.HostPort != 0 { if rule.HostPort != rule.HostPortRange[0] { - return errors.Errorf("field `%s.hostPort` must match field `%s.hostPortRange[0]`", field, field) + return fmt.Errorf("field `%s.hostPort` must match field `%s.hostPortRange[0]`", field, field) } // redundant validation to make sure the error contains the correct field name if err := validatePort(field+".hostPort", rule.HostPort); err != nil { @@ -148,16 +145,16 @@ func Validate(y LimaYAML) error { } } if rule.GuestPortRange[0] > rule.GuestPortRange[1] { - return errors.Errorf("field `%s.guestPortRange[1]` must be greater than or equal to field `%s.guestPortRange[0]`", field, field) + return fmt.Errorf("field `%s.guestPortRange[1]` must be greater than or equal to field `%s.guestPortRange[0]`", field, field) } if rule.HostPortRange[0] > rule.HostPortRange[1] { - return errors.Errorf("field `%s.hostPortRange[1]` must be greater than or equal to field `%s.hostPortRange[0]`", field, field) + return fmt.Errorf("field `%s.hostPortRange[1]` must be greater than or equal to field `%s.hostPortRange[0]`", field, field) } if rule.GuestPortRange[1]-rule.GuestPortRange[0] != rule.HostPortRange[1]-rule.HostPortRange[0] { - return errors.Errorf("field `%s.hostPortRange` must specify the same number of ports as field `%s.guestPortRange`", field, field) + return fmt.Errorf("field `%s.hostPortRange` must specify the same number of ports as field `%s.guestPortRange`", field, field) } if rule.Proto != TCP { - return errors.Errorf("field `%s.proto` must be %q", field, TCP) + return fmt.Errorf("field `%s.proto` must be %q", field, TCP) } // Not validating that the various GuestPortRanges and HostPortRanges are not overlapping. Rules will be // processed sequentially and the first matching rule for a guest port determines forwarding behavior. @@ -175,7 +172,7 @@ func validateNetwork(yNetwork Network) error { for i, vde := range yNetwork.VDE { field := fmt.Sprintf("network.vde[%d]", i) if vde.VNL == "" { - return errors.Errorf("field `%s.vnl` must not be empty", field) + return fmt.Errorf("field `%s.vnl` must not be empty", field) } // The field is called VDE.VNL in anticipation of QEMU upgrading VDE2 to VDEplug4, // but right now the only valid value on macOS is a path to the vde_switch socket directory, @@ -184,28 +181,28 @@ func validateNetwork(yNetwork Network) error { vdeSwitch := strings.TrimPrefix(vde.VNL, "vde://") fi, err := os.Stat(vdeSwitch) if err != nil { - return errors.Wrapf(err, "field `%s.vnl` %q failed stat", field, vdeSwitch) + return fmt.Errorf("field `%s.vnl` %q failed stat: %w", field, vdeSwitch, err) } if fi.IsDir() { /* Switch mode (vdeSwitch is dir, port != 65535) */ ctlSocket := filepath.Join(vdeSwitch, "ctl") fi, err = os.Stat(ctlSocket) if err != nil { - return errors.Wrapf(err, "field `%s.vnl` control socket %q failed stat", field, ctlSocket) + return fmt.Errorf("field `%s.vnl` control socket %q failed stat: %w", field, ctlSocket, err) } if fi.Mode()&os.ModeSocket == 0 { - return errors.Errorf("field `%s.vnl` file %q is not a UNIX socket", field, ctlSocket) + return fmt.Errorf("field `%s.vnl` file %q is not a UNIX socket", field, ctlSocket) } if vde.SwitchPort == 65535 { - return errors.Errorf("field `%s.vnl` points to a non-PTP switch, so the port number must not be 65535", field) + return fmt.Errorf("field `%s.vnl` points to a non-PTP switch, so the port number must not be 65535", field) } } else { /* PTP mode (vdeSwitch is socket, port == 65535) */ if fi.Mode()&os.ModeSocket == 0 { - return errors.Errorf("field `%s.vnl` %q is not a directory nor a UNIX socket", field, vdeSwitch) + return fmt.Errorf("field `%s.vnl` %q is not a directory nor a UNIX socket", field, vdeSwitch) } if vde.SwitchPort != 65535 { - return errors.Errorf("field `%s.vnl` points to a PTP (switchless) socket %q, so the port number has to be 65535 (got %d)", + return fmt.Errorf("field `%s.vnl` points to a PTP (switchless) socket %q, so the port number has to be 65535 (got %d)", field, vdeSwitch, vde.SwitchPort) } } @@ -216,24 +213,24 @@ func validateNetwork(yNetwork Network) error { if vde.MACAddress != "" { hw, err := net.ParseMAC(vde.MACAddress) if err != nil { - return errors.Wrap(err, "field `vmnet.mac` invalid") + return fmt.Errorf("field `vmnet.mac` invalid: %w", err) } if len(hw) != 6 { - return errors.Errorf("field `%s.macAddress` must be a 48 bit (6 bytes) MAC address; actual length of %q is %d bytes", field, vde.MACAddress, len(hw)) + return fmt.Errorf("field `%s.macAddress` must be a 48 bit (6 bytes) MAC address; actual length of %q is %d bytes", field, vde.MACAddress, len(hw)) } } // FillDefault() will make sure that vde.Name is not the empty string if len(vde.Name) >= 16 { - return errors.Errorf("field `%s.name` must be less than 16 bytes, but is %d bytes: %q", field, len(vde.Name), vde.Name) + return fmt.Errorf("field `%s.name` must be less than 16 bytes, but is %d bytes: %q", field, len(vde.Name), vde.Name) } if strings.ContainsAny(vde.Name, " \t\n/") { - return errors.Errorf("field `%s.name` must not contain whitespace or slashes", field) + return fmt.Errorf("field `%s.name` must not contain whitespace or slashes", field) } if vde.Name == qemuconst.SlirpNICName { - return errors.Errorf("field `%s.name` must not be set to %q because it is reserved for slirp", field, qemuconst.SlirpNICName) + return fmt.Errorf("field `%s.name` must not be set to %q because it is reserved for slirp", field, qemuconst.SlirpNICName) } if prev, ok := networkName[vde.Name]; ok { - return errors.Errorf("field `%s.name` value %q has already been used by field `network.vde[%d].name`", field, vde.Name, prev) + return fmt.Errorf("field `%s.name` value %q has already been used by field `network.vde[%d].name`", field, vde.Name, prev) } networkName[vde.Name] = i } @@ -243,13 +240,13 @@ func validateNetwork(yNetwork Network) error { func validatePort(field string, port int) error { switch { case port < 0: - return errors.Errorf("field `%s` must be > 0", field) + return fmt.Errorf("field `%s` must be > 0", field) case port == 0: - return errors.Errorf("field `%s` must be set", field) + return fmt.Errorf("field `%s` must be set", field) case port == 22: - return errors.Errorf("field `%s` must not be 22", field) + return fmt.Errorf("field `%s` must not be 22", field) case port > 65535: - return errors.Errorf("field `%s` must be < 65536", field) + return fmt.Errorf("field `%s` must be < 65536", field) } return nil } diff --git a/pkg/localpathutil/localpathutil.go b/pkg/localpathutil/localpathutil.go index afc096eec98..ba5124dceb7 100644 --- a/pkg/localpathutil/localpathutil.go +++ b/pkg/localpathutil/localpathutil.go @@ -1,11 +1,11 @@ package localpathutil import ( + "errors" + "fmt" "os" "path/filepath" "strings" - - "github.com/pkg/errors" ) // Expand expands a path like "~", "~/", "~/foo". @@ -27,7 +27,7 @@ func Expand(orig string) (string, error) { s = strings.Replace(s, "~", homeDir, 1) } else { // Paths like "~foo/bar" are unsupported. - return "", errors.Errorf("unexpandable path %q", orig) + return "", fmt.Errorf("unexpandable path %q", orig) } } return filepath.Abs(s) diff --git a/pkg/lockutil/lockutil.go b/pkg/lockutil/lockutil.go index a6fe7bef609..cafe58eca7f 100644 --- a/pkg/lockutil/lockutil.go +++ b/pkg/lockutil/lockutil.go @@ -18,9 +18,9 @@ package lockutil import ( + "fmt" "os" - "github.com/pkg/errors" "github.com/sirupsen/logrus" "golang.org/x/sys/unix" ) @@ -32,7 +32,7 @@ func WithDirLock(dir string, fn func() error) error { } defer dirFile.Close() if err := Flock(dirFile, unix.LOCK_EX); err != nil { - return errors.Wrapf(err, "failed to lock %q", dir) + return fmt.Errorf("failed to lock %q: %w", dir, err) } defer func() { if err := Flock(dirFile, unix.LOCK_UN); err != nil { diff --git a/pkg/qemu/qemu.go b/pkg/qemu/qemu.go index 30e2c2dd1d3..a3899f88734 100644 --- a/pkg/qemu/qemu.go +++ b/pkg/qemu/qemu.go @@ -1,6 +1,7 @@ package qemu import ( + "errors" "fmt" "os" "os/exec" @@ -16,7 +17,6 @@ import ( "github.com/lima-vm/lima/pkg/qemu/qemuconst" "github.com/lima-vm/lima/pkg/store/filenames" "github.com/mattn/go-shellwords" - "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -48,7 +48,7 @@ func EnsureDisk(cfg Config) error { downloader.WithExpectedDigest(f.Digest), ) if err != nil { - errs[i] = errors.Wrapf(err, "failed to download %q", f.Location) + errs[i] = fmt.Errorf("failed to download %q: %w", f.Location, err) continue } logrus.Debugf("res.ValidatedDigest=%v", res.ValidatedDigest) @@ -64,7 +64,7 @@ func EnsureDisk(cfg Config) error { break } if !ensuredBaseDisk { - return errors.Errorf("failed to download the image, attempted %d candidates, errors=%v", + return fmt.Errorf("failed to download the image, attempted %d candidates, errors=%v", len(cfg.LimaYAML.Images), errs) } } @@ -83,14 +83,14 @@ func EnsureDisk(cfg Config) error { args = append(args, diffDisk, strconv.Itoa(int(diskSize))) cmd := exec.Command("qemu-img", args...) if out, err := cmd.CombinedOutput(); err != nil { - return errors.Wrapf(err, "failed to run %v: %q", cmd.Args, string(out)) + return fmt.Errorf("failed to run %v: %q: %w", cmd.Args, string(out), err) } return nil } func argValue(args []string, key string) (string, bool) { if !strings.HasPrefix(key, "-") { - panic(errors.Errorf("got unexpected key %q", key)) + panic(fmt.Errorf("got unexpected key %q", key)) } for i, s := range args { if s == key { @@ -111,11 +111,11 @@ func argValue(args []string, key string) (string, bool) { // appendArgsIfNoConflict cannot be used for: -drive, -cdrom, ... func appendArgsIfNoConflict(args []string, k, v string) []string { if !strings.HasPrefix(k, "-") { - panic(errors.Errorf("got unexpected key %q", k)) + panic(fmt.Errorf("got unexpected key %q", k)) } switch k { case "-drive", "-cdrom", "-chardev", "-blockdev", "-netdev", "-device": - panic(errors.Errorf("appendArgsIfNoConflict() must not be called with k=%q", k)) + panic(fmt.Errorf("appendArgsIfNoConflict() must not be called with k=%q", k)) } if v == "" { @@ -267,7 +267,7 @@ func getExe(arch limayaml.Arch) (string, []string, error) { if envV := os.Getenv(envK); envV != "" { ss, err := shellwords.Parse(envV) if err != nil { - return "", nil, errors.Wrapf(err, "failed to parse %s value %q", envK, envV) + return "", nil, fmt.Errorf("failed to parse %s value %q: %w", envK, envV, err) } exeBase, args = ss[0], ss[1:] if len(args) != 0 { @@ -326,7 +326,7 @@ func getFirmware(qemuExe string, arch limayaml.Arch) (string, error) { } if arch == limayaml.X8664 { - return "", errors.Errorf("could not find firmware for %q (hint: try setting `firmware.legacyBIOS` to `true`)", qemuExe) + return "", fmt.Errorf("could not find firmware for %q (hint: try setting `firmware.legacyBIOS` to `true`)", qemuExe) } - return "", errors.Errorf("could not find firmware for %q", qemuExe) + return "", fmt.Errorf("could not find firmware for %q", qemuExe) } diff --git a/pkg/sshutil/sshutil.go b/pkg/sshutil/sshutil.go index 82a670a0a2a..4231b21dcaa 100644 --- a/pkg/sshutil/sshutil.go +++ b/pkg/sshutil/sshutil.go @@ -1,6 +1,8 @@ package sshutil import ( + "errors" + "fmt" "os" "os/exec" "os/user" @@ -11,7 +13,6 @@ import ( "github.com/lima-vm/lima/pkg/osutil" "github.com/lima-vm/lima/pkg/store" "github.com/lima-vm/lima/pkg/store/filenames" - "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -28,7 +29,7 @@ func readPublicKey(f string) (PubKey, error) { if err == nil { entry.Content = strings.TrimSpace(string(content)) } else { - err = errors.Wrapf(err, "failed to read ssh public key %q", f) + err = fmt.Errorf("failed to read ssh public key %q: %w", f, err) } return entry, err } @@ -50,14 +51,14 @@ func DefaultPubKeys(loadDotSSH bool) ([]PubKey, error) { return nil, err } if err := os.MkdirAll(configDir, 0700); err != nil { - return nil, errors.Wrapf(err, "could not create %q directory", configDir) + return nil, fmt.Errorf("could not create %q directory: %w", configDir, err) } if err := lockutil.WithDirLock(configDir, func() error { keygenCmd := exec.Command("ssh-keygen", "-t", "ed25519", "-q", "-N", "", "-f", filepath.Join(configDir, filenames.UserPrivateKey)) logrus.Debugf("executing %v", keygenCmd.Args) if out, err := keygenCmd.CombinedOutput(); err != nil { - return errors.Wrapf(err, "failed to run %v: %q", keygenCmd.Args, string(out)) + return fmt.Errorf("failed to run %v: %q: %w", keygenCmd.Args, string(out), err) } return nil }); err != nil { @@ -85,7 +86,7 @@ func DefaultPubKeys(loadDotSSH bool) ([]PubKey, error) { } for _, f := range files { if !strings.HasSuffix(f, ".pub") { - panic(errors.Errorf("unexpected ssh public key filename %q", f)) + panic(fmt.Errorf("unexpected ssh public key filename %q", f)) } entry, err := readPublicKey(f) if err == nil { @@ -122,7 +123,7 @@ func CommonArgs(useDotSSH bool) ([]string, error) { } for _, f := range files { if !strings.HasSuffix(f, ".pub") { - panic(errors.Errorf("unexpected ssh public key filename %q", f)) + panic(fmt.Errorf("unexpected ssh public key filename %q", f)) } privateKeyPath := strings.TrimSuffix(f, ".pub") _, err = os.Stat(privateKeyPath) @@ -150,7 +151,7 @@ func CommonArgs(useDotSSH bool) ([]string, error) { func SSHArgs(instDir string, useDotSSH bool) ([]string, error) { controlSock := filepath.Join(instDir, filenames.SSHSock) if len(controlSock) >= osutil.UnixPathMax { - return nil, errors.Errorf("socket path %q is too long: >= UNIX_PATH_MAX=%d", controlSock, osutil.UnixPathMax) + return nil, fmt.Errorf("socket path %q is too long: >= UNIX_PATH_MAX=%d", controlSock, osutil.UnixPathMax) } u, err := user.Current() if err != nil { diff --git a/pkg/start/start.go b/pkg/start/start.go index b39a3b3d8c2..5172d76a20b 100644 --- a/pkg/start/start.go +++ b/pkg/start/start.go @@ -2,6 +2,7 @@ package start import ( "context" + "errors" "fmt" "os" "os/exec" @@ -14,7 +15,6 @@ import ( "github.com/lima-vm/lima/pkg/qemu" "github.com/lima-vm/lima/pkg/store" "github.com/lima-vm/lima/pkg/store/filenames" - "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -37,7 +37,7 @@ func ensureDisk(ctx context.Context, instName, instDir string, y *limayaml.LimaY func Start(ctx context.Context, inst *store.Instance) error { haPIDPath := filepath.Join(inst.Dir, filenames.HostAgentPID) if _, err := os.Stat(haPIDPath); !errors.Is(err, os.ErrNotExist) { - return errors.Errorf("instance %q seems running (hint: remove %q if the instance is not actually running)", inst.Name, haPIDPath) + return fmt.Errorf("instance %q seems running (hint: remove %q if the instance is not actually running)", inst.Name, haPIDPath) } y, err := inst.LoadYAML() @@ -102,7 +102,7 @@ func waitHostAgentStart(ctx context.Context, haPIDPath, haStderrPath string) err return nil } if time.Now().After(deadline) { - return errors.Errorf("hostagent (%q) did not start up in %v (hint: see %q)", haPIDPath, deadlineDuration, haStderrPath) + return fmt.Errorf("hostagent (%q) did not start up in %v (hint: see %q)", haPIDPath, deadlineDuration, haStderrPath) } } } @@ -126,13 +126,13 @@ func watchHostAgentEvents(ctx context.Context, instName, haStdoutPath, haStderrP logrus.Errorf("%+v", ev.Status.Errors) } if ev.Status.Exiting { - err = errors.Errorf("exiting, status=%+v (hint: see %q)", ev.Status, haStderrPath) + err = fmt.Errorf("exiting, status=%+v (hint: see %q)", ev.Status, haStderrPath) return true } else if ev.Status.Running { receivedRunningEvent = true if ev.Status.Degraded { logrus.Warnf("DEGRADED. The VM seems running, but file sharing and port forwarding may not work. (hint: see %q)", haStderrPath) - err = errors.Errorf("degraded, status=%+v", ev.Status) + err = fmt.Errorf("degraded, status=%+v", ev.Status) return true } diff --git a/pkg/store/store.go b/pkg/store/store.go index c7430a0a642..1ab8ba81272 100644 --- a/pkg/store/store.go +++ b/pkg/store/store.go @@ -1,13 +1,13 @@ package store import ( - "github.com/lima-vm/lima/pkg/store/filenames" "os" "path/filepath" "strings" "github.com/containerd/containerd/identifiers" "github.com/lima-vm/lima/pkg/limayaml" + "github.com/lima-vm/lima/pkg/store/filenames" ) // DotLima is a directory that appears under the home directory.