Documentation
¶
Index ¶
- Constants
- func BoolValue(b Bool) bool
- func Call[T any](ctx context.Context, t Transport, service, method string, data any) (*T, error)
- func MapUbusCodeToError(code int) error
- type Bool
- type Result
- type RpcClient
- type RpcOption
- type SocketClient
- func (c *SocketClient) Call(ctx context.Context, service, method string, data any) (Result, error)
- func (c *SocketClient) Close() error
- func (c *SocketClient) DialTimeout() time.Duration
- func (c *SocketClient) PeerID() uint32
- func (c *SocketClient) ReadTimeout() time.Duration
- func (c *SocketClient) SetLogger(logger *slog.Logger)
- func (c *SocketClient) WriteTimeout() time.Duration
- type SocketOption
- type Transport
Constants ¶
const ( UbusStatusOK = 0 UbusStatusInvalidCommand = 1 UbusStatusInvalidParameter = 2 UbusStatusMethodNotFound = 3 UbusStatusNotFound = 4 UbusStatusNoData = 5 UbusStatusPermissionDenied = 6 UbusStatusTimeout = 7 UbusStatusNotSupported = 8 UbusStatusUnknown = 9 UbusStatusConnectionFailed = 10 )
Ubus error codes.
const Version = "2.0.0-alpha1"
Version is the current version of the goubus library.
Variables ¶
This section is empty.
Functions ¶
func Call ¶
Call is a generic helper that wraps Transport.Call and unmarshals the response. T represents the expected type of the response data.
func MapUbusCodeToError ¶
MapUbusCodeToError maps a ubus integer code to a typed error defined in errdefs. If the code is not recognized, it returns an unknown error wrapping the code.
Types ¶
type Bool ¶
type Bool bool
Bool is a JSON boolean type that gracefully accepts numbers or string representations. It is useful for handling inconsistent boolean representations in ubus responses (e.g., 1/0, "1"/"0", "true"/"false").
func (*Bool) MarshalJSON ¶
MarshalJSON ensures consistent boolean encoding.
func (*Bool) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler, accepting 0/1, "0"/"1", true/false, "true"/"false", and null.
type Result ¶
type Result interface {
// Unmarshal decodes the response data into the provided target.
// The target must be a pointer to a compatible type.
Unmarshal(target any) error
}
Result is the interface that wraps the basic Unmarshal method. It allows for lazy unmarshaling of ubus call responses into specific Go types.
type RpcClient ¶
type RpcClient struct {
// contains filtered or unexported fields
}
RpcClient handles communication with the ubus JSON-RPC endpoint. It manages authentication and session state internally.
func NewRpcClient ¶
func NewRpcClient(ctx context.Context, host, username, password string, opts ...RpcOption) (*RpcClient, error)
NewRpcClient creates an authenticated RPC client.
type RpcOption ¶
type RpcOption func(*RpcClient)
RpcOption defines a functional option for an RpcClient.
func WithRpcLogger ¶
WithRpcLogger sets the logger for the RPC client.
type SocketClient ¶
type SocketClient struct {
// contains filtered or unexported fields
}
SocketClient implements direct ubus unix socket transport. It communicates directly with the ubusd daemon on the local system.
func NewSocketClient ¶
func NewSocketClient(ctx context.Context, sockPath string, opts ...SocketOption) (*SocketClient, error)
NewSocketClient creates a new ubus socket client and performs the HELLO handshake. If sockPath is empty, it uses the default path (/tmp/run/ubus/ubus.sock).
func (*SocketClient) Close ¶
func (c *SocketClient) Close() error
func (*SocketClient) DialTimeout ¶
func (c *SocketClient) DialTimeout() time.Duration
func (*SocketClient) PeerID ¶
func (c *SocketClient) PeerID() uint32
func (*SocketClient) ReadTimeout ¶
func (c *SocketClient) ReadTimeout() time.Duration
func (*SocketClient) SetLogger ¶
func (c *SocketClient) SetLogger(logger *slog.Logger)
func (*SocketClient) WriteTimeout ¶
func (c *SocketClient) WriteTimeout() time.Duration
type SocketOption ¶
type SocketOption func(*SocketClient)
SocketOption defines a functional option for a SocketClient.
func WithDialTimeout ¶
func WithDialTimeout(timeout time.Duration) SocketOption
WithDialTimeout sets the timeout for connecting to the socket.
func WithReadTimeout ¶
func WithReadTimeout(timeout time.Duration) SocketOption
WithReadTimeout sets the timeout for reading from the socket.
func WithSocketLogger ¶
func WithSocketLogger(logger *slog.Logger) SocketOption
WithSocketLogger sets the logger for the socket client.
func WithWriteTimeout ¶
func WithWriteTimeout(timeout time.Duration) SocketOption
WithWriteTimeout sets the timeout for writing to the socket.
type Transport ¶
type Transport interface {
// Call executes a ubus call for the specified service and method.
// The data parameter is the payload for the call, which will be marshaled to JSON/blobmsg.
Call(ctx context.Context, service, method string, data any) (Result, error)
// SetLogger configures the logger used by the transport.
SetLogger(logger *slog.Logger)
// Close releases any resources held by the transport (e.g., closing connections).
Close() error
}
Transport is the interface that wraps the basic ubus call method. Transport provides a unified way to interact with local ubus sockets and remote JSON-RPC endpoints.