Documentation ¶
Overview ¶
Package gocore implements functionality used by commands. Functions support
- a command line framework
- extensions to the Go flag package
- help for command syntax
- enhanced logging
The gocore package defines the following command line flags:
- -version: to report the current version of the command
- -cpuprofile: profile CPU performance of command
- -memprofile: profile memory usage of command
Index ¶
- Constants
- Variables
- func CamelCase(s string) string
- func Capitalize(s string) string
- func ChDir(dir string) (string, error)
- func FdPath(fd int) (string, error)
- func Format(name, tag string, val reflect.Value, fn Formatter) (ms []any)
- func GoString[C int8 | byte](char *C) string
- func GoStringN[C int8 | byte, ...](char *C, l L) string
- func Groupname(gid int) string
- func Hostname(addr string) string
- func IsTerminal(f *os.File) bool
- func Main(main func(context.Context) error)
- func Measures(filename string) (map[string]string, error)
- func Module(dir string) modval
- func MountMap() (map[string]string, error)
- func MsToTime(ms uint64) time.Time
- func Seteuid()
- func Setuid()
- func SnakeCase(s string) string
- func Spawn(ctx context.Context, cmdline []string) (*bufio.Scanner, error)
- func Subdir(base, targ string) (string, error)
- func Uncapitalize(s string) string
- func Unsupported() error
- func Username(uid int) string
- type Comparator
- type Display
- type Formatter
- type LogLevel
- type LogMessage
- type Node
- type Order
- type Regexp
- type Table
- type Tree
- type Utsname
- type ValidValue
Constants ¶
const (
// RFC3339Milli used for formatting timestamps.
RFC3339Milli = "2006-01-02T15:04:05.000Z07:00"
)
Variables ¶
var (
// Host identifies the local host.
Host, _ = os.Hostname()
// Platform identifies the local OS.
Platform = runtime.GOOS + "_" + runtime.GOARCH
Uname = func() Utsname {
var utsname unix.Utsname
if err := unix.Uname(&utsname); err == nil {
return Utsname{
Sysname: C.GoString((*C.char)(unsafe.Pointer(&utsname.Sysname[0]))),
Nodename: C.GoString((*C.char)(unsafe.Pointer(&utsname.Nodename[0]))),
Release: C.GoString((*C.char)(unsafe.Pointer(&utsname.Release[0]))),
Version: C.GoString((*C.char)(unsafe.Pointer(&utsname.Version[0]))),
Machine: C.GoString((*C.char)(unsafe.Pointer(&utsname.Machine[0]))),
}
}
return Utsname{}
}()
// executable identifies the full command path.
Executable, _ = os.Executable()
// Version of module: version.major.minor-timestamp-commithash
Version string
)
var ( // loggingLevel maps requested LOG_LEVEL to index. LoggingLevel = func() LogLevel { switch strings.ToUpper(os.Getenv("LOG_LEVEL")) { case "TRACE": return LevelTrace case "DEBUG": return LevelDebug case "WARN": return LevelWarn case "ERROR": return LevelError case "FATAL": return LevelFatal } return LevelInfo }() // Log is the default log message formatter and writer. Log = func(msg LogMessage, level LogLevel) { if level >= LoggingLevel { if msg.E == nil && level > LevelInfo { level = LevelInfo } log.Printf("%s %-5s %s", time.Now().Format(RFC3339Milli), logLevels[level], msg.Error()) } } )
var ( // Boottime gets the system boot time. Boottime = func() time.Time { f, err := os.Open("/proc/stat") if err != nil { Error("/proc/stat open", err).Err() return time.Time{} } defer f.Close() sc := bufio.NewScanner(f) for sc.Scan() { l := sc.Text() k, v, _ := strings.Cut(l, " ") switch k { case "btime": sec, err := strconv.Atoi(v) if err != nil { Error("/proc/stat btime", err).Err() return time.Time{} } return time.Unix(int64(sec), 0) } } Error("/proc/stat btime", sc.Err()).Err() return time.Time{} }() )
var ( // DarkAppearance indicates whether system appearance is "dark" or "light" DarkAppearance bool )
var ( // Flags defines and initializes the command line flags Flags = flags{ FlagSet: flag.FlagSet{}, CommandDescription: "", ArgumentDescriptions: [][2]string{}, // contains filtered or unexported fields } )
Functions ¶
func Capitalize ¶
Capitalize uppercases the leading character of a name.
func ChDir ¶
ChDir is a convenience function for changing the current directory and reporting its canonical path. If changing the directory fails, ChDir returns the error and canonical path of the current directory.
func GoStringN ¶
func GoStringN[ C int8 | byte, L int | uint | int8 | uint8 | int16 | uint16 | int32 | uint32 | int64 | uint64, ](char *C, l L) string
GoStringN interprets a length specified and possibly null terminated C char array as a GO string.
func IsTerminal ¶
IsTerminal reports if a file handle is connected to the terminal.
func SnakeCase ¶
SnakeCase converts a CamelCase name to snake_case to follow JSON and data base naming conventions.
func Subdir ¶
Subdir acts like filepath.Rel() but returns an error if the target path is not on the base path.
func Uncapitalize ¶
Uncapitalize lowercases the leading character of a name.
func Unsupported ¶
func Unsupported() error
Unsupported reports that a specific OS does not support a function
Types ¶
type Comparator ¶
Comparator is a value returned by the Order function of a Table for sorting.
type Formatter ¶
Formatter function prototype for functions that encode values for the Format function.
type LogMessage ¶
LogMessage is custom logging error type.
func Error ¶
func Error(source string, err error, details ...map[string]string) LogMessage
Error records the function source, error message, code location, and any details of initial error, preserving the initial error for percolation.
func (LogMessage) Error ¶
func (msg LogMessage) Error() string
Error method to comply with error interface.
func (LogMessage) Info ¶
func (msg LogMessage) Info()
Info log info message (default logging level).
func (LogMessage) Unwrap ¶
func (msg LogMessage) Unwrap() error
Unwrap method to comply with error interface.
type Order ¶
type Order[N Node, C Comparator, V any] func(N, Table[N, V]) C
Order distinguishes similar Node values for sorting.
type Regexp ¶
Regexp is a command line flag type.
type Tree ¶
type Tree[N Node, C Comparator, V any] map[N]Tree[N, C, V]
Tree defines a hierarchy of Nodes of comparable type.
func (Tree[N, C, V]) FindTree ¶
func (tr Tree[N, C, V]) FindTree(node N) Tree[N, C, V]
FindTree finds the subtree anchored by a specific node.
type ValidValue ¶
ValidValue defines list of values that are valid for a type safe string.
func (ValidValue[T]) Define ¶
func (vv ValidValue[T]) Define(values ...T) ValidValue[T]
Define initializes a ValidValue type with its valid values.
func (ValidValue[T]) Index ¶
func (vv ValidValue[T]) Index(v T) int
Index returns the position of a value in the valid value list.
func (ValidValue[T]) IsValid ¶
func (vv ValidValue[T]) IsValid(v T) bool
IsValid returns whether a value is valid.
func (ValidValue[T]) ValidValues ¶
func (vv ValidValue[T]) ValidValues() []string
ValidValues returns an ordered list of valid values for the type.