Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

With API #14

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 3 additions & 76 deletions cmd/mkuimage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@
package main

import (
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"os"
"path"
"runtime"
"sort"
"strings"
"time"

"github.com/dustin/go-humanize"
"github.com/hugelgupf/go-shlex"
"github.com/u-root/gobusybox/src/pkg/golang"
"github.com/u-root/gobusybox/src/pkg/uflag"
Expand Down Expand Up @@ -95,56 +92,6 @@ func init() {
urootSourceDir = flag.String("uroot-source", ".", "Path to the locally checked out u-root source tree in case commands from there are desired.")
}

type buildStats struct {
Label string `json:"label,omitempty"`
Time int64 `json:"time"`
Duration float64 `json:"duration"`
OutputSize int64 `json:"output_size"`
}

func writeBuildStats(stats buildStats, path string) error {
var allStats []buildStats
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 {
if s.Label == stats.Label {
allStats[i] = stats
found = true
break
}
}
if !found {
allStats = append(allStats, stats)
sort.Slice(allStats, func(i, j int) bool {
return strings.Compare(allStats[i].Label, allStats[j].Label) == -1
})
}
data, err = json.MarshalIndent(allStats, "", " ")
if err != nil {
return err
}
return os.WriteFile(*statsOutputPath, data, 0o644)
}

func generateLabel(env *golang.Environ) string {
var baseCmds []string
if len(flag.Args()) > 0 {
// Use the last component of the name to keep the label short
for _, e := range flag.Args() {
baseCmds = append(baseCmds, path.Base(e))
}
} else {
baseCmds = []string{"core"}
}
return fmt.Sprintf("%s-%s-%s-%s", *build, env.GOOS, env.GOARCH, strings.Join(baseCmds, "_"))
}

// checkArgs checks for common mistakes that cause confusion.
// 1. -files as the last argument
// 2. -files followed by any switch, indicating a shell expansion problem
Expand Down Expand Up @@ -207,33 +154,13 @@ func main() {
l.Printf("GOOS is not linux. Did you mean to set GOOS=linux?")
}

start := time.Now()

// Main is in a separate functions so defers run on return.
if err := Main(l, env, gbbOpts); err != nil {
l.Fatalf("Build error: %v", err)
}

elapsed := time.Since(start)

stats := buildStats{
Label: *statsLabel,
Time: start.Unix(),
Duration: float64(elapsed.Milliseconds()) / 1000,
}
if stats.Label == "" {
stats.Label = generateLabel(env)
}
if stat, err := os.Stat(*outputPath); err == nil && stat.ModTime().After(start) {
l.Printf("Successfully built %q (size %d).", *outputPath, stat.Size())
stats.OutputSize = stat.Size()
if *statsOutputPath != "" {
if err := writeBuildStats(stats, *statsOutputPath); err == nil {
l.Printf("Wrote stats to %q (label %q)", *statsOutputPath, stats.Label)
} else {
l.Printf("Failed to write stats to %s: %v", *statsOutputPath, err)
}
}
if stat, err := os.Stat(*outputPath); err == nil {
l.Printf("Successfully built %q (size %d bytes -- %s).", *outputPath, stat.Size(), humanize.IBytes(uint64(stat.Size())))
}
}

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ go 1.21

require (
github.com/hugelgupf/go-shlex v0.0.0-20200702092117-c80c9d0918fa
github.com/u-root/gobusybox/src v0.0.0-20240209041341-8c409c9832aa
github.com/u-root/gobusybox/src v0.0.0-20240212035024-44ff0bf359ad
github.com/u-root/u-root v0.12.0
github.com/u-root/uio v0.0.0-20240118234441-a3c409a6018e
golang.org/x/sync v0.6.0
golang.org/x/sys v0.16.0
golang.org/x/tools v0.17.0
)
Expand All @@ -28,6 +29,5 @@ require (
github.com/vishvananda/netns v0.0.4 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sync v0.6.0 // indirect
src.elv.sh v0.16.0-rc1.0.20220116211855-fda62502ad7f // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/u-root/gobusybox/src v0.0.0-20240209041341-8c409c9832aa h1:+0QRJrUq4ouVyrqwpKP18OXp3cbCdfQ7wvtihc/7r2M=
github.com/u-root/gobusybox/src v0.0.0-20240209041341-8c409c9832aa/go.mod h1:vN1IwhlCo7gTDTJDUs6WCKM4/C2uiq5w0XvZCqLtb5s=
github.com/u-root/gobusybox/src v0.0.0-20240212035024-44ff0bf359ad h1:lUSEFqsEuc+c+sTI5jVEC0wWw0FOuXZbrYGZbxQL19E=
github.com/u-root/gobusybox/src v0.0.0-20240212035024-44ff0bf359ad/go.mod h1:vN1IwhlCo7gTDTJDUs6WCKM4/C2uiq5w0XvZCqLtb5s=
github.com/u-root/u-root v0.12.0 h1:K0AuBFriwr0w/PGS3HawiAw89e3+MU7ks80GpghAsNs=
github.com/u-root/u-root v0.12.0/go.mod h1:FYjTOh4IkIZHhjsd17lb8nYW6udgXdJhG1c0r6u0arI=
github.com/u-root/uio v0.0.0-20240118234441-a3c409a6018e h1:BA9O3BmlTmpjbvajAwzWx4Wo2TRVdpPXZEeemGQcajw=
Expand Down
Loading
Loading