Module eco.uci
UCI (Unified Configuration Interface) bindings.
This module provides bindings to OpenWrt's libuci.
The main entry point is cursor, which creates a cursor object used to load, query and modify UCI configuration.
Functions
| cursor ([confdir[, savedir]]) | Create a UCI cursor. |
Class cursor
Functions
- cursor ([confdir[, savedir]])
-
Create a UCI cursor.
Call forms:
uci.cursor()(use default dirs)uci.cursor(confdir)uci.cursor(confdir, savedir)
Parameters:
Returns:
-
cursor
c
Usage:
local uci = require 'eco.uci' local c = uci.cursor('/etc/config') local ip = c:get('network', 'lan', 'ipaddr')
Class cursor
The cursor is a userdata; it supports explicit cursor:close and is
also finalized on GC. In Lua 5.4 it additionally supports to-be-closed
variables via the __close metamethod.
- cursor:close ()
- Close the cursor and free underlying libuci context.
- cursor:unload (package)
-
Unload a previously loaded package.
Parameters:
- package string Package name.
Returns:
-
boolean
ok
trueon success,falseif not loaded.Or
- boolean false
- string err
- cursor:load (package)
-
Load a UCI package.
This first unloads any previously loaded package with the same name.
Parameters:
- package
string
Package name (e.g.
'network').
Returns:
-
boolean
ok
Or
- boolean false
- string err
- package
string
Package name (e.g.
- cursor:get (package[, section[, option]])
-
Get a UCI value.
Accepted call forms:
c:get('p.s.o')c:get('p', 's')(returns sectiontype, name)c:get('p', 's', 'o')
Return values:
- option: string value, or a list table for list options
- section:
type, name(two strings) - package: a table of sections
On error: returns
nil, err.Parameters:
- package string Package name or combined path.
- section
string
Section name (or anonymous ref, e.g.
'@type[0]'). (optional) - option string Option name. (optional)
Returns:
-
any
value
Or
- nil
- string err
- cursor:get_all (package[, section[, option]])
-
Get a UCI value, returning full section tables.
Similar to cursor:get, but when pointing to a section it returns a table containing:
['.anonymous']boolean['.type']string['.name']string['.index']integer (when available)- plus all options under their option names
Parameters:
- package string Package name or combined path.
- section
string
Section name (or anonymous ref, e.g.
'@type[0]'). (optional) - option string Option name. (optional)
Returns:
-
any
value
Or
- nil
- string err
- cursor:add (package, type)
-
Add a new section.
Parameters:
Returns:
-
string
sid Section identifier (name).
Or
- nil
- string err
- cursor:set (package[, section[, option[, value]]])
-
Set a package/section/option.
Accepted call forms:
c:set('p.s.o=v')orc:set('p.s=v')c:set('p', 's', 'o', 'v')c:set('p', 's', 'v')(sets section type?)c:set('p', 's', 'o', {'v1', 'v2'})(list option)
Parameters:
- package string Package name or combined path.
- section string Section name. (optional)
- option string Option name. (optional)
- value any Value (string) or list table. (optional)
Returns:
-
boolean
ok
Or
- boolean false
- string err
- cursor:rename (package[, section[, option[, value]]])
-
Rename a section or option.
Accepted call forms match cursor:set (without list values).
Parameters:
- package string Package name or combined path.
- section string Section name. (optional)
- option string Option name. (optional)
- value string New name. (optional)
Returns:
-
boolean
ok
Or
- boolean false
- string err
- cursor:save (package)
-
Save a package.
Parameters:
- package string Package name.
Returns:
-
boolean
ok
Or
- boolean false
- string err
- cursor:delete (package[, section[, option]])
-
Delete a section or option.
Accepted call forms:
c:delete('p.s.o')/c:delete('p.s')c:delete('p', 's')/c:delete('p', 's', 'o')
Parameters:
- package string Package name or combined path.
- section string Section name. (optional)
- option string Option name. (optional)
Returns:
-
boolean
ok
Or
- boolean false
- string err
- cursor:commit (package)
-
Commit a package (write changes).
Parameters:
- package string Package name.
Returns:
-
boolean
ok
Or
- boolean false
- string err
- cursor:revert (package[, section[, option]])
-
Revert changes.
This can revert a whole package or a specific section/option.
Parameters:
- package string Package name or combined path.
- section string Section name (or anonymous ref). (optional)
- option string Option name. (optional)
Returns:
-
boolean
ok
Or
- boolean false
- string err
- cursor:reorder (package[, section[, index]])
-
Reorder a section by index.
Parameters:
- package string Package name or combined path.
- section string Section name. (optional)
- index integer New position (0-based). (optional)
Returns:
-
boolean
ok
Or
- boolean false
- string err
- cursor:foreach (package[, type], cb)
-
Iterate sections and call a callback.
The callback is invoked as
cb(section_table). Returnfalsefrom the callback to stop iteration early.Parameters:
- package string Package name.
- type
string
Filter by section type (or
nilfor all types). (optional) - cb function Callback.
Returns:
-
boolean
ok
trueif iteration ran,falseotherwise.Usage:
local c = uci.cursor() c:foeach('network', function(s) print(s['.type'], s['.name']) end) c:foeach('network', 'interface', function(s) print(s['.name']) end)
- cursor:each (package[, type])
-
Get an iterator over sections.
Parameters:
Returns:
-
function
iter Iterator function yielding section tables.
Or
-
nil
If package does not exist.
Usage:
local c = uci.cursor() for s in c:each('network') do print(s['.type'], s['.name']) end
- cursor:get_confdir ()
-
Get current configuration directory.
Returns:
-
string
dir
- cursor:set_confdir (dir)
-
Set configuration directory.
Parameters:
- dir string
Returns:
-
boolean
ok
Or
- boolean false
- string err
- cursor:get_savedir ()
-
Get current save directory.
Returns:
-
string
dir
- cursor:set_savedir (dir)
-
Set save directory.
Parameters:
- dir string
Returns:
-
boolean
ok
Or
- boolean false
- string err
- cursor:list_configs ()
-
List available config files.
Returns:
-
table
configs Array of config file names.
Or
- nil On failure.
- string err Error message.