summaryrefslogtreecommitdiff
path: root/spool/exit.go
blob: efe9d4e63aa0271558870d16653346f08f1c3530 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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,
	)
}