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, ) }