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 --- 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 ------------------ 8 files changed, 101 insertions(+), 101 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 (limited to 'internal') 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 -} -- cgit v1.3.1