]> git.earlybird.gay Git - today/commitdiff
Move include to today
authorearly <me@earlybird.gay>
Sat, 2 Nov 2024 18:26:46 +0000 (12:26 -0600)
committerearly <me@earlybird.gay>
Sat, 2 Nov 2024 18:26:46 +0000 (12:26 -0600)
engine/include/callstack.go [deleted file]
engine/include/include.go [deleted file]
include/callstack.go [new file with mode: 0644]
include/include.go [new file with mode: 0644]

diff --git a/engine/include/callstack.go b/engine/include/callstack.go
deleted file mode 100644 (file)
index 15e4910..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-// Copyright (C) 2024 early (LGPL)
-package include
-
-import (
-       "errors"
-       "runtime"
-       "slices"
-       "strings"
-)
-
-var ErrNoEligibleCaller = errors.New("no eligible caller on the stack")
-var ErrNoRuntimeAccess = errors.New("couldn't access the runtime")
-
-var callStackNotEligible = []string{
-       "runtime",
-       "git.earlybird.gay/today-engine/include",
-}
-
-// SetNotEligible marks a package as ineligible for including files.
-// You should use the arguments of include to to this if you can, but if you are
-// using a package that passes through to include, you can call this in an init
-// function.
-func SetNotEligible(pkg string) {
-       callStackNotEligible = append(callStackNotEligible, pkg)
-}
-
-func isNotEligible(caller string, ignorePackages []string) bool {
-       return slices.ContainsFunc(callStackNotEligible, func(notEligible string) bool {
-               return strings.HasPrefix(caller, notEligible+".")
-       }) || slices.ContainsFunc(ignorePackages, func(notEligible string) bool {
-               return strings.HasPrefix(caller, notEligible+".")
-       })
-}
-
-// getCallStackButt gets the calling file, ignoring any file in an ignored
-// package.
-func getCallStackButt(ignorePackages []string) (string, error) {
-       const incr int = 2
-       const max int = incr * 10
-       for skip := 0; skip < max; skip += incr {
-               callers := make([]uintptr, incr)
-               count := runtime.Callers(skip, callers)
-               frames := runtime.CallersFrames(callers)
-
-               frame, more := frames.Next()
-               for {
-                       // If getCallStackButt gets called from main, use the runtime to
-                       // determine what module main is in.
-                       if isNotEligible(frame.Function, ignorePackages) {
-                               if !more {
-                                       break
-                               }
-                               frame, more = frames.Next()
-                       } else {
-                               return frame.File, nil
-                       }
-               }
-               if count < incr {
-                       break
-               }
-       }
-
-       return "", ErrNoEligibleCaller
-}
diff --git a/engine/include/include.go b/engine/include/include.go
deleted file mode 100644 (file)
index c2ee4fd..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-// Copyright (C) 2024 early (LGPL)
-package include
-
-import (
-       "io"
-       "os"
-       "path"
-)
-
-type Opener interface {
-       Open() (io.ReadCloser, error)
-}
-
-type FileOpener interface {
-       Opener
-       FileName() string
-}
-
-type OpenerFunc func() (io.ReadCloser, error)
-
-func (opener OpenerFunc) Open() (io.ReadCloser, error) {
-       return opener()
-}
-
-type fileOpener struct {
-       absPath   string
-       alwaysErr error
-}
-
-func (opener *fileOpener) Open() (io.ReadCloser, error) {
-       if opener.alwaysErr != nil {
-               return nil, opener.alwaysErr
-       }
-       return os.Open(opener.absPath)
-}
-
-func (fopener *fileOpener) FileName() string {
-       return fopener.absPath
-}
-
-// File returns an Opener that Opens() a file.
-// If filename is a relative path, it is considered relative to the *calling
-// file*, not the working directory.
-// If ignorePackages is nonempty, callers in any package specified are ignored.
-func File(filename string, ignorePackages ...string) FileOpener {
-       opener := new(fileOpener)
-       if path.IsAbs(filename) {
-               opener.absPath = filename
-       } else {
-               caller, err := getCallStackButt(ignorePackages)
-               if err != nil {
-                       opener.alwaysErr = err
-               } else {
-                       opener.absPath = path.Join(path.Dir(caller), filename)
-               }
-       }
-       return opener
-}
diff --git a/include/callstack.go b/include/callstack.go
new file mode 100644 (file)
index 0000000..94f53b2
--- /dev/null
@@ -0,0 +1,64 @@
+// Copyright (C) 2024 early (LGPL)
+package include
+
+import (
+       "errors"
+       "runtime"
+       "slices"
+       "strings"
+)
+
+var ErrNoEligibleCaller = errors.New("no eligible caller on the stack")
+var ErrNoRuntimeAccess = errors.New("couldn't access the runtime")
+
+var callStackNotEligible = []string{
+       "runtime",
+       "git.earlybird.gay/today/include",
+}
+
+// SetNotEligible marks a package as ineligible for including files.
+// You should use the arguments of include to to this if you can, but if you are
+// using a package that passes through to include, you can call this in an init
+// function.
+func SetNotEligible(pkg string) {
+       callStackNotEligible = append(callStackNotEligible, pkg)
+}
+
+func isNotEligible(caller string, ignorePackages []string) bool {
+       return slices.ContainsFunc(callStackNotEligible, func(notEligible string) bool {
+               return strings.HasPrefix(caller, notEligible+".")
+       }) || slices.ContainsFunc(ignorePackages, func(notEligible string) bool {
+               return strings.HasPrefix(caller, notEligible+".")
+       })
+}
+
+// getCallStackButt gets the calling file, ignoring any file in an ignored
+// package.
+func getCallStackButt(ignorePackages []string) (string, error) {
+       const incr int = 2
+       const max int = incr * 10
+       for skip := 0; skip < max; skip += incr {
+               callers := make([]uintptr, incr)
+               count := runtime.Callers(skip, callers)
+               frames := runtime.CallersFrames(callers)
+
+               frame, more := frames.Next()
+               for {
+                       // If getCallStackButt gets called from main, use the runtime to
+                       // determine what module main is in.
+                       if isNotEligible(frame.Function, ignorePackages) {
+                               if !more {
+                                       break
+                               }
+                               frame, more = frames.Next()
+                       } else {
+                               return frame.File, nil
+                       }
+               }
+               if count < incr {
+                       break
+               }
+       }
+
+       return "", ErrNoEligibleCaller
+}
diff --git a/include/include.go b/include/include.go
new file mode 100644 (file)
index 0000000..800fc2c
--- /dev/null
@@ -0,0 +1,71 @@
+// Copyright (C) 2024 early (LGPL)
+package include
+
+import (
+       "io"
+       "os"
+       "path"
+)
+
+type Opener interface {
+       Open() (io.ReadCloser, error)
+}
+
+type FileOpener interface {
+       Opener
+       FileName() string
+}
+
+type OpenerFunc func() (io.ReadCloser, error)
+
+func (opener OpenerFunc) Open() (io.ReadCloser, error) {
+       return opener()
+}
+
+type fileOpener struct {
+       absPath   string
+       alwaysErr error
+}
+
+func (opener *fileOpener) Open() (io.ReadCloser, error) {
+       if opener.alwaysErr != nil {
+               return nil, opener.alwaysErr
+       }
+       return os.Open(opener.absPath)
+}
+
+func (fopener *fileOpener) FileName() string {
+       return fopener.absPath
+}
+
+func Abs(filename string, ignorePackages ...string) (string, error) {
+       if path.IsAbs(filename) {
+               return filename, nil
+       } else {
+               caller, err := getCallStackButt(ignorePackages)
+               if err != nil {
+                       return "", err
+               } else {
+                       return path.Join(path.Dir(caller), filename), nil
+               }
+       }
+}
+
+// File returns an Opener that Opens() a file.
+// If filename is a relative path, it is considered relative to the *calling
+// file*, not the working directory.
+// If ignorePackages is nonempty, callers in any package specified are ignored.
+func File(filename string, ignorePackages ...string) FileOpener {
+       opener := new(fileOpener)
+       if path.IsAbs(filename) {
+               opener.absPath = filename
+       } else {
+               caller, err := getCallStackButt(ignorePackages)
+               if err != nil {
+                       opener.alwaysErr = err
+               } else {
+                       opener.absPath = path.Join(path.Dir(caller), filename)
+               }
+       }
+       return opener
+}