2015-09-22 10:27:14 +00:00
|
|
|
// Package lib contains functions useful for most programs.
|
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
var progname = filepath.Base(os.Args[0])
|
|
|
|
|
2015-10-26 23:37:44 +00:00
|
|
|
// ProgName returns what lib thinks the program name is, namely the
|
|
|
|
// basename of of argv0.
|
2015-12-23 21:23:59 +00:00
|
|
|
//
|
|
|
|
// It is similar to the Linux __progname.
|
2015-10-26 23:37:44 +00:00
|
|
|
func ProgName() string {
|
|
|
|
return progname
|
|
|
|
}
|
|
|
|
|
2015-09-22 10:27:14 +00:00
|
|
|
// Warnx displays a formatted error message to standard error, à la
|
|
|
|
// warnx(3).
|
|
|
|
func Warnx(format string, a ...interface{}) (int, error) {
|
|
|
|
format = fmt.Sprintf("[%s] %s", progname, format)
|
|
|
|
format += "\n"
|
|
|
|
return fmt.Fprintf(os.Stderr, format, a...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warn displays a formatted error message to standard output,
|
|
|
|
// appending the error string, à la warn(3).
|
|
|
|
func Warn(err error, format string, a ...interface{}) (int, error) {
|
|
|
|
format = fmt.Sprintf("[%s] %s", progname, format)
|
|
|
|
format += ": %v\n"
|
|
|
|
a = append(a, err)
|
|
|
|
return fmt.Fprintf(os.Stderr, format, a...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errx displays a formatted error message to standard error and exits
|
|
|
|
// with the status code from `exit`, à la errx(3).
|
|
|
|
func Errx(exit int, format string, a ...interface{}) {
|
|
|
|
format = fmt.Sprintf("[%s] %s", progname, format)
|
|
|
|
format += "\n"
|
|
|
|
fmt.Fprintf(os.Stderr, format, a...)
|
|
|
|
os.Exit(exit)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Err displays a formatting error message to standard error,
|
|
|
|
// appending the error string, and exits with the status code from
|
|
|
|
// `exit`, à la err(3).
|
|
|
|
func Err(exit int, err error, format string, a ...interface{}) {
|
|
|
|
format = fmt.Sprintf("[%s] %s", progname, format)
|
|
|
|
format += ": %v\n"
|
|
|
|
a = append(a, err)
|
|
|
|
fmt.Fprintf(os.Stderr, format, a...)
|
|
|
|
os.Exit(exit)
|
|
|
|
}
|
2015-09-22 18:25:54 +00:00
|
|
|
|
|
|
|
// CheckFatal calls Err if err isn't nil.
|
|
|
|
func CheckFatal(err error, format string, a ...interface{}) {
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(ExitFailure, err, format, a...)
|
|
|
|
}
|
2015-09-23 05:58:58 +00:00
|
|
|
|
|
|
|
// Itoa provides cheap integer to fixed-width decimal ASCII. Give a
|
|
|
|
// negative width to avoid zero-padding. Adapted from the 'itoa'
|
|
|
|
// function in the log/log.go file in the standard library.
|
|
|
|
func Itoa(i int, wid int) string {
|
|
|
|
// Assemble decimal in reverse order.
|
|
|
|
var b [20]byte
|
|
|
|
bp := len(b) - 1
|
|
|
|
for i >= 10 || wid > 1 {
|
|
|
|
wid--
|
|
|
|
q := i / 10
|
|
|
|
b[bp] = byte('0' + i - q*10)
|
|
|
|
bp--
|
|
|
|
i = q
|
|
|
|
}
|
|
|
|
// i < 10
|
|
|
|
b[bp] = byte('0' + i)
|
|
|
|
return string(b[bp:])
|
|
|
|
}
|