* bugfix: use os.IsNotExist() instead of comparing errors for equality; this fixes incorrect handling of missing authorized_keys file. * move die() and warn() into die.go - and make them public functions. * teach die.go to also provide atexit() like functionality * teach all callers of sign.SafeFile{} to use AtExit() to delete temporary artifacts * symbol renaming: die->Die, warn->Warn.
55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
// die.go -- die() and warn()
|
|
//
|
|
// (c) 2016 Sudhi Herle <sudhi@herle.net>
|
|
//
|
|
// Licensing Terms: GPLv2
|
|
//
|
|
// If you need a commercial license for this work, please contact
|
|
// the author.
|
|
//
|
|
// This software does not come with any express or implied
|
|
// warranty; it is provided "as is". No claim is made to its
|
|
// suitability for any purpose.
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
var atExit []func()
|
|
|
|
// Die prints an error message to stderr
|
|
// and exits the program after calling all the registered
|
|
// at-exit functions.
|
|
func Die(f string, v ...interface{}) {
|
|
Warn(f, v...)
|
|
Exit(1)
|
|
}
|
|
|
|
// Warn prints an error message to stderr
|
|
func Warn(f string, v ...interface{}) {
|
|
z := fmt.Sprintf("%s: %s", os.Args[0], f)
|
|
s := fmt.Sprintf(z, v...)
|
|
if n := len(s); s[n-1] != '\n' {
|
|
s += "\n"
|
|
}
|
|
|
|
os.Stderr.WriteString(s)
|
|
os.Stderr.Sync()
|
|
}
|
|
|
|
// AtExit registers a function to be called before the program exits.
|
|
func AtExit(f func()) {
|
|
atExit = append(atExit, f)
|
|
}
|
|
|
|
// Exit invokes the registered atexit handlers and exits with the
|
|
// given code.
|
|
func Exit(v int) {
|
|
for _, f := range atExit {
|
|
f()
|
|
}
|
|
os.Exit(v)
|
|
}
|