Skip to content

Commit

Permalink
archive,build,cmd: modified the fmt.Errorf()
Browse files Browse the repository at this point in the history
1.replace `fmt.Errorf()` by `errors.Errorf()` or `errors.New()`.
By doing this,when trying error tracing in those folder, we
can see the specific function having errors and errors' line numbers.

2.add the dubug mod in build.

Signed-off-by: Zeyu Li <mr.lizeyu@outlook.com>
  • Loading branch information
zyllee committed Jun 16, 2023
1 parent 136abb3 commit ec96227
Show file tree
Hide file tree
Showing 18 changed files with 95 additions and 82 deletions.
22 changes: 11 additions & 11 deletions archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"archive/tar"
"compress/gzip"
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"time"

"github.com/juju/errors"
"github.com/sirupsen/logrus"

"kraftkit.sh/log"
Expand All @@ -38,12 +38,12 @@ func TarFileWriter(ctx context.Context, src, dst string, tw *tar.Writer, opts ..
dst = filepath.ToSlash(dst)

if dst == "" {
return fmt.Errorf("cannot tar file with no specified destination")
return errors.Errorf("cannot tar file with no specified destination")
} else if dst[0] == '/' {
dst = dst[1:]
}
if strings.HasSuffix(dst, "/") {
return fmt.Errorf("attempting to use TarFileWriter with directory")
return errors.Errorf("attempting to use TarFileWriter with directory")
}

aopts := ArchiveOptions{}
Expand All @@ -55,7 +55,7 @@ func TarFileWriter(ctx context.Context, src, dst string, tw *tar.Writer, opts ..

fi, err := os.Stat(src)
if err != nil {
return fmt.Errorf("fail to stat %s: %v", src, err)
return errors.Errorf("fail to stat %s: %v", src, err)
}

var link string
Expand All @@ -68,7 +68,7 @@ func TarFileWriter(ctx context.Context, src, dst string, tw *tar.Writer, opts ..

header, err := tar.FileInfoHeader(fi, link)
if err != nil {
return fmt.Errorf("%s: %w", src, err)
return errors.Errorf("%s: %v", src, err)
}

header.Name = dst
Expand All @@ -85,7 +85,7 @@ func TarFileWriter(ctx context.Context, src, dst string, tw *tar.Writer, opts ..
}

if err := tw.WriteHeader(header); err != nil {
return fmt.Errorf("tar: %w", err)
return errors.Errorf("tar:%v", err)
}

if mode.IsRegular() {
Expand All @@ -96,14 +96,14 @@ func TarFileWriter(ctx context.Context, src, dst string, tw *tar.Writer, opts ..

fp, err := os.Open(src)
if err != nil {
return fmt.Errorf("fail to open file %s: %v", src, err)
return errors.Errorf("fail to open file %s: %v", src, err)
}

buf := bufPool.Get().(*[]byte)
defer bufPool.Put(buf)

if _, err := io.CopyBuffer(tw, fp, *buf); err != nil {
return fmt.Errorf("failed to copy to %s: %w", src, err)
return errors.Errorf("failed to copy to %s: %v", src, err)
}

if err := fp.Close(); err != nil {
Expand All @@ -126,7 +126,7 @@ func TarFileTo(ctx context.Context, src, dst, out string, opts ...ArchiveOption)

fp, err := os.OpenFile(out, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
return fmt.Errorf("could not create tarball file: %s: %v", out, err)
return errors.Errorf("could not create tarball file: %s: %v", out, err)
}

var tw *tar.Writer
Expand Down Expand Up @@ -164,7 +164,7 @@ func TarFileTo(ctx context.Context, src, dst, out string, opts ...ArchiveOption)
func TarFile(ctx context.Context, src, prefix, out string, opts ...ArchiveOption) error {
fp, err := os.OpenFile(out, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
return fmt.Errorf("could not create tarball file: %s: %v", out, err)
return errors.Errorf("could not create tarball file: %s: %v", out, err)
}

tw := tar.NewWriter(fp)
Expand All @@ -183,7 +183,7 @@ func TarFile(ctx context.Context, src, prefix, out string, opts ...ArchiveOption
func TarDir(ctx context.Context, root, prefix, out string, opts ...ArchiveOption) error {
fp, err := os.OpenFile(out, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
return fmt.Errorf("could not create tarball file: %s: %v", out, err)
return errors.Errorf("could not create tarball file: %s: %v", out, err)
}

tw := tar.NewWriter(fp)
Expand Down
17 changes: 9 additions & 8 deletions archive/unarchive.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ package archive
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/juju/errors"
)

// Unarchive takes an input src file and determines (based on its extension)
Expand All @@ -21,21 +22,21 @@ func Unarchive(src, dst string, opts ...UnarchiveOption) error {
return UntarGz(src, dst, opts...)
}

return fmt.Errorf("unrecognized extension: %s", filepath.Base(src))
return errors.Errorf("unrecognized extension: %s", filepath.Base(src))
}

// UntarGz unarchives a tarball which has been gzip compressed
func UntarGz(src, dst string, opts ...UnarchiveOption) error {
f, err := os.Open(src)
if err != nil {
return fmt.Errorf("could not open file: %v", err)
return errors.Errorf("could not open file: %v", err)
}

defer f.Close()

gzipReader, err := gzip.NewReader(f)
if err != nil {
return fmt.Errorf("could not open gzip reader: %v", err)
return errors.Errorf("could not open gzip reader: %v", err)
}

return Untar(gzipReader, dst, opts...)
Expand Down Expand Up @@ -78,23 +79,23 @@ func Untar(src io.Reader, dst string, opts ...UnarchiveOption) error {
switch header.Typeflag {
case tar.TypeDir:
if err := os.MkdirAll(path, info.Mode()); err != nil {
return fmt.Errorf("could not create directory: %v", err)
return errors.Errorf("could not create directory: %v", err)
}

case tar.TypeReg:
// Create parent path if it does not exist
if err := os.MkdirAll(filepath.Dir(path), info.Mode()); err != nil {
return fmt.Errorf("could not create directory: %v", err)
return errors.Errorf("could not create directory: %v", err)
}

newFile, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode())
if err != nil {
return fmt.Errorf("could not create file: %v", err)
return errors.Errorf("could not create file: %v", err)
}

if _, err := io.Copy(newFile, tr); err != nil {
newFile.Close()
return fmt.Errorf("could not copy file: %v", err)
return errors.Errorf("could not copy file: %v", err)
}

newFile.Close()
Expand Down
23 changes: 12 additions & 11 deletions cmd/kraft/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"

"github.com/MakeNowJust/heredoc"
"github.com/juju/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -91,7 +92,7 @@ func (opts *Build) Run(cmd *cobra.Command, args []string) error {
var workdir string

if (len(opts.Architecture) > 0 || len(opts.Platform) > 0) && len(opts.Target) > 0 {
return fmt.Errorf("the `--arch` and `--plat` options are not supported in addition to `--target`")
return errors.Errorf("the `--arch` and `--plat` options are not supported in addition to `--target`")
}

if len(args) == 0 {
Expand All @@ -116,7 +117,7 @@ func (opts *Build) Run(cmd *cobra.Command, args []string) error {
}

if !app.IsWorkdirInitialized(workdir) {
return fmt.Errorf("cannot build uninitialized project! start with: ukbuild init")
return errors.Errorf("cannot build uninitialized project! start with: ukbuild init")
}

parallel := !config.G[config.KraftKit](ctx).NoParallel
Expand Down Expand Up @@ -161,11 +162,11 @@ func (opts *Build) Run(cmd *cobra.Command, args []string) error {
}

if len(packages) == 0 {
return fmt.Errorf("could not find: %s",
return errors.Errorf("could not find: %s",
unikraft.TypeNameVersion(project.Template()),
)
} else if len(packages) > 1 {
return fmt.Errorf("too many options for %s",
return errors.Errorf("too many options for %s",
unikraft.TypeNameVersion(project.Template()),
)
}
Expand All @@ -188,7 +189,7 @@ func (opts *Build) Run(cmd *cobra.Command, args []string) error {
}

if err := treemodel.Start(); err != nil {
return fmt.Errorf("could not complete search: %v", err)
return errors.Errorf("could not complete search: %v", err)
}

proc := paraprogress.NewProcess(
Expand Down Expand Up @@ -221,7 +222,7 @@ func (opts *Build) Run(cmd *cobra.Command, args []string) error {
}

if err := paramodel.Start(); err != nil {
return fmt.Errorf("could not pull all components: %v", err)
return errors.Errorf("could not pull all components: %v", err)
}
}

Expand Down Expand Up @@ -273,11 +274,11 @@ func (opts *Build) Run(cmd *cobra.Command, args []string) error {
}

if len(p) == 0 {
return fmt.Errorf("could not find: %s",
return errors.Errorf("could not find: %s",
unikraft.TypeNameVersion(component),
)
} else if len(p) > 1 {
return fmt.Errorf("too many options for %s",
return errors.Errorf("too many options for %s",
unikraft.TypeNameVersion(component),
)
}
Expand All @@ -303,7 +304,7 @@ func (opts *Build) Run(cmd *cobra.Command, args []string) error {
}

if err := treemodel.Start(); err != nil {
return fmt.Errorf("could not complete search: %v", err)
return errors.Errorf("could not complete search: %v", err)
}
}

Expand Down Expand Up @@ -339,7 +340,7 @@ func (opts *Build) Run(cmd *cobra.Command, args []string) error {
}

if err := paramodel.Start(); err != nil {
return fmt.Errorf("could not pull all components: %v", err)
return errors.Errorf("could not pull all components: %v", err)
}
}

Expand All @@ -354,7 +355,7 @@ func (opts *Build) Run(cmd *cobra.Command, args []string) error {
)

if len(selected) == 0 {
return fmt.Errorf("no targets selected to build")
return errors.Errorf("no targets selected to build")
}

var mopts []make.MakeOption
Expand Down
12 changes: 6 additions & 6 deletions cmd/kraft/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package events

import (
"context"
"errors"
"fmt"
"os"
"os/signal"
Expand All @@ -15,6 +14,7 @@ import (
"time"

"github.com/MakeNowJust/heredoc"
"github.com/juju/errors"
"github.com/spf13/cobra"

"kraftkit.sh/cmdfactory"
Expand Down Expand Up @@ -64,7 +64,7 @@ func (opts *Events) Run(cmd *cobra.Command, args []string) error {
store, err := machine.NewMachineStoreFromPath(config.G[config.KraftKit](ctx).RuntimeDir)
if err != nil {
cancel()
return fmt.Errorf("could not access machine store: %v", err)
return errors.Errorf("could not access machine store: %v", err)
}

var pidfile *os.File
Expand All @@ -79,7 +79,7 @@ func (opts *Events) Run(cmd *cobra.Command, args []string) error {
pidfile, err = os.OpenFile(config.G[config.KraftKit](ctx).EventsPidFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o666)
if err != nil {
cancel()
return fmt.Errorf("could not create pidfile: %v", err)
return errors.Errorf("could not create pidfile: %v", err)
}

defer func() {
Expand All @@ -93,12 +93,12 @@ func (opts *Events) Run(cmd *cobra.Command, args []string) error {

if _, err := pidfile.Write([]byte(fmt.Sprintf("%d", os.Getpid()))); err != nil {
cancel()
return fmt.Errorf("failed to write PID file: %w", err)
return errors.Errorf("failed to write PID file: %v", err)
}

if err := pidfile.Sync(); err != nil {
cancel()
return fmt.Errorf("could not sync pid file: %v", err)
return errors.Errorf("could not sync pid file: %v", err)
}
}

Expand Down Expand Up @@ -129,7 +129,7 @@ seek:
var mids []machine.MachineID
allMids, err := store.ListAllMachineIDs()
if err != nil {
return fmt.Errorf("could not list machines: %v", err)
return errors.Errorf("could not list machines: %v", err)
}

if len(args) > 0 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/kraft/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
package fetch

import (
"fmt"
"os"

"github.com/MakeNowJust/heredoc"
"github.com/juju/errors"
"github.com/spf13/cobra"

"kraftkit.sh/cmdfactory"
Expand Down Expand Up @@ -102,7 +102,7 @@ func (opts *Fetch) Run(cmd *cobra.Command, args []string) error {
t = targets[0]

case config.G[config.KraftKit](ctx).NoPrompt:
return fmt.Errorf("could not determine which target to prepare")
return errors.Errorf("could not determine which target to prepare")

default:
t, err = cli.SelectTarget(targets)
Expand Down
3 changes: 2 additions & 1 deletion cmd/kraft/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"kraftkit.sh/config"
"kraftkit.sh/iostreams"

"github.com/juju/errors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -63,7 +64,7 @@ func (opts *Login) Run(cmd *cobra.Command, args []string) error {

btoken, err := term.ReadPassword(int(iostreams.G(ctx).In.Fd()))
if err != nil {
return fmt.Errorf("could not read password: %v", err)
return errors.Errorf("could not read password: %v", err)
}

fmt.Fprint(iostreams.G(ctx).Out, "\n")
Expand Down
Loading

0 comments on commit ec96227

Please sign in to comment.