uci

package
v0.0.0-...-683b059 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2022 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package uci contains utility functions to wrap around the uci program.

The UCI (United Configuration Interface) system is used to centralize the configuration of OpenWrt services.

See https://openwrt.org/docs/guide-user/base-system/uci for documentation on the UCI system and https://openwrt.org/docs/guide-user/base-system/uci#command-line_utility for documentation on the uci CLI which this package uses.

Index

Constants

View Source
const (
	// ConfigDHCP is the name of config for dnsmasq and odhcpd
	// settings: DNS, DHCP, DHCPv6.
	ConfigDHCP = "dhcp"

	// ConfigNetwork is the name of the config for switch, interface and
	// route configuration: Basics, IPv4, IPv6, Routes, Rules, WAN, Aliases,
	// Switches, VLAN, IPv4/IPv6 transitioning, Tunneling.
	ConfigNetwork = "network"

	// ConfigWireless is the name of the config for wireless settings and
	// Wi-Fi network definition.
	ConfigWireless = "wireless"
)

Constants for relevant config names as specified in the OpenWrt documentation here: https://openwrt.org/docs/guide-user/base-system/uci#configuration_files.

Variables

This section is empty.

Functions

func BackupConfig

func BackupConfig(ctx context.Context, uci *Runner, config, backupFilePath string) (string, error)

BackupConfig copies the config file, located at configDir/config, to the backupPath. If no backupFilePath is specified, it defaults to using configBackupDir/config. Returns the path to the newly created backup file. If the backupFilePath does not contain a directory, it is assumed that the target directory is configBackupDir.

If a file exists at backupFilePath already, it is overwritten.

Note: A "uci commit" is not necessary after running this, as it manually changes the config files and does not go through the uci CLI. However, to make the config changes used, any corresponding services that use it must be reloaded.

func CommitAndReloadConfig

func CommitAndReloadConfig(ctx context.Context, uci *Runner, config string) error

CommitAndReloadConfig first commits any pending config changes and then reloads its dependent service to put the changes into effect.

func ReloadConfigServices

func ReloadConfigServices(ctx context.Context, uci *Runner, configs ...string) error

ReloadConfigServices reloads any service that uses any of the specified configs with the current committed configuration settings.

func ReloadNetwork

func ReloadNetwork(ctx context.Context, uci *Runner) error

ReloadNetwork reloads the network service with the current committed settings in ConfigNetwork. See the OpenWrt documentation for more info at https://openwrt.org/docs/guide-user/base-system/basic-networking#network_configuration_management.

func ReloadWifi

func ReloadWifi(ctx context.Context, uci *Runner) error

ReloadWifi reloads Wi-Fi, which uses the current committed settings in that are in ConfigWireless.

func ResetConfigs

func ResetConfigs(ctx context.Context, uci *Runner, expectBackupsToExist bool, configs ...string) error

ResetConfigs restores the given configs to their backed up states. If expectBackupsToExist is false, configs are backed up again after they are restored to ensure a backup exists.

Note: A "uci commit" is not necessary after running this, as it manually changes the config files and does not go through the uci CLI. However, to make the config changes used, any corresponding services that use them must be reloaded.

func RestartDnsmasq

func RestartDnsmasq(ctx context.Context, uci *Runner) error

RestartDnsmasq restarts the dnsmasq service, which reloads the current committed settings that are in ConfigDHCP. See the OpenWrt documentation for more info at https://openwrt.org/docs/guide-user/base-system/dhcp.

func RestoreConfig

func RestoreConfig(ctx context.Context, uci *Runner, config, backupFilePath string, ignoreMissingBackup bool) (bool, error)

RestoreConfig restores the config by replacing the existing config file, located at configDir/config, with a previously backed up copy located at backupFilePath. If no backupFilePath is specified, it defaults to using configBackupDir/config. If backupFilePath does not exist and ignoreMissingBackup is true, the config will not be changed and the function will return false. Returns true if the target config was updated from an existing backup.

Note: A "uci commit" is not necessary after running this, as it manually changes the config files and does not go through the uci CLI. However, to make the config changes used, any corresponding services that use it must be reloaded.

Types

type CLIFlag

type CLIFlag []string

CLIFlag is a list of the CLI arguments that make up a CLI flag and any parameters the flag requires for the uci command.

uci CLI flags are documented at https://openwrt.org/docs/guide-user/base-system/uci#usage.

func CLIFlagConfigPath

func CLIFlagConfigPath(path string) CLIFlag

CLIFlagConfigPath builds the CLIFlag that configures the uci command to set the search path for config files (default: /etc/config).

func CLIFlagDelimiter

func CLIFlagDelimiter() CLIFlag

CLIFlagDelimiter builds the CLIFlag that configures the uci command to set the delimiter for list values in uci show.

func CLIFlagDisableStrictMode

func CLIFlagDisableStrictMode() CLIFlag

CLIFlagDisableStrictMode builds the CLIFlag that configures the uci command to disable strict mode.

func CLIFlagDoNotNameUnnamedSections

func CLIFlagDoNotNameUnnamedSections() CLIFlag

CLIFlagDoNotNameUnnamedSections builds the CLIFlag that configures the uci command to not name unnamed sections.

func CLIFlagDoNotUseShowExtendedSyntax

func CLIFlagDoNotUseShowExtendedSyntax() CLIFlag

CLIFlagDoNotUseShowExtendedSyntax builds the CLIFlag that configures the uci command to not use extended syntax on 'show'.

func CLIFlagFileInput

func CLIFlagFileInput(file string) CLIFlag

CLIFlagFileInput builds the CLIFlag that configures the uci command to use file as input instead of stdin.

func CLIFlagForceStrictMode

func CLIFlagForceStrictMode() CLIFlag

CLIFlagForceStrictMode builds the CLIFlag that forces the uci command to use strict mode (stops on parser errors and is the default behavior).

func CLIFlagMerge

func CLIFlagMerge() CLIFlag

CLIFlagMerge builds the CLIFlag that configures the uci command to merge data into an existing package when importing.

func CLIFlagNameUnnamedSections

func CLIFlagNameUnnamedSections() CLIFlag

CLIFlagNameUnnamedSections builds the CLIFlag that configures the uci command to name unnamed sections on export (default behavior).

func CLIFlagQuietMode

func CLIFlagQuietMode() CLIFlag

CLIFlagQuietMode builds the CLIFlag that configures the uci command to use quiet mode (does not print error messages).

func CLIFlagSearchPath

func CLIFlagSearchPath(path string) CLIFlag

CLIFlagSearchPath builds the CLIFlag that configures the uci command to add a search path for config change files.

func CLIFlagSearchPathDefault

func CLIFlagSearchPathDefault(path string) CLIFlag

CLIFlagSearchPathDefault builds the CLIFlag that configures the uci command to add a search path for config change files and use it as a default.

type Runner

type Runner struct {
	// contains filtered or unexported fields
}

Runner contains methods for running the uci command.

func NewRemoteRunner

func NewRemoteRunner(host *ssh.Conn) *Runner

NewRemoteRunner creates an uci runner for remote execution.

func NewRunner

func NewRunner(cmd cmd.Runner) *Runner

NewRunner creates uci command runner.

func (*Runner) Add

func (r *Runner) Add(ctx context.Context, config, sectionType string, flags ...CLIFlag) error

Add adds an anonymous section of type sectionType to the given configuration.

CLI usage is "uci add <config> <sectionType>".

func (*Runner) AddList

func (r *Runner) AddList(ctx context.Context, config, section, option, str string, flags ...CLIFlag) error

AddList adds the given string to an existing list option.

CLI usage is "uci add_list <config>.<section>.<option>=<str>".

func (*Runner) Batch

func (r *Runner) Batch() error

Batch executes a multi-line UCI script which is typically wrapped into a "here" document syntax. This command is not supported by this wrapper and an error will always be returned if this is called.

func (*Runner) Changes

func (r *Runner) Changes(ctx context.Context, config string, flags ...CLIFlag) ([]string, error)

Changes lists staged changes to the given configuration file or if none given, all configuration files.

CLI usage is "uci changes [<config>]".

func (*Runner) Commit

func (r *Runner) Commit(ctx context.Context, config string, flags ...CLIFlag) error

Commit writes changes of the given configuration file, or if none is given, all configuration files, to the filesystem.

All “uci set”, “uci add”, “uci rename” and “uci delete” commands are staged into a temporary location and written to flash at once with “uci commit”. This must be called after editing any of the mentioned commands to save their changes.

CLI usage is "uci commit [<config>]".

func (*Runner) DelList

func (r *Runner) DelList(ctx context.Context, config, section, option, str string, flags ...CLIFlag) error

DelList removes the given string from an existing list option.

CLI usage is "uci del_list <config>.<section>.<option>=<str>".

func (*Runner) Delete

func (r *Runner) Delete(ctx context.Context, config, section, option string, flags ...CLIFlag) ([]string, error)

Delete deletes the given section or option.

CLI usage is "uci delete <config>.<section>[.<option>]".

func (*Runner) Export

func (r *Runner) Export(ctx context.Context, config string, flags ...CLIFlag) ([]string, error)

Export exports the configuration in a machine-readable format.

CLI usage is "uci export [<config>]".

func (*Runner) Get

func (r *Runner) Get(ctx context.Context, config, section, option string, flags ...CLIFlag) ([]string, error)

Get gets the value of the given option or the type of the given section.

CLI usage is "uci get <config>.<section>[.<option>]".

func (*Runner) Import

func (r *Runner) Import(ctx context.Context, config string, flags ...CLIFlag) error

Import imports configuration files in UCI syntax.

CLI usage is "uci import [<config>]".

func (*Runner) Rename

func (r *Runner) Rename(ctx context.Context, config, section, option, name string, flags ...CLIFlag) error

Rename renames the given option or section to the given name.

CLI usage is "uci rename <config>.<section>[.<option>]=<name>".

func (*Runner) Reorder

func (r *Runner) Reorder(ctx context.Context, config, section, position string, flags ...CLIFlag) error

Reorder moves a section to another position.

CLI usage is "uci reorder <config>.<section>=<position>".

func (*Runner) Revert

func (r *Runner) Revert(ctx context.Context, config, section, option string, flags ...CLIFlag) error

Revert reverts the given option, section or configuration file.

CLI usage is "uci revert <config>[.<section>[.<option>]]".

func (*Runner) Set

func (r *Runner) Set(ctx context.Context, config, section, option, value string, flags ...CLIFlag) error

Set sets the value of the given option, or adds a new section with the type set to the given value.

CLI usage is "uci set <config>.<section>[.<option>]=<value>".

func (*Runner) Show

func (r *Runner) Show(ctx context.Context, config, section, option string, flags ...CLIFlag) ([]string, error)

Show shows the given option, section or configuration in compressed notation.

CLI usage is "uci show [<config>[.<section>[.<option>]]]".

func (*Runner) Uci

func (r *Runner) Uci(ctx context.Context, flags []CLIFlag, args ...string) error

Uci runs the uci command with the given arguments and CLI flags.

func (*Runner) UciWithOutput

func (r *Runner) UciWithOutput(ctx context.Context, flags []CLIFlag, args ...string) ([]string, error)

UciWithOutput runs the uci command with the given arguments and CLI flags and returns the output lines in a list.

Jump to

Keyboard shortcuts

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