summaryrefslogtreecommitdiff
path: root/spool
diff options
context:
space:
mode:
Diffstat (limited to 'spool')
-rw-r--r--spool/exit.go60
-rw-r--r--spool/spool.go23
2 files changed, 83 insertions, 0 deletions
diff --git a/spool/exit.go b/spool/exit.go
new file mode 100644
index 0000000..efe9d4e
--- /dev/null
+++ b/spool/exit.go
@@ -0,0 +1,60 @@
+package spool
+
+import (
+ "github.com/ewy1/pik/paths"
+ "os"
+)
+
+type ExitCode struct {
+ Value int
+ Message string
+}
+
+var (
+ Success = New(0, "succesful")
+ Cancelled = New(0, "operation cancelled by user")
+ NotFoundFailure = Cancelled.Next("target not found")
+ FatalReadFailure = NotFoundFailure.Next("fatal failure during initialization")
+ FatalWriteFailure = FatalReadFailure.Next("fatal failure during file write")
+ WorkingDirectoryFailure = FatalWriteFailure.Next("could not get current working directory")
+ OpenRootFailure = WorkingDirectoryFailure.Next("failed to init root directory")
+ RootFsFailure = OpenRootFailure.Next("failed to make fs from root")
+ CacheReadFailure = RootFsFailure.Next("failed to read cache (from " + paths.ContextsFile.String())
+ HydrationFailure = CacheReadFailure.Next("a hydrator failed")
+ MenuFailure = HydrationFailure.Next("error during menu")
+ NoTargetsFailure = MenuFailure.Next("no targets found")
+ NoEditorFailure = NoTargetsFailure.Next("$EDITOR not set")
+ NoDebugInfo = NoEditorFailure.Next("could not read debug info")
+ ManFailure = New(120, "failure to generate manual pages")
+ UnknownShellFailure = New(110, "unable to detect shell type through $SHELL")
+ CompletionAlreadyInstalledFailure = UnknownShellFailure.Next("completion seems already installed")
+ CompletionFailure = CompletionAlreadyInstalledFailure.Next("failed to install completion")
+ ProfilingFailure = CompletionFailure.Next("failed to initialize profiler")
+)
+
+var Codes []ExitCode
+var CodeMap = make(map[int]*ExitCode)
+
+func New(num int, message string) ExitCode {
+ if CodeMap[num] != nil && num != 0 {
+ _, _ = Warn("redefined error code: %v", num)
+ }
+ c := ExitCode{
+ Value: num,
+ Message: message,
+ }
+ Codes = append(Codes, c)
+ CodeMap[num] = &c
+ return c
+}
+
+func (e ExitCode) Exit() {
+ os.Exit(e.Value)
+}
+
+func (e ExitCode) Next(message string) ExitCode {
+ return New(
+ e.Value+1,
+ message,
+ )
+}
diff --git a/spool/spool.go b/spool/spool.go
index e7b13e9..4335d50 100644
--- a/spool/spool.go
+++ b/spool/spool.go
@@ -3,6 +3,9 @@ package spool
import (
"fmt"
"os"
+ "reflect"
+ "runtime/debug"
+ "strings"
)
var (
@@ -10,6 +13,10 @@ var (
Stdout = os.Stdout
)
+type empty struct{}
+
+const PanicErrorCode = 8
+
var Print = func(format string, values ...any) (any, error) {
return fmt.Fprintf(Stdout, format, values...)
}
@@ -17,3 +24,19 @@ var Print = func(format string, values ...any) (any, error) {
var Warn = func(format string, values ...any) (any, error) {
return fmt.Fprintf(Stderr, format, values...)
}
+
+var Panic = func(code ExitCode, format string, values ...any) (any, error) {
+ pkg := reflect.TypeOf(empty{}).PkgPath()
+ v, err := fmt.Fprintf(Stderr, format, values...)
+ st := strings.Split(string(debug.Stack()), "\n")
+ for i, l := range st {
+ if strings.Contains(l, pkg) {
+ st = st[i:]
+ break
+ }
+ }
+
+ _, _ = Warn("%s\n", strings.Join(st, "\n"))
+ os.Exit(PanicErrorCode)
+ return v, err
+}