diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 7b2e86f..8304f63 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -19,7 +19,7 @@ jobs: name: Cross-platform builds strategy: matrix: - go-version: ['1.21.x'] + go-version: ['1.21.x', '1.22.x'] runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -35,7 +35,7 @@ jobs: name: Build and test strategy: matrix: - go-version: ['1.21.x'] + go-version: ['1.21.x', '1.22.x'] platform: [ubuntu-latest] runs-on: ${{ matrix.platform }} @@ -68,7 +68,7 @@ jobs: name: Race test strategy: matrix: - go-version: ['1.21.x'] + go-version: ['1.21.x', '1.22.x'] platform: [ubuntu-latest] runs-on: ${{ matrix.platform }} diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 834d661..64c3ef6 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -22,7 +22,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-go@v4 with: - go-version: '1.21.x' + go-version: '1.22.x' - name: golangci-lint uses: golangci/golangci-lint-action@v3 with: diff --git a/cmd/mkuimage/main.go b/cmd/mkuimage/main.go index 156eaa8..4013a3c 100644 --- a/cmd/mkuimage/main.go +++ b/cmd/mkuimage/main.go @@ -241,6 +241,7 @@ func main() { var recommendedVersions = []string{ "go1.20", "go1.21", + "go1.22", } func isRecommendedVersion(v string) bool { @@ -275,12 +276,13 @@ func Main(l ulog.Logger, env *golang.Environ, buildOpts *golang.BuildOpts) error // Open the target initramfs file. if *outputPath == "" { - if len(env.GOOS) == 0 && len(env.GOARCH) == 0 { - return fmt.Errorf("passed no path, GOOS, and GOARCH to CPIOArchiver.OpenWriter") + *outputPath, err = archiver.CreateDefault(env) + if err != nil { + return err } - *outputPath = fmt.Sprintf("/tmp/initramfs.%s_%s.cpio", env.GOOS, env.GOARCH) + l.Printf("Output path is %v", *outputPath) } - w, err := archiver.OpenWriter(l, *outputPath) + w, err := archiver.OpenWriter(*outputPath) if err != nil { return err } diff --git a/uroot/initramfs/archive.go b/uroot/initramfs/archive.go index 4badbbd..99576de 100644 --- a/uroot/initramfs/archive.go +++ b/uroot/initramfs/archive.go @@ -9,8 +9,8 @@ import ( "fmt" "io" + "github.com/u-root/gobusybox/src/pkg/golang" "github.com/u-root/mkuimage/cpio" - "github.com/u-root/uio/ulog" ) var ( @@ -35,8 +35,11 @@ var ( // Archiver is an archive format that builds an archive using a given set of // files. type Archiver interface { + // CreateDefault creates a default file that can be passed as path to OpenWriter. + CreateDefault(env *golang.Environ) (string, error) + // OpenWriter opens an archive writer at `path`. - OpenWriter(l ulog.Logger, path string) (Writer, error) + OpenWriter(path string) (Writer, error) // Reader returns a Reader that allows reading files from a file. Reader(file io.ReaderAt) Reader diff --git a/uroot/initramfs/cpio.go b/uroot/initramfs/cpio.go index 578bab3..199308b 100644 --- a/uroot/initramfs/cpio.go +++ b/uroot/initramfs/cpio.go @@ -9,8 +9,8 @@ import ( "io" "os" + "github.com/u-root/gobusybox/src/pkg/golang" "github.com/u-root/mkuimage/cpio" - "github.com/u-root/uio/ulog" ) // CPIOArchiver is an implementation of Archiver for the cpio format. @@ -18,9 +18,18 @@ type CPIOArchiver struct { cpio.RecordFormat } +// CreateDefault chooses /tmp/initramfs.cpio or /tmp/initramfs.GOOS_GOARCH.cpio +// if available. +func (ca CPIOArchiver) CreateDefault(env *golang.Environ) (string, error) { + if len(env.GOOS) == 0 || len(env.GOARCH) == 0 { + return "/tmp/initramfs.cpio", nil + } + return fmt.Sprintf("/tmp/initramfs.%s_%s.cpio", env.GOOS, env.GOARCH), nil +} + // OpenWriter opens `path` as the correct file type and returns an // Writer pointing to `path`. -func (ca CPIOArchiver) OpenWriter(l ulog.Logger, path string) (Writer, error) { +func (ca CPIOArchiver) OpenWriter(path string) (Writer, error) { if len(path) == 0 { return nil, fmt.Errorf("path is required") } diff --git a/uroot/initramfs/dir.go b/uroot/initramfs/dir.go index 970c4a5..4317009 100644 --- a/uroot/initramfs/dir.go +++ b/uroot/initramfs/dir.go @@ -5,12 +5,13 @@ package initramfs import ( + "errors" "fmt" "io" "os" + "github.com/u-root/gobusybox/src/pkg/golang" "github.com/u-root/mkuimage/cpio" - "github.com/u-root/uio/ulog" ) // DirArchiver implements Archiver for a directory. @@ -23,23 +24,24 @@ func (da DirArchiver) Reader(io.ReaderAt) Reader { return nil } +// CreateDefault creates a tmpdir using os.MkdirTemp prefixed with mkuimage- or +// mkuimage-GOOS-GOARCH- if available. +func (da DirArchiver) CreateDefault(env *golang.Environ) (string, error) { + name := "mkuimage-" + if len(env.GOOS) != 0 && len(env.GOARCH) != 0 { + name = fmt.Sprintf("mkuimage-%s-%s-", env.GOOS, env.GOARCH) + } + return os.MkdirTemp("", name) +} + // OpenWriter implements Archiver.OpenWriter. -func (da DirArchiver) OpenWriter(l ulog.Logger, path string) (Writer, error) { +func (da DirArchiver) OpenWriter(path string) (Writer, error) { if len(path) == 0 { - var err error - path, err = os.MkdirTemp("", "u-root") - if err != nil { - return nil, err - } - } else { - if _, err := os.Stat(path); os.IsExist(err) { - return nil, fmt.Errorf("path %q already exists", path) - } - if err := os.MkdirAll(path, 0o755); err != nil { - return nil, err - } + return nil, fmt.Errorf("path is required") + } + if err := os.MkdirAll(path, 0o755); err != nil && !errors.Is(err, os.ErrExist) { + return nil, err } - l.Printf("Path is %s", path) return dirWriter{path}, nil }