OpenWrt UCI configuration

The uci module provides access to the native OpenWrt libuci API for reading and manipulating UCI configuration files.

Functions can be individually imported and directly accessed using the named import syntax:

import { cursor } from 'uci';

let ctx = cursor();
let hostname = ctx.get_first('system', 'system', 'hostname');

Alternatively, the module namespace can be imported using a wildcard import statement:

import * as uci from 'uci';

let ctx = uci.cursor();
let hostname = ctx.get_first('system', 'system', 'hostname');

Additionally, the uci module namespace may also be imported by invoking the ucode interpreter with the -luci switch.

Classes

uci.cursor

Represents a context for interacting with uci configuration files.

Operations on uci configurations are performed through a uci cursor object which operates on in-memory representations of loaded configuration files.

Any changes made to configuration values are local to the cursor object and held in memory only until they're written out to the filesystem using the save() and commit() methods.

Changes performed in one cursor instance are not reflected in another, unless the first instance writes those changes to the filesystem and the other instance explicitly (re)loads the affected configuration files.

Methods

cursor(config_diropt, delta_diropt) → {cursor}nullable

Instantiate uci cursor.

A uci cursor is a context for interacting with uci configuration files. It's purpose is to cache and hold changes made to loaded configuration states until those changes are written out to disk or discared.

Unsaved and uncommitted changes in a cursor instance are private and not visible to other cursor instances instantiated by the same program or other processes on the system.

Returns the instantiated cursor on success.

Returns null on error, e.g. if an invalid path argument was provided.

Parameters:
NameTypeDescription
config_dirstring(optional, default: /etc/config)

The directory to search for configuration files. It defaults to the well known uci configuration directory /etc/config but may be set to a different path for special purpose applications.

delta_dirstring(optional, default: /tmp/.uci)

The directory to save delta records in. It defaults to the well known /tmp/.uci path which is used as default by the uci command line tool.

By changing this path to a different location, it is possible to isolate uncommitted application changes from the uci cli or other processes on the system.

Returns: cursor

error() → {string}nullable

Query error information.

Returns a string containing a description of the last occurred error or null if there is no error information.

Returns: string
Example
// Trigger error
const ctx = cursor();
ctx.set("not_existing_config", "test", "1");

// Print error (should yield "Entry not found")
print(ctx.error(), "\n");