From 32136b8cb18944157ff1f361bc0755f6b627fd47 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 21 Feb 2026 10:25:39 +0200 Subject: Align Go project structure and add Mage targets Amp-Thread-ID: https://ampcode.com/threads/T-019c7f3b-1326-767b-94d5-366b91eaf712 Co-authored-by: Amp --- Magefile.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++ cmd/ior/main.go | 5 +++-- internal/flags/flags.go | 47 +++++++++++++++++++++--------------------- internal/flags/version.go | 18 +++++++++------- 4 files changed, 90 insertions(+), 32 deletions(-) create mode 100644 Magefile.go diff --git a/Magefile.go b/Magefile.go new file mode 100644 index 0000000..7f63a22 --- /dev/null +++ b/Magefile.go @@ -0,0 +1,52 @@ +//go:build mage + +// Magefile for ior targets: build, test, install. +package main + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/magefile/mage/mg" + "github.com/magefile/mage/sh" +) + +const binaryName = "ior" + +// Default builds the project. +func Default() { + mg.Deps(Build) +} + +// Build compiles the binary. +func Build() error { + return sh.RunV("go", "build", "-o", binaryName, "./cmd/ior") +} + +// Test runs the full test suite. +func Test() error { + return sh.RunV("go", "test", "./...") +} + +// Install copies the binary into GOPATH/bin. +func Install() error { + mg.Deps(Build) + + goPath := os.Getenv("GOPATH") + if goPath == "" { + home, err := os.UserHomeDir() + if err != nil { + return fmt.Errorf("resolve home directory: %w", err) + } + goPath = filepath.Join(home, "go") + } + + binDir := filepath.Join(goPath, "bin") + if err := os.MkdirAll(binDir, 0o755); err != nil { + return fmt.Errorf("ensure %s: %w", binDir, err) + } + + dest := filepath.Join(binDir, binaryName) + return sh.RunV("cp", "-v", binaryName, dest) +} diff --git a/cmd/ior/main.go b/cmd/ior/main.go index 0f3b38e..595f8f6 100644 --- a/cmd/ior/main.go +++ b/cmd/ior/main.go @@ -2,10 +2,11 @@ package main import ( "fmt" - "ior/internal" - "ior/internal/flags" "os" "runtime" + + "ior/internal" + "ior/internal/flags" ) // main is the entry point for the application. It checks if the OS is Linux, diff --git a/internal/flags/flags.go b/internal/flags/flags.go index 512697c..de0e74f 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -17,25 +17,25 @@ var ( once sync.Once ) -var validCollapsedFields = []string{ - "path", - "comm", - "tracepoint", - "pid", - "tid", - "flags", -} +const flamegraphToolDefault = "$HOME/git/FlameGraph/flamegraph.pl" -var validCollapsedCounts = []string{ - "count", - "duration", - "durationToPrev", - "bytes", -} +var ( + validCollapsedFields = []string{ + "path", + "comm", + "tracepoint", + "pid", + "tid", + "flags", + } -func Get() Flags { - return singleton -} + validCollapsedCounts = []string{ + "count", + "duration", + "durationToPrev", + "bytes", + } +) type Flags struct { PidFilter int @@ -63,6 +63,10 @@ type Flags struct { FlamegraphTool string } +func Get() Flags { + return singleton +} + func Parse() { once.Do(func() { parse() @@ -70,8 +74,6 @@ func Parse() { } func parse() { - version := flag.Bool("version", false, "Print version") - flag.IntVar(&singleton.PidFilter, "pid", -1, "Filter for processes ID") flag.IntVar(&singleton.TidFilter, "tid", -1, "Filter for thread ID") flag.IntVar(&singleton.EventMapSize, "mapSize", 4096*16, "BPF FD event ring buffer map size") @@ -96,12 +98,11 @@ func parse() { // https://github.com/brendangregg/FlameGraph flag.StringVar(&singleton.FlamegraphTool, "flamegraphTool", - os.Getenv("HOME")+"/git/FlameGraph/flamegraph.pl", "Path to the flamegraph tool (e.g. flamegraph.pl or inferno-flamegraph)") + "", "Path to the flamegraph tool (e.g. flamegraph.pl or inferno-flamegraph)") flag.Parse() - if *version { - PrintVersion() - os.Exit(0) + if singleton.FlamegraphTool == "" { + singleton.FlamegraphTool = flamegraphToolDefault } singleton.TracepointsToAttach = extractTracepointFlags(*tracepointsToAttach) diff --git a/internal/flags/version.go b/internal/flags/version.go index 1cb2dfd..7e6c50e 100644 --- a/internal/flags/version.go +++ b/internal/flags/version.go @@ -1,13 +1,17 @@ package flags -const version = `v0.0.0` +import "fmt" -const asciiBanner = ` ___   _____    ___ _     _    -|_ _| / / _ \  | _ (_)___| |_  - | | / / (_) | |   / / _ \  _| -|___/_/ \___/  |_|_\_\___/\__| NG - ` + version +// Version is the current application version. +const Version = "v0.0.0" +const asciiBannerTemplate = ` ___ _____ ___ _ _ +|_ _| / / _ \ | _ (_)___| |_ + | | / / (_) | | / / _ \ _| +|___/_/ \___/ |_|_\_\___/\__| NG + %s` + +// PrintVersion prints the banner with the current version. func PrintVersion() { - println(asciiBanner) + fmt.Printf(asciiBannerTemplate+"\n", Version) } -- cgit v1.2.3