Documentation ¶
Overview ¶
Package utils with functions to get the outbound network interface
Index ¶
- Constants
- Variables
- func Clone(b []byte) (c []byte)
- func ExitOnSignal(release func()) context.Context
- func FormatMSE(mse int64, short bool) string
- func GetInterfaces(address string) ([]net.Interface, error)
- func GetOutboundIP(destination string) net.IP
- func GetOutboundInterface(address string) (interfaceName string, macAddress string, ipAddr net.IP)
- func MapKeys[K comparable, V any](mm map[K]V) []K
- func OrderedMapKeys[K constraints.Ordered, V any](mm map[K]V) []K
- func WaitForSignal(ctx context.Context)
- type AppEnvironment
Constants ¶
const ( COReset = "\033[0m" CORed = "\033[31m" COGreen = "\033[32m" COYellow = "\033[33m" COBlue = "\033[34m" COPurple = "\033[35m" COCyan = "\033[36m" COGray = "\033[37m" COWhite = "\033[97m" )
Console ASCII commands to control colors See also: https://www.codequoi.com/en/coloring-terminal-text-tput-and-ansi-escape-sequences/
const ( CBBlack = "\033[40m" CBRed = "\033[41m" CBGreen = "\033[42m" CBYellow = "\033[43m" CBBlue = "\033[44m" CBGray = "\033[47m" CBWhite = "\033[41m" )
const ( WrapOff = "\033[?7l" WrapOn = "\033[?7h" )
Variables ¶
var LongFormat = "2006-01-02 15:04:05.000 -0700"
var ShortFormat = time.StampMilli
Functions ¶
func Clone ¶
Clone the given byte array. Use this is the source holds a transient value and its buffer can be reused elsewhere. Eg, all capnp byte arrays Golang will eventually get a bytes.clone() method but it isn't in go-19
returns nil if b is nil
func ExitOnSignal ¶
ExitOnSignal starts a background process and closes the context when a SIGINT or SIGTERM is received.
If a release function is provided it is invoked before the function exits normally. If no release function is provided this ends the process with os.Exit(0). This returns a child context which is cancelled before the call to release.
func FormatMSE ¶
FormatMSE returns a human readable string of the time in millisec since epoc The short format is: StampMilli: Jan _2 15:04:05.000 (local time) The long format is: RFC1123Z: Mon, 02 Jan 2006 15:04:05 -07:00
func GetInterfaces ¶
GetInterfaces returns a list of active network interfaces excluding the loopback interface
address to only return the interface that serves the given IP address
func GetOutboundIP ¶
GetOutboundIP returns the default outbound IP address to reach the given hostname. This uses 1.1.1.1 as the default destination. TODO: use the default gateway address instead so this works without internet access.
Use a local hostname if a subnet other than the default one should be used. Use "" for the default route address
destination to reach or "" to use 1.1.1.1 (no connection will be established)
func GetOutboundInterface ¶
GetOutboundInterface Get preferred outbound network interface of this machine Credits: https://stackoverflow.com/questions/23558425/how-do-i-get-the-local-ip-address-in-go and https://qiita.com/shaching/items/4c2ee8fd2914cce8687c
func OrderedMapKeys ¶
func OrderedMapKeys[K constraints.Ordered, V any](mm map[K]V) []K
OrderedMapKeys returns an ordered slice of keys
func WaitForSignal ¶
WaitForSignal waits until a SIGINT or SIGTERM is received or the context is closed
Types ¶
type AppEnvironment ¶
type AppEnvironment struct { // Directories BinDir string `yaml:"binDir,omitempty"` // Application binary folder, eg launcher, cli, ... PluginsDir string `yaml:"pluginsDir,omitempty"` // Plugin folder HomeDir string `yaml:"homeDir,omitempty"` // Home folder, default this is the parent of bin, config, certs and logs ConfigDir string `yaml:"configDir,omitempty"` // Config folder with application and configuration files ConfigFile string `yaml:"configFile,omitempty"` // Application configuration file. Default is clientID.yaml CertsDir string `yaml:"certsDir,omitempty"` // Certificates and keys location LogsDir string `yaml:"logsDir,omitempty"` // Logging output LogLevel string `yaml:"logLevel,omitempty"` // logging level StoresDir string `yaml:"storesDir,omitempty"` // Root of the service stores // Server //Core string `yaml:"core"` // core to use, "nats" or "mqtt". empty for auto-detect ServerURL string `yaml:"serverURL,omitempty"` // server address // Credentials CaCert *x509.Certificate `yaml:"-"` // default cert if loaded ClientID string `yaml:"clientID"` // the clientID based on the application binary name KeyFile string `yaml:"keyFile"` // client's key pair file location TokenFile string `yaml:"tokenFile"` // client's auth token file location }
AppEnvironment holds the running environment naming conventions. Intended for services and plugins. This contains folder locations, CA certificate and application clientID
func GetAppEnvironment ¶
func GetAppEnvironment(homeDir string, withFlags bool) AppEnvironment
GetAppEnvironment returns the application environment including folders for use by the Hub services.
Optionally parse commandline flags:
-home alternative home directory. Default is the parent folder of the app binary -clientID alternative clientID. Default is the application binary name. -config alternative config directory. Default is home/certs -configFile alternative application config file. Default is {clientID}.yaml -loglevel debug, info, warning (default), error -server optional server URL or "" for auto-detect -core optional server core or "" for auto-detect
The default 'user based' structure is:
home |- bin Core binaries |- plugins Plugin binaries |- config Service configuration yaml files |- certs CA and service certificates |- logs Logging output |- run PID files and sockets |- stores |- {service} Store for service
The system based folder structure is used when launched from a path starting with /usr or /opt:
/opt/hiveot/bin Application binaries, cli and launcher /opt/hiveot/plugins Plugin binaries /etc/hiveot/conf.d Service configuration yaml files /etc/hiveot/certs CA and service certificates /var/log/hiveot Logging output /run/hiveot PID files and sockets /var/lib/hiveot/{service} Storage of service
This uses os.Args[0] application path to determine the home directory, which is the parent of the application binary. The default clientID is based on the binary name using os.Args[0].
homeDir to override the auto-detected or commandline paths. Use "" for defaults. withFlags parse the commandline flags for -home and -clientID
func (*AppEnvironment) LoadConfig ¶
func (env *AppEnvironment) LoadConfig(configFile string, cfg interface{}) error
LoadConfig loads the application .
configFile is the optional name. Use "" for default {clientID}.yaml
This returns an error if loading or parsing the config file fails. Returns nil if the config file doesn't exist or is loaded successfully.