util

package
v0.0.0-...-2561dba Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 15, 2024 License: GPL-3.0 Imports: 14 Imported by: 19

Documentation

Overview

Package util contains utility functions that are specific to the resource engine.

Index

Constants

View Source
const (
	ModeUser  uint32 = 64
	ModeGroup uint32 = 8
	ModeOther uint32 = 1

	ModeRead  uint32 = 4
	ModeWrite uint32 = 2
	ModeExec  uint32 = 1

	ModeSetU   uint32 = 4
	ModeSetG   uint32 = 2
	ModeSticky uint32 = 1
)

Constant bytes for the who (u, g, or o) and the what (r, w, x, s, or t).

View Source
const (
	// DBusInterface is the dbus interface that contains genereal methods.
	DBusInterface = "org.freedesktop.DBus"
	// DBusAddMatch is the dbus method to receive a subset of dbus broadcast
	// signals.
	DBusAddMatch = DBusInterface + ".AddMatch"
	// DBusRemoveMatch is the dbus method to remove a previously defined
	// AddMatch rule.
	DBusRemoveMatch = DBusInterface + ".RemoveMatch"
	// DBusSystemd1Path is the base systemd1 path.
	DBusSystemd1Path = "/org/freedesktop/systemd1"
	// DBusSystemd1Iface is the base systemd1 interface.
	DBusSystemd1Iface = "org.freedesktop.systemd1"
	// DBusSystemd1ManagerIface is the systemd manager interface used for
	// interfacing with systemd units.
	DBusSystemd1ManagerIface = DBusSystemd1Iface + ".Manager"
	// DBusRestartUnit is the dbus method for restarting systemd units.
	DBusRestartUnit = DBusSystemd1ManagerIface + ".RestartUnit"
	// DBusStopUnit is the dbus method for stopping systemd units.
	DBusStopUnit = DBusSystemd1ManagerIface + ".StopUnit"
	// DBusSignalJobRemoved is the name of the dbus signal that produces a
	// message when a dbus job is done (or has errored.)
	DBusSignalJobRemoved = "JobRemoved"
)

Variables

This section is empty.

Functions

func AutoEdgeCombiner

func AutoEdgeCombiner(ae ...engine.AutoEdge) (engine.AutoEdge, error)

AutoEdgeCombiner takes any number of AutoEdge structs, and combines them into a single one, so that the logic from each one can be built separately, and then combined using this utility. This makes implementing different AutoEdge generators much easier. This respects the Next() and Test() API, and ratchets through each AutoEdge entry until they have all run their course.

func B64ToRes

func B64ToRes(str string) (engine.Res, error)

B64ToRes decodes a resource from a base64 encoded string (after deserialization).

func CleanError

func CleanError(err error) string

CleanError takes the engine errors and prints them on a single line. TODO: maybe we can improve this here, it's a bit ugly.

func GetGID

func GetGID(group string) (int, error)

GetGID returns the GID of a group. It supports a GID or a group name. Caller should first check group is not empty. It will return an error if it can't lookup the GID or group name.

func GetUID

func GetUID(username string) (int, error)

GetUID returns the UID of an user. It supports an UID or an username. Caller should first check user is not empty. It will return an error if it can't lookup the UID or username.

func LangFieldNameToStructFieldName

func LangFieldNameToStructFieldName(kind string) (map[string]string, error)

LangFieldNameToStructFieldName returns the mapping from lang (AST) field names to field name as used in the struct. The logic here is a bit strange; if the resource has struct tags, then it uses those, otherwise it falls back to using the lower case versions of things. It might be clever to combine the two so that tagged fields are used as such, and others are used in lowercase, but this is currently not implemented. TODO: should this behaviour be changed?

func LangFieldNameToStructType

func LangFieldNameToStructType(kind string) (map[string]*types.Type, error)

LangFieldNameToStructType returns the mapping from lang (AST) field names, and the expected type in our type system for each. XXX: Should this return unification variables instead of variant types?

func LowerStructFieldNameToFieldName

func LowerStructFieldNameToFieldName(res engine.Res) (map[string]string, error)

LowerStructFieldNameToFieldName returns a mapping from the lower case version of each field name to the actual field name. It only returns public fields. It returns an error if it finds a collision.

func ParseSymbolicModes

func ParseSymbolicModes(modes []string, from os.FileMode, allowAssign bool) (os.FileMode, error)

ParseSymbolicModes parses a slice of symbolic mode strings. By default it will only accept the assignment input (=), but if allowAssign is true, then all symbolic mode strings (=, +, -) can be used as well.

Symbolic mode is expected to be a string of who (user, group, other) then the operation (=, +, -) then the change (read, write, execute, setuid, setgid, sticky).

Eg: ug=rw

If you repeat yourself in the slice (eg. u=rw,u=w) ParseSymbolicModes will fail with an error.

func ResPathUID

func ResPathUID(res engine.Res) string

ResPathUID returns a unique resource UID based on its name and kind. It's safe to use as a token in a path, and as a result has no slashes in it.

func ResToB64

func ResToB64(res engine.Res) (string, error)

ResToB64 encodes a resource to a base64 encoded string (after serialization).

func ResToParamValues

func ResToParamValues(res engine.Res) (map[string]types.Value, error)

ResToParamValues returns a list of field names and their corresponding values if they are non-zero. This is meant for testing, and should be improved for robustness or with tests if it's ever used for value extraction. This also contains a hack to specifically print from resources that contain interface fields too. Consider moving that into types.ValueOf after testing if that doesn't break other code paths.

func RestartUnit

func RestartUnit(ctx context.Context, conn *dbus.Conn, unit string) error

RestartUnit resarts the given dbus unit and waits for it to finish starting.

func StopUnit

func StopUnit(ctx context.Context, conn *dbus.Conn, unit string) error

StopUnit stops the given dbus unit and waits for it to finish stopping.

func StrListCmp

func StrListCmp(x, y []string) error

StrListCmp compares two lists of strings. If they are not the same length or do not contain identical strings in the same order, then this errors.

func StrPtrCmp

func StrPtrCmp(x, y *string) error

StrPtrCmp compares two pointers to strings. If they aren't both nil or aren't both of the same value, then this errors.

func StructFieldCompat

func StructFieldCompat(st1 interface{}, key1 string, st2 interface{}, key2 string) error

StructFieldCompat returns whether a send struct and key is compatible with a recv struct and key. This inputs must both be a ptr to a string, and a valid key that can be found in the struct tag. The (1) first values are for send, and the (2) second values are for recv. TODO: add a bool to decide if *string to string or string to *string is okay.

func StructTagToFieldName

func StructTagToFieldName(stptr interface{}) (map[string]string, error)

StructTagToFieldName returns a mapping from recommended alias to actual field name. It returns an error if it finds a collision. It uses the `lang` tags. It must be passed a ptr to a struct or it will error.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL