Skip to content

Commit

Permalink
xml list control
Browse files Browse the repository at this point in the history
  • Loading branch information
mihakralj committed Sep 21, 2023
1 parent 7230fdc commit 0d0f3b6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ jobs:
echo -e "name: opnsense-cli\nversion: ${VERSION}\norigin: net-mgmt/opnsense-cli\ncomment: \"CLI to manage and monitor OPNsense firewall configuration, check status, change settings, and execute commands.\"\ndesc: \"opnsense is a command-line utility for managing, configuring, and monitoring OPNsense firewall systems. It facilitates non-GUI administration, both directly in the shell and remotely via an SSH tunnel. All interactions with OPNsense utilize the same mechanisms as the Web GUI, including staged modifications of config.xml and execution of available configd commands.\"\nmaintainer: \"[email protected]\"\nwww: \"https://github.com/mihakralj/opnsense-cli\"\nabi: \"FreeBSD:*:amd64\"\nprefix: /usr/local\nflatsize: ${flatsize}" > /Users/runner/work/opnsense-cli/opnsense-cli/manifest
echo -e "files: {\n \"/usr/local/bin/opnsense\": \"${checksum}\",\n}" >> /Users/runner/work/opnsense-cli/opnsense-cli/manifest
pkg create -M /Users/runner/work/opnsense-cli/opnsense-cli/manifest -o /Users/runner/work/opnsense-cli/opnsense-cli/ -f txz
mv opnsense-cli-*.pkg *.txz
ls -l
- name: Upload all artifacts
Expand All @@ -175,6 +174,7 @@ jobs:
./opnsense-macos
./opnsense*.txz
./opnsense.pkg
./opnsense-cli-*.pkg
release:
needs: [linuxwindows, macbsd]
Expand Down
50 changes: 9 additions & 41 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ Administrators typically only two options how to manage the firewall: either th

### Commands

- **`show system [<xpath>]`**: Retrieves system information from the firewall.
- **`show config [<xpath>]`**: Displays xpath segment of config.xml.
- **`show backup [<backup.xml>]`**: Lists available backup configs or displays a specific backup.
- **`sysctl [<xpath>]`**: Retrieves system information from the firewall.
- **`show [<xpath>]`**: Displays xpath segment of config.xml.
- **`backups [<backup.xml>]`**: Lists available backup configs or displays a specific backup.
- **`run <service> <command>`**: Executes commands on OPNsense.
- **`set <xpath> value <value>`**: Sets a value of a specific node in staging.xml file.
- **`commit`**: Moves staging.xml to active config.xml.
Expand All @@ -36,49 +36,17 @@ Administrators typically only two options how to manage the firewall: either th
- **`delete age [days]`**: Deletes all backups older than specified days
- **`delete keep [count]`**: Keeps specified number of backups and deletes the rest
- **`delete trim [count]`**: Deletes number of the oldest backups
- **`set <xpath> [value] [(attribute)]`**: Adds a new branch, value and/or attribute
- **`set <xpath> [value] [(attribute)] -d`**: Deletes branch, value and/or attribute

### Flags

- **`--target (-t)`**: Sets the target OPNsense in the form of `user@hostname[:port]`.
- **`--no-color (-n)`**: Disable ANSI color output
- **`--force (-f)`**: Removes checks and prompts before `config.xml` or `configctl` are touched.
- **`--depth (-d)`**: Specifies the number of branch levels to show.
- **`--verbose (-v)`**: Sets verbosity (1=error, 2=warning, 3=info, 4=note, 5=debug).
- **`--no-color (-n)`**: Removes ANSI colors from the printout.
- **`--xml`**: Displays results in XML format.
- **`--json`**: Displays results in JSON format.
- **`--yaml`**: Displays results in YAML format.
- **`--xml (-x)`**: Displays results in XML format.
- **`--json (-j)`**: Displays results in JSON format.
- **`--yaml (-y)`**: Displays results in YAML format.

## Installation

### FreeBSD 12/13

installs to `usr/local/bin`:
```bash
sudo pkg add https://github.com/mihakralj/opnsense-cli/releases/download/beta/opnsense.txz
```
### Debian/Ubuntu

installs to `usr/local/bin`:
```bash
curl -O https://github.com/mihakralj/opnsense-cli/releases/download/beta/opnsense.deb
dpkg -i opnsense.deb
```

### macOS
Package install (installs to `/usr/local/bin`):
```bash
curl -O https://github.com/mihakralj/opnsense-cli/releases/download/beta/opnsense.pkg
sudo installer -pkg opnsense.pkg -target /
```

signed binary:
```bash
curl -O https://github.com/mihakralj/opnsense-cli/releases/download/beta/opnsense -O opnsense
sudo chmod +x opnsense
```

### Windows
executable `opnsense.exe`:
```powershell
Invoke-WebRequest -Uri https://github.com/mihakralj/opnsense-cli/releases/download/beta/opnsense.exe -OutFile opnsense.exe
```
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var (
Version string = "0.12.0"
Version string = "0.12.1"
verbose int
force bool
host string
Expand Down
11 changes: 6 additions & 5 deletions internal/eTreeRender.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@ import (

func FocusEtree(doc *etree.Document, path string) *etree.Element {
foundElements := doc.FindElements(path)
foundElement := foundElements[len(foundElements)-1]
if foundElement == nil {
if len(foundElements) == 0 {
Log(1, "Xpath element \"%s\" does not exist", path)
return nil
}
//fmt.Println(path, "elements: ", len(foundElements))

parts := strings.Split(path, "/")
focused := etree.NewElement(parts[0])

// Get the space of the found element
space := foundElement.Space
space := foundElements[0].Space
depth := len(parts)
if depth > 1 {
parts = parts[:depth-1]
Expand All @@ -41,7 +39,10 @@ func FocusEtree(doc *etree.Document, path string) *etree.Element {
}
current = newElem
}
current.AddChild(foundElement.Copy())
// Add all found elements
for _, foundElement := range foundElements {
current.AddChild(foundElement.Copy())
}
} else {
focused = doc.Root()
}
Expand Down
7 changes: 5 additions & 2 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ func EtreeToJSON(el *etree.Element) (string, error) {

func ConfigToTTY(doc *etree.Document, path string) string {
focused := FocusEtree(doc, path)
//calculate depth of path
return EtreeToTTY(focused, depth+len(strings.Split(path, "/"))-1, 0)
d := depth + len(strings.Split(path, "/")) - 1
if len(doc.FindElements(path)) > 1 {
d -= 1
}
return EtreeToTTY(focused, d, 0)
}

func ConfigToXML(doc *etree.Document, path string) string {
Expand Down

0 comments on commit 0d0f3b6

Please sign in to comment.