From adeb4a8558a2194fcfe4b9af0c13210cde180072 Mon Sep 17 00:00:00 2001 From: Chris Koch Date: Fri, 19 Jan 2024 03:30:04 +0000 Subject: [PATCH] Fix lint issues Signed-off-by: Chris Koch --- cmd/mkuimage/main.go | 44 +++++++++---------------- cmd/mkuimage/main_test.go | 29 ++++++++-------- cpio/archive.go | 8 +++-- cpio/archive_test.go | 11 +++---- cpio/const.go | 1 + cpio/cpio.go | 30 ----------------- cpio/fs_unix.go | 7 ++-- cpio/newc.go | 4 +-- cpio/newc_test.go | 11 ++++--- cpio/sysinfo_linux.go | 12 +++---- cpio/utils.go | 5 +-- ldd/ldd.go | 2 +- ldd/ldd_test.go | 2 +- ldd/ldd_unix.go | 6 +++- testutil/testutil.go | 17 ++++++---- uroot/builder/builder.go | 10 +++--- uroot/builder/gbb.go | 8 ++--- uroot/initramfs/archive.go | 5 ++- uroot/initramfs/files.go | 2 +- uroot/initramfs/files_test.go | 28 +++++++++------- uroot/initramfs/test/ramfs.go | 1 + uroot/test/bar/bar.go | 1 + uroot/test/foo/foo.go | 1 + uroot/test/gopath1/src/foo/foo.go | 1 + uroot/test/gopath1/src/os/os.go | 1 + uroot/test/gopath2/src/mypkga/mypkga.go | 1 + uroot/test/gopath2/src/mypkgb/mypkgb.go | 1 + uroot/uroot.go | 15 +++++---- uroot/uroot_test.go | 32 +++++++++--------- uroot/util/pkg_test.go | 25 -------------- uroot/util/usage.go | 1 + 31 files changed, 142 insertions(+), 180 deletions(-) delete mode 100644 uroot/util/pkg_test.go diff --git a/cmd/mkuimage/main.go b/cmd/mkuimage/main.go index a4adb5c..31b91e4 100644 --- a/cmd/mkuimage/main.go +++ b/cmd/mkuimage/main.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// Command mkuimage builds CPIO archives with the given files and Go commands. package main import ( @@ -27,21 +28,21 @@ import ( "github.com/u-root/uio/ulog" ) -// multiFlag is used for flags that support multiple invocations, e.g. -files +// multiFlag is used for flags that support multiple invocations, e.g. -files. type multiFlag []string func (m *multiFlag) String() string { return fmt.Sprint(*m) } +// Set implements flag.Value.Set. func (m *multiFlag) Set(value string) error { *m = append(*m, value) return nil } -// errors from the u-root command var ( - ErrEmptyFilesArg = errors.New("empty argument to -files") + errEmptyFilesArg = errors.New("empty argument to -files") ) // Flags for u-root builder. @@ -55,10 +56,7 @@ var ( statsOutputPath *string statsLabel *string shellbang *bool - // For the new gobusybox support - usegobusybox *bool - genDir *string - // For the new "filepath only" logic + // For the new "filepath only" logic. urootSourceDir *string ) @@ -93,9 +91,6 @@ func init() { statsOutputPath = flag.String("stats-output-path", "", "Write build stats to this file (JSON)") statsLabel = flag.String("stats-label", "", "Use this statsLabel when writing stats") - // Flags for the gobusybox, which we hope to move to, since it works with modules. - genDir = flag.String("gen-dir", "", "Directory to generate source in") - // Flag for the new filepath only mode. This will be required to find the u-root commands and make templates work // In almost every case, "." is fine. urootSourceDir = flag.String("uroot-source", ".", "Path to the locally checked out u-root source tree in case commands from there are desired.") @@ -110,8 +105,12 @@ type buildStats struct { func writeBuildStats(stats buildStats, path string) error { var allStats []buildStats - if data, err := os.ReadFile(*statsOutputPath); err == nil { - json.Unmarshal(data, &allStats) + data, err := os.ReadFile(*statsOutputPath) + if err != nil { + return err + } + if err := json.Unmarshal(data, &allStats); err != nil { + return err } found := false for i, s := range allStats { @@ -127,14 +126,11 @@ func writeBuildStats(stats buildStats, path string) error { return strings.Compare(allStats[i].Label, allStats[j].Label) == -1 }) } - data, err := json.MarshalIndent(allStats, "", " ") + data, err = json.MarshalIndent(allStats, "", " ") if err != nil { return err } - if err := os.WriteFile(*statsOutputPath, data, 0o644); err != nil { - return err - } - return nil + return os.WriteFile(*statsOutputPath, data, 0o644) } func generateLabel(env *golang.Environ) string { @@ -166,14 +162,14 @@ func checkArgs(args ...string) error { } if args[len(args)-1] == "-files" { - return fmt.Errorf("last argument is -files:%w", ErrEmptyFilesArg) + return fmt.Errorf("last argument is -files:%w", errEmptyFilesArg) } // We know the last arg is not -files; scan the arguments for -files // followed by a switch. for i := 0; i < len(args)-1; i++ { if args[i] == "-files" && args[i+1][0] == '-' { - return fmt.Errorf("-files argument %d is followed by a switch: %w", i, ErrEmptyFilesArg) + return fmt.Errorf("-files argument %d is followed by a switch: %w", i, errEmptyFilesArg) } } @@ -219,7 +215,7 @@ func main() { l.Fatalf("Build error: %v", err) } - elapsed := time.Now().Sub(start) + elapsed := time.Since(start) stats := buildStats{ Label: *statsLabel, @@ -256,14 +252,6 @@ func isRecommendedVersion(v string) bool { return false } -func canFindSource(dir string) error { - d := filepath.Join(dir, "cmds", "core") - if _, err := os.Stat(d); err != nil { - return fmt.Errorf("can not build u-root in %q:%w (-uroot-source may be incorrect or not set)", *urootSourceDir, os.ErrNotExist) - } - return nil -} - // Main is a separate function so defers are run on return, which they wouldn't // on exit. func Main(l ulog.Logger, env *golang.Environ, buildOpts *golang.BuildOpts) error { diff --git a/cmd/mkuimage/main_test.go b/cmd/mkuimage/main_test.go index 2cc4aaa..9ec3e64 100644 --- a/cmd/mkuimage/main_test.go +++ b/cmd/mkuimage/main_test.go @@ -56,7 +56,7 @@ func TestUrootCmdline(t *testing.T) { env: []string{"GO111MODULE=off"}, err: nil, validators: []itest.ArchiveValidator{ - itest.HasFile{"bin/bash"}, + itest.HasFile{Path: "bin/bash"}, }, }, { @@ -65,8 +65,8 @@ func TestUrootCmdline(t *testing.T) { env: []string{"GO111MODULE=off"}, err: nil, validators: []itest.ArchiveValidator{ - itest.HasFile{"/bin/foo"}, - itest.HasFile{"/bin/bar"}, + itest.HasFile{Path: "/bin/foo"}, + itest.HasFile{Path: "/bin/bar"}, }, }, { @@ -74,9 +74,9 @@ func TestUrootCmdline(t *testing.T) { args: []string{"-nocmd", "-files=/bin/bash", "-files=/bin/ls", fmt.Sprintf("-files=%s", samplef.Name())}, env: []string{"GO111MODULE=off"}, validators: []itest.ArchiveValidator{ - itest.HasFile{"bin/bash"}, - itest.HasFile{"bin/ls"}, - itest.HasFile{samplef.Name()}, + itest.HasFile{Path: "bin/bash"}, + itest.HasFile{Path: "bin/ls"}, + itest.HasFile{Path: samplef.Name()}, }, }, { @@ -84,7 +84,7 @@ func TestUrootCmdline(t *testing.T) { args: []string{"-nocmd", "-files=/bin/bash:bin/bush"}, env: []string{"GO111MODULE=off"}, validators: []itest.ArchiveValidator{ - itest.HasFile{"bin/bush"}, + itest.HasFile{Path: "bin/bush"}, }, }, { @@ -92,8 +92,8 @@ func TestUrootCmdline(t *testing.T) { args: []string{"-nocmd", "-files=/bin/bash:bin/bash", "-uinitcmd=/bin/bash"}, env: []string{"GO111MODULE=off"}, validators: []itest.ArchiveValidator{ - itest.HasFile{"bin/bash"}, - itest.HasRecord{cpio.Symlink("bin/uinit", "bash")}, + itest.HasFile{Path: "bin/bash"}, + itest.HasRecord{R: cpio.Symlink("bin/uinit", "bash")}, }, }, } @@ -104,7 +104,7 @@ func TestUrootCmdline(t *testing.T) { args: []string{"-uinitcmd=echo foobar fuzz", "-defaultsh=", "github.com/u-root/u-root/cmds/core/init", "github.com/u-root/u-root/cmds/core/echo"}, err: nil, validators: []itest.ArchiveValidator{ - itest.HasRecord{cpio.Symlink("bin/uinit", "../bbin/echo")}, + itest.HasRecord{R: cpio.Symlink("bin/uinit", "../bbin/echo")}, itest.HasContent{ Path: "etc/uinit.flags", Content: "\"foobar\"\n\"fuzz\"", @@ -209,7 +209,7 @@ func TestUrootCmdline(t *testing.T) { var bbTests []testCase for _, test := range bareTests { gbbTest := test - gbbTest.name = gbbTest.name + " gbb-gomodule" + gbbTest.name += " gbb-gomodule" gbbTest.args = append([]string{"-build=gbb"}, gbbTest.args...) gbbTest.env = append(gbbTest.env, "GO111MODULE=on") @@ -289,6 +289,7 @@ func TestUrootCmdline(t *testing.T) { } func buildIt(t *testing.T, args, env []string, want error) (*os.File, []byte, error) { + t.Helper() f, err := os.CreateTemp("", "u-root-") if err != nil { return nil, nil, err @@ -321,9 +322,9 @@ func TestCheckArgs(t *testing.T) { args []string err error }{ - {"-files is only arg", []string{"-files"}, ErrEmptyFilesArg}, - {"-files followed by -files", []string{"-files", "-files"}, ErrEmptyFilesArg}, - {"-files followed by any other switch", []string{"-files", "-abc"}, ErrEmptyFilesArg}, + {"-files is only arg", []string{"-files"}, errEmptyFilesArg}, + {"-files followed by -files", []string{"-files", "-files"}, errEmptyFilesArg}, + {"-files followed by any other switch", []string{"-files", "-abc"}, errEmptyFilesArg}, {"no args", []string{}, nil}, {"u-root alone", []string{"u-root"}, nil}, {"u-root with -files and other args", []string{"u-root", "-files", "/bin/bash", "core"}, nil}, diff --git a/cpio/archive.go b/cpio/archive.go index ff93bc0..3883839 100644 --- a/cpio/archive.go +++ b/cpio/archive.go @@ -30,12 +30,14 @@ func InMemArchive() *Archive { } // ArchiveFromRecords creates a new Archive from the records. -func ArchiveFromRecords(rs []Record) *Archive { +func ArchiveFromRecords(rs []Record) (*Archive, error) { a := InMemArchive() for _, r := range rs { - a.WriteRecord(r) + if err := a.WriteRecord(r); err != nil { + return nil, err + } } - return a + return a, nil } // ArchiveFromReader reads records from r into a new Archive in memory. diff --git a/cpio/archive_test.go b/cpio/archive_test.go index 417668b..4e14d17 100644 --- a/cpio/archive_test.go +++ b/cpio/archive_test.go @@ -23,7 +23,6 @@ func FuzzWriteReadInMemArchive(f *testing.F) { recs := []Record{} var i uint64 for i = 0; i < fileCount; i++ { - recs = append(recs, StaticRecord(content, Info{ Ino: ino | i, Mode: syscall.S_IFREG | mode | i, @@ -40,12 +39,14 @@ func FuzzWriteReadInMemArchive(f *testing.F) { })) } - arch := ArchiveFromRecords(recs) - archReader := arch.Reader() + arch, err := ArchiveFromRecords(recs) + if err != nil { + t.Fatal(err) + } + archReader := arch.Reader() for _, rec := range recs { readRec, err := archReader.ReadRecord() - if err != nil { t.Fatalf("failed to read record from archive") } @@ -53,11 +54,9 @@ func FuzzWriteReadInMemArchive(f *testing.F) { if !Equal(rec, readRec) { t.Fatalf("records not equal: %v %v", rec, readRec) } - if !arch.Contains(rec) { t.Fatalf("record not in archive %v %#v", rec, arch) } } - }) } diff --git a/cpio/const.go b/cpio/const.go index 0773794..0526577 100644 --- a/cpio/const.go +++ b/cpio/const.go @@ -8,6 +8,7 @@ package cpio // But we are unable to import the unix package when plan 9 is enabled, // so lucky us, the numbers have been the same for half a century. // It is ok to just define them. +// nolint const ( S_IEXEC = 0x40 S_IFBLK = 0x6000 diff --git a/cpio/cpio.go b/cpio/cpio.go index 1bd0125..f86722b 100644 --- a/cpio/cpio.go +++ b/cpio/cpio.go @@ -121,33 +121,3 @@ func Format(name string) (RecordFormat, error) { } return op, nil } - -func modeFromLinux(mode uint64) os.FileMode { - m := os.FileMode(mode & 0o777) - switch mode & S_IFMT { - case S_IFBLK: - m |= os.ModeDevice - case S_IFCHR: - m |= os.ModeDevice | os.ModeCharDevice - case S_IFDIR: - m |= os.ModeDir - case S_IFIFO: - m |= os.ModeNamedPipe - case S_IFLNK: - m |= os.ModeSymlink - case S_IFREG: - // nothing to do - case S_IFSOCK: - m |= os.ModeSocket - } - if mode&S_ISGID != 0 { - m |= os.ModeSetgid - } - if mode&S_ISUID != 0 { - m |= os.ModeSetuid - } - if mode&S_ISVTX != 0 { - m |= os.ModeSticky - } - return m -} diff --git a/cpio/fs_unix.go b/cpio/fs_unix.go index 46cdd34..b912af7 100644 --- a/cpio/fs_unix.go +++ b/cpio/fs_unix.go @@ -15,8 +15,8 @@ import ( "path/filepath" "syscall" - "github.com/u-root/uio/uio" "github.com/u-root/mkuimage/upath" + "github.com/u-root/uio/uio" "golang.org/x/sys/unix" ) @@ -51,10 +51,7 @@ func setModes(r Record) error { if err := os.Chown(r.Name, int(r.UID), int(r.GID)); err != nil { return err } - if err := os.Chmod(r.Name, toFileMode(r)); err != nil { - return err - } - return nil + return os.Chmod(r.Name, toFileMode(r)) } func toFileMode(r Record) os.FileMode { diff --git a/cpio/newc.go b/cpio/newc.go index d8bd3d0..4f7c4da 100644 --- a/cpio/newc.go +++ b/cpio/newc.go @@ -56,7 +56,7 @@ func headerFromInfo(i Info) header { return h } -func (h header) Info() Info { +func (h header) info() Info { var i Info i.Ino = uint64(h.Ino) i.Mode = uint64(h.Mode) @@ -307,7 +307,7 @@ func (r *reader) ReadRecord() (Record, error) { return Record{}, err } - info := hdr.Info() + info := hdr.info() info.Name = Normalize(string(nameBuf[:hdr.NameLength-1])) recLen := uint64(r.pos - recPos) diff --git a/cpio/newc_test.go b/cpio/newc_test.go index 88fbf9e..3d0f1f4 100644 --- a/cpio/newc_test.go +++ b/cpio/newc_test.go @@ -17,6 +17,7 @@ import ( "github.com/u-root/uio/uio" ) +//nolint:godot /* drwxrwxr-x 9 rminnich rminnich 0 Jan 22 22:18 . drwxr-xr-x 2 root root 0 Jan 22 22:18 etc @@ -576,7 +577,7 @@ func TestReproducible(t *testing.T) { t.Errorf("Could not write record %q: %v", rec[0].Name, err) } - if reflect.DeepEqual(b1.Bytes()[:], b2.Bytes()[:]) { + if reflect.DeepEqual(b1.Bytes(), b2.Bytes()) { t.Error("Reproducible: compared as same, wanted different") } @@ -585,7 +586,7 @@ func TestReproducible(t *testing.T) { b1 = &bytes.Buffer{} w = Newc.Writer(b1) - rec[0].ReaderAt = bytes.NewReader([]byte(contents)) + rec[0].ReaderAt = bytes.NewReader(contents) MakeAllReproducible(rec) if err := WriteRecords(w, rec); err != nil { t.Errorf("Could not write record %q: %v", rec[0].Name, err) @@ -594,16 +595,16 @@ func TestReproducible(t *testing.T) { b2 = &bytes.Buffer{} w = Newc.Writer(b2) rec[0].MTime++ - rec[0].ReaderAt = bytes.NewReader([]byte(contents)) + rec[0].ReaderAt = bytes.NewReader(contents) MakeAllReproducible(rec) if err := WriteRecords(w, rec); err != nil { t.Errorf("Could not write record %q: %v", rec[0].Name, err) } if len(b1.Bytes()) != len(b2.Bytes()) { - t.Fatalf("Reproducible \n%v,\n%v: len is different, wanted same", b1.Bytes()[:], b2.Bytes()[:]) + t.Fatalf("Reproducible \n%v,\n%v: len is different, wanted same", b1.Bytes(), b2.Bytes()) } - if !reflect.DeepEqual(b1.Bytes()[:], b2.Bytes()[:]) { + if !reflect.DeepEqual(b1.Bytes(), b2.Bytes()) { t.Error("Reproducible: compared different, wanted same") for i := range b1.Bytes() { a := b1.Bytes()[i] diff --git a/cpio/sysinfo_linux.go b/cpio/sysinfo_linux.go index 678c514..0f53f2b 100644 --- a/cpio/sysinfo_linux.go +++ b/cpio/sysinfo_linux.go @@ -14,14 +14,14 @@ func sysInfo(n string, sys *syscall.Stat_t) Info { Mode: uint64(sys.Mode), UID: uint64(sys.Uid), GID: uint64(sys.Gid), - NLink: uint64(sys.Nlink), + NLink: sys.Nlink, MTime: uint64(sys.Mtim.Sec), FileSize: uint64(sys.Size), - Dev: uint64(sys.Dev), - Major: uint64(sys.Dev >> 8), - Minor: uint64(sys.Dev & 0xff), - Rmajor: uint64(sys.Rdev >> 8), - Rminor: uint64(sys.Rdev & 0xff), + Dev: sys.Dev, + Major: sys.Dev >> 8, + Minor: sys.Dev & 0xff, + Rmajor: sys.Rdev >> 8, + Rminor: sys.Rdev & 0xff, Name: n, } } diff --git a/cpio/utils.go b/cpio/utils.go index dd908fa..92a8879 100644 --- a/cpio/utils.go +++ b/cpio/utils.go @@ -205,10 +205,7 @@ func Passthrough(r RecordReader, w RecordWriter) error { if err := Concat(w, r, nil); err != nil { return err } - if err := WriteTrailer(w); err != nil { - return err - } - return nil + return WriteTrailer(w) } // WriteTrailer writes the trailer record. diff --git a/ldd/ldd.go b/ldd/ldd.go index b0d1682..ee62282 100644 --- a/ldd/ldd.go +++ b/ldd/ldd.go @@ -5,7 +5,7 @@ //go:build freebsd || linux || darwin // +build freebsd linux darwin -// ldd returns library dependencies of an executable. +// Package ldd returns library dependencies of an executable. // // The way this is done on GNU-based systems is interesting. For each ELF, one // finds the .interp section. If there is no interpreter there's not much to diff --git a/ldd/ldd_test.go b/ldd/ldd_test.go index a5e5ff8..79e3622 100644 --- a/ldd/ldd_test.go +++ b/ldd/ldd_test.go @@ -27,7 +27,7 @@ func TestLdd(t *testing.T) { } // TestLddList tests that the LddList is the -// same as the info returned by Ldd +// same as the info returned by Ldd. func TestLddList(t *testing.T) { n, err := List("/bin/date") if err != nil { diff --git a/ldd/ldd_unix.go b/ldd/ldd_unix.go index 90ccf68..a656acd 100644 --- a/ldd/ldd_unix.go +++ b/ldd/ldd_unix.go @@ -56,6 +56,9 @@ func runinterp(interp, file string) ([]string, error) { return parseinterp(string(o)) } +// GetInterp returns the interpreter file path for the given ELF. +// +// It is not an error for file not to be an ELF. func GetInterp(file string) (string, error) { r, err := os.Open(file) if err != nil { @@ -64,7 +67,8 @@ func GetInterp(file string) (string, error) { defer r.Close() f, err := elf.NewFile(r) if err != nil { - return "", nil + // Not an ELF is not an error. + return "", nil //nolint:nilerr } s := f.Section(".interp") diff --git a/testutil/testutil.go b/testutil/testutil.go index 5a97e5e..bd83fa1 100644 --- a/testutil/testutil.go +++ b/testutil/testutil.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// Package testutil has utilities to test Go commands. package testutil import ( @@ -22,7 +23,8 @@ var binary string // Command decides which executable to call based on environment variables: // - EXECPATH="executable args" overrides any other test subject. // - UROOT_TEST_BUILD=1 will force compiling the u-root command in question. -func Command(t testing.TB, args ...string) *exec.Cmd { +func Command(tb testing.TB, args ...string) *exec.Cmd { + tb.Helper() // If EXECPATH is set, just use that. execPath := os.Getenv("EXECPATH") if len(execPath) > 0 { @@ -32,7 +34,7 @@ func Command(t testing.TB, args ...string) *exec.Cmd { // Should be cached by Run if os.Executable is going to fail. if len(binary) > 0 { - t.Logf("binary: %v", binary) + tb.Logf("binary: %v", binary) return exec.Command(binary, args...) } @@ -40,7 +42,7 @@ func Command(t testing.TB, args ...string) *exec.Cmd { if err != nil { // Run should have prevented this case by caching something in // `binary`. - t.Fatal("You must call testutil.Run() in your TestMain.") + tb.Fatal("You must call testutil.Run() in your TestMain.") } c := exec.Command(execPath, args...) @@ -74,19 +76,22 @@ func run(m *testing.M, mainFn func()) int { // This is NOT build-system-independent, and hence the fallback. tmpDir, err := os.MkdirTemp("", "uroot-build") if err != nil { - log.Fatal(err) + log.Print(err) + return 1 } defer os.RemoveAll(tmpDir) wd, err := os.Getwd() if err != nil { - log.Fatal(err) + log.Print(err) + return 1 } execPath := filepath.Join(tmpDir, "binary") // Build the stuff. if err := golang.Default().BuildDir(wd, execPath, nil); err != nil { - log.Fatal(err) + log.Print(err) + return 1 } // Cache dat. diff --git a/uroot/builder/builder.go b/uroot/builder/builder.go index bc00c21..0de02c7 100644 --- a/uroot/builder/builder.go +++ b/uroot/builder/builder.go @@ -2,12 +2,14 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// Package builder has methods for building many Go commands into an initramfs +// archive. package builder import ( - gbbgolang "github.com/u-root/gobusybox/src/pkg/golang" - "github.com/u-root/uio/ulog" + "github.com/u-root/gobusybox/src/pkg/golang" "github.com/u-root/mkuimage/uroot/initramfs" + "github.com/u-root/uio/ulog" ) var ( @@ -20,11 +22,11 @@ var ( // Opts are options passed to the Builder.Build function. type Opts struct { // Env is the Go compiler environment. - Env *gbbgolang.Environ + Env *golang.Environ // Build options for building go binaries. Ultimate this holds all the // args that end up being passed to `go build`. - BuildOpts *gbbgolang.BuildOpts + BuildOpts *golang.BuildOpts // Packages are the Go packages to compile. // diff --git a/uroot/builder/gbb.go b/uroot/builder/gbb.go index 97fa04e..727547b 100644 --- a/uroot/builder/gbb.go +++ b/uroot/builder/gbb.go @@ -12,8 +12,8 @@ import ( "github.com/u-root/gobusybox/src/pkg/bb" "github.com/u-root/mkuimage/cpio" - "github.com/u-root/uio/ulog" "github.com/u-root/mkuimage/uroot/initramfs" + "github.com/u-root/uio/ulog" ) // Commands to skip building in bb mode. @@ -84,11 +84,11 @@ func (b GBBBuilder) Build(l ulog.Logger, af *initramfs.Files, opts Opts) error { var errGomod *bb.ErrModuleBuild if errors.As(err, &errGopath) { return fmt.Errorf("preserving bb generated source directory at %s due to error. To reproduce build, `cd %s` and `GO111MODULE=off GOPATH=%s go build`: %v", opts.TempDir, errGopath.CmdDir, errGopath.GOPATH, err) - } else if errors.As(err, &errGomod) { + } + if errors.As(err, &errGomod) { return fmt.Errorf("preserving bb generated source directory at %s due to error. To debug build, `cd %s` and use `go build` to build, or `go mod [why|tidy|graph]` to debug dependencies, or `go list -m all` to list all dependency versions:\n%v", opts.TempDir, errGomod.CmdDir, err) - } else { - return fmt.Errorf("preserving bb generated source directory at %s due to error:\n%v", opts.TempDir, err) } + return fmt.Errorf("preserving bb generated source directory at %s due to error:\n%v", opts.TempDir, err) } if err := af.AddFile(bbPath, "bbin/bb"); err != nil { diff --git a/uroot/initramfs/archive.go b/uroot/initramfs/archive.go index 1d57434..4badbbd 100644 --- a/uroot/initramfs/archive.go +++ b/uroot/initramfs/archive.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// Package initramfs can write archives out to CPIO or directories. package initramfs import ( @@ -13,10 +14,12 @@ import ( ) var ( + // CPIO creates files in a CPIO file. CPIO = CPIOArchiver{ RecordFormat: cpio.Newc, } + // Dir writes "archived" files to a directory. Dir = DirArchiver{} // Archivers are the supported initramfs archivers at the moment. @@ -117,7 +120,7 @@ func Write(opts *Opts) error { } // TODO: ignore only the error where it already exists // in archive. - opts.Files.AddRecord(transform(f)) + _ = opts.Files.AddRecord(transform(f)) } } diff --git a/uroot/initramfs/files.go b/uroot/initramfs/files.go index 6e05fd2..da8de05 100644 --- a/uroot/initramfs/files.go +++ b/uroot/initramfs/files.go @@ -177,7 +177,7 @@ func (af *Files) addParent(name string) { return } if !af.Contains(parent) { - af.AddRecord(cpio.Directory(parent, 0o755)) + _ = af.AddRecord(cpio.Directory(parent, 0o755)) } af.addParent(parent) } diff --git a/uroot/initramfs/files_test.go b/uroot/initramfs/files_test.go index 41be338..66c5160 100644 --- a/uroot/initramfs/files_test.go +++ b/uroot/initramfs/files_test.go @@ -27,8 +27,11 @@ func TestFilesAddFileNoFollow(t *testing.T) { dir := t.TempDir() dir2 := t.TempDir() - os.Create(filepath.Join(dir, "foo2")) - os.Symlink(filepath.Join(dir, "foo2"), filepath.Join(dir2, "foo3")) + //nolint:errcheck + { + os.Create(filepath.Join(dir, "foo2")) + os.Symlink(filepath.Join(dir, "foo2"), filepath.Join(dir2, "foo3")) + } for i, tt := range []struct { name string @@ -92,16 +95,19 @@ func TestFilesAddFile(t *testing.T) { dir2 := t.TempDir() dir3 := t.TempDir() - os.Create(filepath.Join(dir, "foo")) - os.Create(filepath.Join(dir, "foo2")) - os.Symlink(filepath.Join(dir, "foo2"), filepath.Join(dir2, "foo3")) - - fooDir := filepath.Join(dir3, "fooDir") - os.Mkdir(fooDir, os.ModePerm) symlinkToDir3 := filepath.Join(dir3, "fooSymDir/") - os.Symlink(fooDir, symlinkToDir3) - os.Create(filepath.Join(fooDir, "foo")) - os.Create(filepath.Join(fooDir, "bar")) + fooDir := filepath.Join(dir3, "fooDir") + //nolint:errcheck + { + os.Create(filepath.Join(dir, "foo")) + os.Create(filepath.Join(dir, "foo2")) + os.Symlink(filepath.Join(dir, "foo2"), filepath.Join(dir2, "foo3")) + + os.Mkdir(fooDir, os.ModePerm) + os.Symlink(fooDir, symlinkToDir3) + os.Create(filepath.Join(fooDir, "foo")) + os.Create(filepath.Join(fooDir, "bar")) + } for i, tt := range []struct { name string diff --git a/uroot/initramfs/test/ramfs.go b/uroot/initramfs/test/ramfs.go index a01be39..a998df5 100644 --- a/uroot/initramfs/test/ramfs.go +++ b/uroot/initramfs/test/ramfs.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// nolint package test import ( diff --git a/uroot/test/bar/bar.go b/uroot/test/bar/bar.go index 46f025c..2154ea0 100644 --- a/uroot/test/bar/bar.go +++ b/uroot/test/bar/bar.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// nolint package bar import "fmt" diff --git a/uroot/test/foo/foo.go b/uroot/test/foo/foo.go index dc67b21..2a8501d 100644 --- a/uroot/test/foo/foo.go +++ b/uroot/test/foo/foo.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// nolint package main import ( diff --git a/uroot/test/gopath1/src/foo/foo.go b/uroot/test/gopath1/src/foo/foo.go index 5e16ecd..33b862a 100644 --- a/uroot/test/gopath1/src/foo/foo.go +++ b/uroot/test/gopath1/src/foo/foo.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// nolint package main func main() { diff --git a/uroot/test/gopath1/src/os/os.go b/uroot/test/gopath1/src/os/os.go index 5e16ecd..33b862a 100644 --- a/uroot/test/gopath1/src/os/os.go +++ b/uroot/test/gopath1/src/os/os.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// nolint package main func main() { diff --git a/uroot/test/gopath2/src/mypkga/mypkga.go b/uroot/test/gopath2/src/mypkga/mypkga.go index 5e16ecd..33b862a 100644 --- a/uroot/test/gopath2/src/mypkga/mypkga.go +++ b/uroot/test/gopath2/src/mypkga/mypkga.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// nolint package main func main() { diff --git a/uroot/test/gopath2/src/mypkgb/mypkgb.go b/uroot/test/gopath2/src/mypkgb/mypkgb.go index 5e16ecd..33b862a 100644 --- a/uroot/test/gopath2/src/mypkgb/mypkgb.go +++ b/uroot/test/gopath2/src/mypkgb/mypkgb.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// nolint package main func main() { diff --git a/uroot/uroot.go b/uroot/uroot.go index e44e15a..2fa5942 100644 --- a/uroot/uroot.go +++ b/uroot/uroot.go @@ -21,9 +21,9 @@ import ( "github.com/u-root/mkuimage/cpio" "github.com/u-root/mkuimage/ldd" "github.com/u-root/mkuimage/uflag" - "github.com/u-root/uio/ulog" "github.com/u-root/mkuimage/uroot/builder" "github.com/u-root/mkuimage/uroot/initramfs" + "github.com/u-root/uio/ulog" ) // These constants are used in DefaultRamfs. @@ -37,13 +37,13 @@ const ( nameserver = "nameserver 8.8.8.8\n" ) -// DefaultRamRamfs returns a cpio.Archive for the target OS. +// DefaultRamfs returns a cpio.Archive for the target OS. // If an OS is not known it will return a reasonable u-root specific // default. func DefaultRamfs() *cpio.Archive { switch gbbgolang.Default().GOOS { case "linux": - return cpio.ArchiveFromRecords([]cpio.Record{ + a, _ := cpio.ArchiveFromRecords([]cpio.Record{ cpio.Directory("bin", 0o755), cpio.Directory("dev", 0o755), cpio.Directory("env", 0o755), @@ -65,11 +65,13 @@ func DefaultRamfs() *cpio.Archive { cpio.StaticFile("etc/resolv.conf", nameserver, 0o644), cpio.StaticFile("etc/localtime", gmt0, 0o644), }) + return a default: - return cpio.ArchiveFromRecords([]cpio.Record{ + a, _ := cpio.ArchiveFromRecords([]cpio.Record{ cpio.Directory("ubin", 0o755), cpio.Directory("bbin", 0o755), }) + return a } } @@ -423,7 +425,7 @@ func ParseExtraFiles(logger ulog.Logger, archive *initramfs.Files, extraFiles [] // step. The file will still be included from above. f, err := elf.Open(name) if err != nil { - return nil + return nil //nolint:nilerr } if err = f.Close(); err != nil { logger.Printf("WARNING: Closing ELF file %q: %v", name, err) @@ -453,10 +455,11 @@ func (o *Opts) AddCommands(c ...Commands) { o.Commands = append(o.Commands, c...) } +// AddBusyBoxCommands adds Go commands to the busybox build. func (o *Opts) AddBusyBoxCommands(pkgs ...string) { for i, cmds := range o.Commands { if cmds.Builder == builder.BusyBox { - o.Commands[i].Packages = append(cmds.Packages, pkgs...) + o.Commands[i].Packages = append(o.Commands[i].Packages, pkgs...) return } } diff --git a/uroot/uroot_test.go b/uroot/uroot_test.go index 34a4610..e099763 100644 --- a/uroot/uroot_test.go +++ b/uroot/uroot_test.go @@ -13,9 +13,9 @@ import ( "github.com/u-root/gobusybox/src/pkg/golang" "github.com/u-root/mkuimage/cpio" - "github.com/u-root/uio/ulog/ulogtest" "github.com/u-root/mkuimage/uroot/builder" itest "github.com/u-root/mkuimage/uroot/initramfs/test" + "github.com/u-root/uio/ulog/ulogtest" ) type inMemArchive struct { @@ -69,11 +69,11 @@ func TestCreateInitramfs(t *testing.T) { }, want: "", validators: []itest.ArchiveValidator{ - itest.HasFile{"bbin/bb"}, - itest.HasRecord{cpio.Symlink("bbin/init", "bb")}, - itest.HasRecord{cpio.Symlink("bbin/ls", "bb")}, - itest.HasRecord{cpio.Symlink("bin/defaultsh", "../bbin/ls")}, - itest.HasRecord{cpio.Symlink("bin/sh", "../bbin/ls")}, + itest.HasFile{Path: "bbin/bb"}, + itest.HasRecord{R: cpio.Symlink("bbin/init", "bb")}, + itest.HasRecord{R: cpio.Symlink("bbin/ls", "bb")}, + itest.HasRecord{R: cpio.Symlink("bin/defaultsh", "../bbin/ls")}, + itest.HasRecord{R: cpio.Symlink("bin/sh", "../bbin/ls")}, }, }, { @@ -94,7 +94,7 @@ func TestCreateInitramfs(t *testing.T) { }, want: "", validators: []itest.ArchiveValidator{ - itest.MissingFile{"bbin/bb"}, + itest.MissingFile{Path: "bbin/bb"}, }, }, { @@ -127,7 +127,7 @@ func TestCreateInitramfs(t *testing.T) { }, want: "", validators: []itest.ArchiveValidator{ - itest.HasRecord{cpio.Symlink("init", "bin/systemd")}, + itest.HasRecord{R: cpio.Symlink("init", "bin/systemd")}, }, }, { @@ -159,18 +159,18 @@ func TestCreateInitramfs(t *testing.T) { }, want: "", validators: []itest.ArchiveValidator{ - itest.HasRecord{cpio.Symlink("init", "bbin/init")}, + itest.HasRecord{R: cpio.Symlink("init", "bbin/init")}, // bb mode. - itest.HasFile{"bbin/bb"}, - itest.HasRecord{cpio.Symlink("bbin/init", "bb")}, - itest.HasRecord{cpio.Symlink("bbin/ls", "bb")}, - itest.HasRecord{cpio.Symlink("bin/defaultsh", "../bbin/ls")}, - itest.HasRecord{cpio.Symlink("bin/sh", "../bbin/ls")}, + itest.HasFile{Path: "bbin/bb"}, + itest.HasRecord{R: cpio.Symlink("bbin/init", "bb")}, + itest.HasRecord{R: cpio.Symlink("bbin/ls", "bb")}, + itest.HasRecord{R: cpio.Symlink("bin/defaultsh", "../bbin/ls")}, + itest.HasRecord{R: cpio.Symlink("bin/sh", "../bbin/ls")}, // binary mode. - itest.HasFile{"bin/cp"}, - itest.HasFile{"bin/dd"}, + itest.HasFile{Path: "bin/cp"}, + itest.HasFile{Path: "bin/dd"}, }, }, } { diff --git a/uroot/util/pkg_test.go b/uroot/util/pkg_test.go deleted file mode 100644 index 7ee1211..0000000 --- a/uroot/util/pkg_test.go +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2021 the u-root Authors. All rights reserved -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package util - -import ( - "bytes" - "fmt" - "os" - "testing" -) - -func TestTODO(t *testing.T) { - b := &bytes.Buffer{} - f := func() { - fmt.Fprintf(b, "hi %s", os.Args[0]) - } - - f = Usage(f, "there") - f() - if b.String() != "hi there" { - t.Errorf("f(): Got %q, want %q", b.String(), "hi there") - } -} diff --git a/uroot/util/usage.go b/uroot/util/usage.go index b03fbb5..76f05ab 100644 --- a/uroot/util/usage.go +++ b/uroot/util/usage.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// Package util makes CLI usage strings easier. package util import (