From be0e387ab07c7d9f1441365a05c4c0141779184a Mon Sep 17 00:00:00 2001 From: Ophestra Date: Thu, 13 Nov 2025 07:29:46 +0900 Subject: internal/info: relocate from internal This is cleaner and makes more sense. The longer LDFLAGS was never a valid concern since it is always inserted by a script. Signed-off-by: Ophestra --- cmd/hakurei/command.go | 4 ++-- cmd/hakurei/print.go | 20 +++++++++---------- cmd/hpkg/proc.go | 4 ++-- dist/release.sh | 6 +++--- internal/info/path.go | 33 +++++++++++++++++++++++++++++++ internal/info/path_test.go | 45 ++++++++++++++++++++++++++++++++++++++++++ internal/info/version.go | 19 ++++++++++++++++++ internal/outcome/dispatcher.go | 4 ++-- internal/outcome/process.go | 4 ++-- internal/path.go | 33 ------------------------------- internal/path_test.go | 45 ------------------------------------------ internal/version.go | 19 ------------------ package.nix | 2 +- 13 files changed, 119 insertions(+), 119 deletions(-) create mode 100644 internal/info/path.go create mode 100644 internal/info/path_test.go create mode 100644 internal/info/version.go delete mode 100644 internal/path.go delete mode 100644 internal/path_test.go delete mode 100644 internal/version.go diff --git a/cmd/hakurei/command.go b/cmd/hakurei/command.go index 94fe7ead..2b0568e2 100644 --- a/cmd/hakurei/command.go +++ b/cmd/hakurei/command.go @@ -17,8 +17,8 @@ import ( "hakurei.app/container/check" "hakurei.app/container/fhs" "hakurei.app/hst" - "hakurei.app/internal" "hakurei.app/internal/env" + "hakurei.app/internal/info" "hakurei.app/internal/outcome" "hakurei.app/internal/system/dbus" "hakurei.app/message" @@ -353,7 +353,7 @@ func buildCommand(ctx context.Context, msg message.Msg, early *earlyHardeningErr }).Flag(&flagShort, "short", command.BoolFlag(false), "Print instance id") } - c.Command("version", "Display version information", func(args []string) error { fmt.Println(internal.Version()); return errSuccess }) + c.Command("version", "Display version information", func(args []string) error { fmt.Println(info.Version()); return errSuccess }) c.Command("license", "Show full license text", func(args []string) error { fmt.Println(license); return errSuccess }) c.Command("template", "Produce a config template", func(args []string) error { encodeJSON(log.Fatal, os.Stdout, false, hst.Template()); return errSuccess }) c.Command("help", "Show this help message", func([]string) error { c.PrintHelp(); return errSuccess }) diff --git a/cmd/hakurei/print.go b/cmd/hakurei/print.go index 51358585..62131dbe 100644 --- a/cmd/hakurei/print.go +++ b/cmd/hakurei/print.go @@ -12,8 +12,8 @@ import ( "time" "hakurei.app/hst" - "hakurei.app/internal" "hakurei.app/internal/env" + "hakurei.app/internal/info" "hakurei.app/internal/outcome" "hakurei.app/internal/store" "hakurei.app/message" @@ -24,20 +24,20 @@ func printShowSystem(output io.Writer, short, flagJSON bool) { t := newPrinter(output) defer t.MustFlush() - info := &hst.Info{Version: internal.Version(), User: new(outcome.Hsu).MustID(nil)} - env.CopyPaths().Copy(&info.Paths, info.User) + hi := &hst.Info{Version: info.Version(), User: new(outcome.Hsu).MustID(nil)} + env.CopyPaths().Copy(&hi.Paths, hi.User) if flagJSON { - encodeJSON(log.Fatal, output, short, info) + encodeJSON(log.Fatal, output, short, hi) return } - t.Printf("Version:\t%s\n", info.Version) - t.Printf("User:\t%d\n", info.User) - t.Printf("TempDir:\t%s\n", info.TempDir) - t.Printf("SharePath:\t%s\n", info.SharePath) - t.Printf("RuntimePath:\t%s\n", info.RuntimePath) - t.Printf("RunDirPath:\t%s\n", info.RunDirPath) + t.Printf("Version:\t%s\n", hi.Version) + t.Printf("User:\t%d\n", hi.User) + t.Printf("TempDir:\t%s\n", hi.TempDir) + t.Printf("SharePath:\t%s\n", hi.SharePath) + t.Printf("RuntimePath:\t%s\n", hi.RuntimePath) + t.Printf("RunDirPath:\t%s\n", hi.RunDirPath) } // printShowInstance writes a representation of [hst.State] or [hst.Config] to output. diff --git a/cmd/hpkg/proc.go b/cmd/hpkg/proc.go index 013e0b69..aca2db42 100644 --- a/cmd/hpkg/proc.go +++ b/cmd/hpkg/proc.go @@ -10,11 +10,11 @@ import ( "os/exec" "hakurei.app/hst" - "hakurei.app/internal" + "hakurei.app/internal/info" "hakurei.app/message" ) -var hakureiPathVal = internal.MustHakureiPath().String() +var hakureiPathVal = info.MustHakureiPath().String() func mustRunApp(ctx context.Context, msg message.Msg, config *hst.Config, beforeFail func()) { var ( diff --git a/dist/release.sh b/dist/release.sh index 47b6fd01..4dcb278e 100755 --- a/dist/release.sh +++ b/dist/release.sh @@ -10,9 +10,9 @@ cp -rv "dist/comp" "${out}" go generate ./... go build -trimpath -v -o "${out}/bin/" -ldflags "-s -w -buildid= -extldflags '-static' - -X hakurei.app/internal.buildVersion=${VERSION} - -X hakurei.app/internal.hakureiPath=/usr/bin/hakurei - -X hakurei.app/internal.hsuPath=/usr/bin/hsu + -X hakurei.app/internal/info.buildVersion=${VERSION} + -X hakurei.app/internal/info.hakureiPath=/usr/bin/hakurei + -X hakurei.app/internal/info.hsuPath=/usr/bin/hsu -X main.hakureiPath=/usr/bin/hakurei" ./... rm -f "./${out}.tar.gz" && tar -C dist -czf "${out}.tar.gz" "${pname}" diff --git a/internal/info/path.go b/internal/info/path.go new file mode 100644 index 00000000..9f96734a --- /dev/null +++ b/internal/info/path.go @@ -0,0 +1,33 @@ +package info + +import ( + "log" + + "hakurei.app/container/check" +) + +// Absolute paths to the Hakurei installation. +// +// These are set by the linker. +var hakureiPath, hsuPath string + +// MustHakureiPath returns the [check.Absolute] path to hakurei. +func MustHakureiPath() *check.Absolute { return mustCheckPath(log.Fatal, "hakurei", hakureiPath) } + +// MustHsuPath returns the [check.Absolute] to hsu. +func MustHsuPath() *check.Absolute { return mustCheckPath(log.Fatal, "hsu", hsuPath) } + +// mustCheckPath checks a pathname to not be zero, then [check.NewAbs], calling fatal if either step fails. +func mustCheckPath(fatal func(v ...any), name, pathname string) *check.Absolute { + if pathname != "" { + if a, err := check.NewAbs(pathname); err != nil { + fatal(err.Error()) + return nil // unreachable + } else { + return a + } + } else { + fatal("invalid " + name + " path, this program is compiled incorrectly") + return nil // unreachable + } +} diff --git a/internal/info/path_test.go b/internal/info/path_test.go new file mode 100644 index 00000000..3137ee6a --- /dev/null +++ b/internal/info/path_test.go @@ -0,0 +1,45 @@ +package info + +import ( + "reflect" + "testing" + + "hakurei.app/container/check" +) + +func TestMustCheckPath(t *testing.T) { + t.Parallel() + + testCases := []struct { + name string + pathname string + wantFatal string + }{ + {"zero", "", "invalid test path, this program is compiled incorrectly"}, + {"not absolute", "\x00", `path "\x00" is not absolute`}, + {"success", "/proc/nonexistent", ""}, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + fatal := func(v ...any) { t.Fatal(append([]any{"invalid call to fatal:"}, v...)...) } + if tc.wantFatal != "" { + fatal = func(v ...any) { + if len(v) != 1 { + t.Errorf("mustCheckPath: fatal %#v", v) + } else if gotFatal, ok := v[0].(string); !ok { + t.Errorf("mustCheckPath: fatal = %#v", v[0]) + } else if gotFatal != tc.wantFatal { + t.Errorf("mustCheckPath: fatal = %q, want %q", gotFatal, tc.wantFatal) + } + + // do not simulate exit + } + } + + if got := mustCheckPath(fatal, "test", tc.pathname); got != nil && !reflect.DeepEqual(got, check.MustAbs(tc.pathname)) { + t.Errorf("mustCheckPath: %q", got) + } + }) + } +} diff --git a/internal/info/version.go b/internal/info/version.go new file mode 100644 index 00000000..8e722f78 --- /dev/null +++ b/internal/info/version.go @@ -0,0 +1,19 @@ +package info + +// FallbackVersion is returned when a version string was not set by the linker. +const FallbackVersion = "dirty" + +// buildVersion is the Hakurei tree's version string at build time. +// +// This is set by the linker. +var buildVersion string + +// Version returns the Hakurei tree's version string. +// It is either the value of the constant [FallbackVersion] or, +// when possible, a release tag like "v1.0.0". +func Version() string { + if buildVersion != "" { + return buildVersion + } + return FallbackVersion +} diff --git a/internal/outcome/dispatcher.go b/internal/outcome/dispatcher.go index e2feb373..20c70a4b 100644 --- a/internal/outcome/dispatcher.go +++ b/internal/outcome/dispatcher.go @@ -14,7 +14,7 @@ import ( "hakurei.app/container/check" "hakurei.app/container/seccomp" "hakurei.app/container/std" - "hakurei.app/internal" + "hakurei.app/internal/info" "hakurei.app/internal/system/dbus" "hakurei.app/message" ) @@ -156,7 +156,7 @@ func (direct) seccompLoad(rules []std.NativeRule, flags seccomp.ExportFlag) erro return seccomp.Load(rules, flags) } -func (direct) mustHsuPath() *check.Absolute { return internal.MustHsuPath() } +func (direct) mustHsuPath() *check.Absolute { return info.MustHsuPath() } func (direct) dbusAddress() (session, system string) { return dbus.Address() } diff --git a/internal/outcome/process.go b/internal/outcome/process.go index 537235fd..e7b7113d 100644 --- a/internal/outcome/process.go +++ b/internal/outcome/process.go @@ -16,7 +16,7 @@ import ( "hakurei.app/container/check" "hakurei.app/container/fhs" "hakurei.app/hst" - "hakurei.app/internal" + "hakurei.app/internal/info" "hakurei.app/internal/store" "hakurei.app/internal/system" "hakurei.app/message" @@ -39,7 +39,7 @@ func (k *outcome) main(msg message.Msg, identifierFd int) { } // read comp value early for early failure - hsuPath := internal.MustHsuPath() + hsuPath := info.MustHsuPath() const ( // transitions to processCommit, or processFinal on failure diff --git a/internal/path.go b/internal/path.go deleted file mode 100644 index 85e93eaf..00000000 --- a/internal/path.go +++ /dev/null @@ -1,33 +0,0 @@ -package internal - -import ( - "log" - - "hakurei.app/container/check" -) - -// Absolute paths to the Hakurei installation. -// -// These are set by the linker. -var hakureiPath, hsuPath string - -// MustHakureiPath returns the [check.Absolute] path to hakurei. -func MustHakureiPath() *check.Absolute { return mustCheckPath(log.Fatal, "hakurei", hakureiPath) } - -// MustHsuPath returns the [check.Absolute] to hsu. -func MustHsuPath() *check.Absolute { return mustCheckPath(log.Fatal, "hsu", hsuPath) } - -// mustCheckPath checks a pathname to not be zero, then [check.NewAbs], calling fatal if either step fails. -func mustCheckPath(fatal func(v ...any), name, pathname string) *check.Absolute { - if pathname != "" { - if a, err := check.NewAbs(pathname); err != nil { - fatal(err.Error()) - return nil // unreachable - } else { - return a - } - } else { - fatal("invalid " + name + " path, this program is compiled incorrectly") - return nil // unreachable - } -} diff --git a/internal/path_test.go b/internal/path_test.go deleted file mode 100644 index f670c786..00000000 --- a/internal/path_test.go +++ /dev/null @@ -1,45 +0,0 @@ -package internal - -import ( - "reflect" - "testing" - - "hakurei.app/container/check" -) - -func TestMustCheckPath(t *testing.T) { - t.Parallel() - - testCases := []struct { - name string - pathname string - wantFatal string - }{ - {"zero", "", "invalid test path, this program is compiled incorrectly"}, - {"not absolute", "\x00", `path "\x00" is not absolute`}, - {"success", "/proc/nonexistent", ""}, - } - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - fatal := func(v ...any) { t.Fatal(append([]any{"invalid call to fatal:"}, v...)...) } - if tc.wantFatal != "" { - fatal = func(v ...any) { - if len(v) != 1 { - t.Errorf("mustCheckPath: fatal %#v", v) - } else if gotFatal, ok := v[0].(string); !ok { - t.Errorf("mustCheckPath: fatal = %#v", v[0]) - } else if gotFatal != tc.wantFatal { - t.Errorf("mustCheckPath: fatal = %q, want %q", gotFatal, tc.wantFatal) - } - - // do not simulate exit - } - } - - if got := mustCheckPath(fatal, "test", tc.pathname); got != nil && !reflect.DeepEqual(got, check.MustAbs(tc.pathname)) { - t.Errorf("mustCheckPath: %q", got) - } - }) - } -} diff --git a/internal/version.go b/internal/version.go deleted file mode 100644 index 5c671084..00000000 --- a/internal/version.go +++ /dev/null @@ -1,19 +0,0 @@ -package internal - -// FallbackVersion is returned when a version string was not set by the linker. -const FallbackVersion = "dirty" - -// buildVersion is the Hakurei tree's version string at build time. -// -// This is set by the linker. -var buildVersion string - -// Version returns the Hakurei tree's version string. -// It is either the value of the constant [FallbackVersion] or, -// when possible, a release tag like "v1.0.0". -func Version() string { - if buildVersion != "" { - return buildVersion - } - return FallbackVersion -} diff --git a/package.nix b/package.nix index 16b769e3..b8518a94 100644 --- a/package.nix +++ b/package.nix @@ -65,7 +65,7 @@ buildGoModule rec { lib.attrsets.foldlAttrs ( ldflags: name: value: - ldflags ++ [ "-X hakurei.app/internal.${name}=${value}" ] + ldflags ++ [ "-X hakurei.app/internal/info.${name}=${value}" ] ) ( [ "-s -w" ] -- cgit v1.3.1