Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create wled-tools.sh #4625

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Create wled-tools.sh #4625

wants to merge 10 commits into from

Conversation

LordMike
Copy link
Contributor

@LordMike LordMike commented Mar 29, 2025

Per discussion #4621, add a discover-fueled tool to update/backup wled devices in the local network.

Todo:

  • discover and log hostnames, but connect to ips. Avoid requiring host dns to work when connecting

Help text:

# ./wled-tools.sh
Usage: wled-tools.sh [OPTIONS] COMMAND [ARGS...]

Options:
  -h, --help              Show this help message and exit.
  -v, --verbose           Enable verbose output for debugging.
  -t, --target <IP/Host>  Specify a single WLED device by IP address or hostname.
  -D, --discover          Discover multiple WLED devices using mDNS.
  -d, --directory <Path>  Specify a directory for saving backups (default: working directory).
  -f, --firmware <File>   Specify the firmware file for updating devices.
  -q, --quiet             Suppress logging output (also makes discover output hostnames only).

Commands:
  backup      Backup the current state of a WLED device or multiple discovered devices.
  update      Update the firmware of a WLED device or multiple discovered devices.
  discover    Discover WLED devices using mDNS and list their IP addresses and names.

Example command:

# ./wled-tools.sh backup --discover -d backups
[INFO] Backing up device config/presets: wled-light-1-6.local
[INFO] Backing up device config/presets: wled-light-5-3.local

Summary by CodeRabbit

  • New Features
    • Introduced a command-line tool for managing WLED devices.
    • Enables device discovery on your network.
    • Supports configuration backup and firmware updates.
    • Provides intuitive logging with clear visual cues and adjustable verbosity.
    • Offers comprehensive usage instructions through a command-line help option.

Per discussion, add a discover-fueled tool to update/backup wled devices in the local network.
Copy link

coderabbitai bot commented Mar 29, 2025

Walkthrough

A new Bash script named wled-tools.sh has been added to manage WLED devices on a local network. The script features color-coded logging and includes a log function for output control. It implements a curl_handler for managing HTTP requests with error handling. The script accepts command-line arguments for operations such as device discovery, configuration backup, and firmware updates. It includes functions for discovering devices using avahi-browse, backing up configurations, and validating firmware updates. A help function provides usage instructions.

Changes

File Path Change Summary
tools/wled-tools/wled-tools.sh Added a new Bash script that supports device discovery, configuration backup, firmware updates, logging with colored output, and error handling for HTTP requests.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9e96bd6 and 81b7422.

📒 Files selected for processing (1)
  • tools/wled-tools (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tools/wled-tools
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: wled_build / Build Environments (usermods)
  • GitHub Check: wled_build / Build Environments (lolin_s2_mini)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@LordMike LordMike marked this pull request as draft March 29, 2025 13:10
@LordMike LordMike marked this pull request as ready for review March 29, 2025 17:21
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (6)
tools/wled-tools.sh (6)

3-9: Unused Color Variable Defined
The GREEN variable is defined but never used in the script. Consider removing it or utilizing it for consistency with the other color definitions.

🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 4-4: GREEN appears unused. Verify use (or export if used externally).

(SC2034)


27-47: Generic cURL Handler is Functional
The curl_handler function encapsulates HTTP request error handling well. One minor point: ensure that the command passed to this function does not introduce word-splitting issues. Consider validating or sanitizing $command if user input could ever be involved.


71-87: Device Discovery Function is Effective
The discover_devices function leverages avahi-browse and processes its output correctly. If you wish to enhance robustness, consider outputting discovered device entries as newline-separated values instead of a space‐delimited string (this can help avoid potential parsing issues when hostname fields contain spaces).


89-113: Backup Function – Quotation Issue in cURL Command Strings
In the backup_one function, the assignments on lines 105 and 106 trigger shellcheck warnings because inner quotes are not properly escaped. This may lead to unexpected behavior when the URLs or destination paths contain spaces.
Consider applying the following diff to fix the quoting:

-    local curl_command_cfg="curl -s "$cfg_url" -o "$cfg_dest.tmp""
-    local curl_command_presets="curl -s "$presets_url" -o "$presets_dest.tmp""
+    local curl_command_cfg="curl -s \"$cfg_url\" -o \"$cfg_dest.tmp\""
+    local curl_command_presets="curl -s \"$presets_url\" -o \"$presets_dest.tmp\""
🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 105-105: The surrounding quotes actually unquote this. Remove or escape them.

(SC2027)


[warning] 106-106: The surrounding quotes actually unquote this. Remove or escape them.

(SC2027)


115-128: Firmware Update Function – Quotation in cURL Command Needs Correction
On line 125 in update_one, the command string is not properly quoted, which may lead to issues with files or URLs containing spaces. Applying proper escaping will make the command robust. For example:

-    local curl_command="curl -s -X POST -F "file=@$firmware" "$url""
+    local curl_command="curl -s -X POST -F \"file=@$firmware\" \"$url\""
🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 125-125: The surrounding quotes actually unquote this. Remove or escape them.

(SC2027)


181-242: Command Execution Block Appears Consistent
The switch-case block executing the chosen command (discover, backup, or update) is logically structured and handles errors (e.g., missing targets) appropriately. Ensure that the assumptions (e.g., defaulting to port 80 for a target) are documented if they are intended design decisions.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e76e9a3 and 8a6e3a9.

📒 Files selected for processing (1)
  • tools/wled-tools.sh (1 hunks)
🧰 Additional context used
🪛 Shellcheck (0.10.0)
tools/wled-tools.sh

[warning] 4-4: GREEN appears unused. Verify use (or export if used externally).

(SC2034)


[warning] 105-105: The surrounding quotes actually unquote this. Remove or escape them.

(SC2027)


[warning] 106-106: The surrounding quotes actually unquote this. Remove or escape them.

(SC2027)


[warning] 125-125: The surrounding quotes actually unquote this. Remove or escape them.

(SC2027)

🔇 Additional comments (1)
tools/wled-tools.sh (1)

11-25: Logging Function Implementation Looks Good
The log function correctly handles conditional terminal output based on the quiet flag and whether the output is a terminal.

Comment on lines 143 to 177
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
show_help
exit 0
;;
-t|--target)
target="$2"
shift 2
;;
-D|--discover)
discover=true
shift
;;
-d|--directory)
backup_dir="$2"
shift 2
;;
-f|--firmware)
firmware_file="$2"
shift 2
;;
-q|--quiet)
quiet=true
shift
;;
backup|update|discover)
command="$1"
shift
;;
*)
log "ERROR" "$RED" "Unknown argument: $1"
exit 1
;;
esac
Copy link

@coderabbitai coderabbitai bot Mar 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

❓ Verification inconclusive

Missing Verbose Option Handling in Command-Line Arguments
While the help message (show_help) describes a -v/--verbose option, there is no case handling for it in the argument processing loop. If verbose output is intended, add an option block to set a verbose variable and use it accordingly in your functions (for example, to control additional debug output).


Missing Verbose Option Handling in Command-Line Arguments

The help message clearly references a -v/--verbose option to enable verbose output; however, the current argument parser in tools/wled-tools.sh (lines 143-177) lacks any handling for this flag. To align the implementation with the documentation, please add a case for -v|--verbose that sets a verbose variable (e.g., to true) and then shifts the arguments. For instance:

-v|--verbose)
    verbose=true
    shift
    ;;

This update will ensure that verbose mode can be correctly activated as documented.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is CodeRabbit correct about this?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@netmindz Yes, CodeRabbit is correct about this issue. Looking at the script, there's a -v, --verbose option described in the help text (around line 55), but there's no corresponding case handler in the argument processing section (lines 143-177).

All the other options mentioned in the help text have proper handlers, but the verbose option is missing its implementation. Adding the suggested code would properly implement the verbose flag:

-v|--verbose)
    verbose=true
    shift
    ;;

Additionally, you'd need to initialize the verbose variable at the top of the script where the other option variables are defined (around line 133-137).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CodeRabbit you seem pretty sure... 😄 However someone else needs to verify if you are indeed correct. And I'm not saying you are not.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

♻️ Duplicate comments (1)
tools/wled-tools.sh (1)

142-177: 🛠️ Refactor suggestion

Missing Verbose Option Handling
The PR objectives and help text documentation indicate support for a verbose flag (-v/--verbose), yet no corresponding case exists in the argument parsing block. To enable verbose logging, add handling for this flag (e.g., set a global variable verbose=true) and update the help text to list the verbose option.

Proposed diff for the argument parsing (suggest inserting after the -f|--firmware case and before -q|--quiet):

-        -f|--firmware)
-            firmware_file="$2"
-            shift 2
-            ;;
+        -f|--firmware)
+            firmware_file="$2"
+            shift 2
+            ;;
+        -v|--verbose)
+            verbose=true
+            shift
+            ;;

Proposed diff for updating the help message (inside the heredoc in show_help):

  -f, --firmware <File>   Specify the firmware file for updating devices.
+  -v, --verbose           Enable verbose output for debugging.
  -q, --quiet             Suppress logging output (also makes discover output hostnames only).
🧹 Nitpick comments (4)
tools/wled-tools.sh (4)

4-8: Unused Variable Warning: GREEN
The variable GREEN is defined at the top but is never referenced in the script. If you plan to use it in future logging or other features, consider adding an inline comment for clarity; otherwise, removing it could help clean up the code.

🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 4-4: GREEN appears unused. Verify use (or export if used externally).

(SC2034)


104-105: Fix Quoting in Curl Command Construction for Backup
The curl command strings in the backup_one function use inner quotes without escaping, which may lead to unexpected behavior or shellcheck warnings. Using escaped quotes will ensure the URL and destination paths are correctly interpreted.

-    local curl_command_cfg="curl -s "$cfg_url" -o "$cfg_dest.tmp""
-    local curl_command_presets="curl -s "$presets_url" -o "$presets_dest.tmp""
+    local curl_command_cfg="curl -s \"$cfg_url\" -o \"$cfg_dest.tmp\""
+    local curl_command_presets="curl -s \"$presets_url\" -o \"$presets_dest.tmp\""
🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 104-104: The surrounding quotes actually unquote this. Remove or escape them.

(SC2027)


[warning] 105-105: The surrounding quotes actually unquote this. Remove or escape them.

(SC2027)


124-124: Fix Quoting in Curl Command for Firmware Update
The curl command in the update_one function is also affected by unescaped inner quotes. Escaping these quotes correctly will fix potential issues with command interpretation.

-    local curl_command="curl -s -X POST -F "file=@$firmware" "$url""
+    local curl_command="curl -s -X POST -F \"file=@$firmware\" \"$url\""
🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 124-124: The surrounding quotes actually unquote this. Remove or escape them.

(SC2027)


107-111: Enhance Backup Error Handling
In the backup_one function, both curl calls are executed without checking their exit statuses. To prevent partial backups (e.g., renaming temporary files even if one download fails), consider verifying that each curl_handler call succeeds before moving the temporary files. For example:

if ! curl_handler "$curl_command_cfg" "$hostname"; then
    log "ERROR" "$RED" "Failed to backup config for $hostname."
    return 1
fi
if ! curl_handler "$curl_command_presets" "$hostname"; then
    log "ERROR" "$RED" "Failed to backup presets for $hostname."
    return 1
fi
mv "$cfg_dest.tmp" "$cfg_dest"
mv "$presets_dest.tmp" "$presets_dest"

This additional check will help ensure data integrity during the backup process.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8a6e3a9 and 6f54827.

📒 Files selected for processing (1)
  • tools/wled-tools.sh (1 hunks)
🧰 Additional context used
🪛 Shellcheck (0.10.0)
tools/wled-tools.sh

[warning] 4-4: GREEN appears unused. Verify use (or export if used externally).

(SC2034)


[warning] 104-104: The surrounding quotes actually unquote this. Remove or escape them.

(SC2027)


[warning] 105-105: The surrounding quotes actually unquote this. Remove or escape them.

(SC2027)


[warning] 124-124: The surrounding quotes actually unquote this. Remove or escape them.

(SC2027)

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: wled_build / Build Environments (usermods)
  • GitHub Check: wled_build / Build Environments (esp32s3dev_8MB_opi)

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🔭 Outside diff range comments (1)
tools/wled-tools (1)

1-241: 🛠️ Refactor suggestion

Consider adding verbose mode for additional debugging information.

The script defines a -v or --verbose option in the help text, but doesn't implement it in the code. Consider adding a verbose mode that provides additional details during execution:

# Command-line arguments processing
command=""
target=""
discover=false
quiet=false
+verbose=false
backup_dir="./"
firmware_file=""

# ... (in the argument processing section)
+       -v|--verbose)
+           verbose=true
+           shift
+           ;;

# ... (modify the log function)
log() {
    local category="$1"
    local color="$2"
    local text="$3"
+   local level="$4"

+   # Skip debug messages unless verbose mode is enabled
+   if [ "$level" = "DEBUG" ] && [ "$verbose" != true ]; then
+       return
+   fi

    if [ "$quiet" = true ]; then
        return
    fi

    # ... (rest of the function)
}

# ... (then use it for debug logging)
+if [ "$verbose" = true ]; then
+    log "DEBUG" "$BLUE" "Using target: $target" "DEBUG"
+fi
🧹 Nitpick comments (4)
tools/wled-tools (4)

1-9: The script should have executable permissions.

Make sure to add executable permissions to the script file:

chmod +x tools/wled-tools

Additionally, consider renaming the file to match its mention in the PR as "wled-tools.sh" for consistency.


27-47: Consider adding more robust error handling in the curl_handler function.

The curl_handler function has good error handling, but could be improved:

  1. Add error handling for malformed URLs
  2. Consider capturing and logging the actual response body on error
  3. Add timeout handling to prevent hanging on unresponsive devices
curl_handler() {
    local command="$1"
    local hostname="$2"
+   local timeout=10
    
-   response=$($command -w "%{http_code}" -o /dev/null)
+   response=$($command -m $timeout -w "%{http_code}" -o /tmp/curl_error.$$)
    curl_exit_code=$?
    
    if [ "$response" -ge 200 ] && [ "$response" -lt 300 ]; then
+       rm -f /tmp/curl_error.$$
        return 0
    elif [ $curl_exit_code -ne 0 ]; then
-       log "ERROR" "$RED" "Connection error during request to $hostname (curl exit code: $curl_exit_code)."
+       log "ERROR" "$RED" "Connection error during request to $hostname (curl exit code: $curl_exit_code). $(cat /tmp/curl_error.$$)"
+       rm -f /tmp/curl_error.$$
        return 1
    elif [ "$response" -ge 400 ]; then
-       log "ERROR" "$RED" "Server error during request to $hostname (HTTP status code: $response)."
+       log "ERROR" "$RED" "Server error during request to $hostname (HTTP status code: $response). $(cat /tmp/curl_error.$$)"
+       rm -f /tmp/curl_error.$$
        return 2
    else
-       log "ERROR" "$RED" "Unexpected response from $hostname (HTTP status code: $response)."
+       log "ERROR" "$RED" "Unexpected response from $hostname (HTTP status code: $response). $(cat /tmp/curl_error.$$)"
+       rm -f /tmp/curl_error.$$
        return 3
    fi
}

114-127: Improve update_one function with better feedback and error handling.

The update function should provide better feedback to the user about the update progress and result:

update_one() {
    local hostname="$1"
    local address="$2"
    local port="$3"
    local firmware="$4"

    log "INFO" "$YELLOW" "Starting firmware update for device: $hostname ($address:$port)"

    local url="http://$address:$port/update"
-   local curl_command="curl -s -X POST -F "file=@$firmware" "$url""
+   local curl_command="curl -s -X POST -F "file=@$firmware" "$url" -o /tmp/update_result.$$"

-   curl_handler "$curl_command" "$hostname"
+   if curl_handler "$curl_command" "$hostname"; then
+       log "INFO" "$GREEN" "Firmware update initiated successfully for $hostname"
+       log "INFO" "$BLUE" "Device may reboot and be temporarily unavailable"
+       return 0
+   else
+       log "ERROR" "$RED" "Failed to update firmware for $hostname"
+       return 1
+   fi
}

197-211: Add validation for the backup directory in the backup command.

The script should verify that the backup directory is writable:

backup)
    if [ -n "$target" ]; then
+       # Check if backup directory is writable
+       if ! [ -d "$backup_dir" ] && ! mkdir -p "$backup_dir" 2>/dev/null; then
+           log "ERROR" "$RED" "Backup directory '$backup_dir' doesn't exist and cannot be created"
+           exit 1
+       fi
+       if ! [ -w "$backup_dir" ]; then
+           log "ERROR" "$RED" "Backup directory '$backup_dir' is not writable"
+           exit 1
+       fi
        # Assume target is both the hostname and address, with port 80
        backup_one "$target" "$target" "80"
    elif [ "$discover" = true ]; then
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6f54827 and dc5eaf3.

📒 Files selected for processing (1)
  • tools/wled-tools (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: wled_build / Build Environments (esp32s3_4M_qspi)
  • GitHub Check: wled_build / Build Environments (usermods)
  • GitHub Check: wled_build / Build Environments (esp32s3dev_16MB_opi)
  • GitHub Check: wled_build / Build Environments (lolin_s2_mini)
🔇 Additional comments (3)
tools/wled-tools (3)

11-25: Good logging implementation with color support and terminal detection.

The logging function properly handles colored output for terminals and plain text for non-terminal output. It also respects the quiet flag.


215-219: Good validation of firmware file existence.

The check that ensures the firmware file exists before proceeding with the update is a good practice.


1-241: Overall this is a well-structured and useful utility script.

The script provides valuable functionality for managing WLED devices. It has good error handling, clear help text, and a well-designed command structure. With the suggested improvements to robustness and user feedback, it will be an even more valuable tool for WLED users.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (4)
tools/wled-tools (4)

1-2: Add file permissions to make the script executable.

The script should have executable permissions. After adding the file to the repository, you should run:

chmod +x tools/wled-tools

1-2: Consider adding version information to the script.

Including version information would help track changes and compatibility.

#!/bin/bash

+# WLED Tools v1.0.0
+# A utility for managing WLED devices in a local network
+# https://github.com/wled/WLED

134-136: Add a Content-Type header to the firmware update request.

For better compatibility, explicitly set the Content-Type header for the multipart form data.

local url="http://$address:$port/update"
-local curl_command="curl -s -X POST -F "file=@$firmware" "$url""
+local curl_command="curl -s -X POST -H 'Content-Type: multipart/form-data' -F "file=@$firmware" "$url""

49-68: Add examples to the help section.

Including usage examples in the help text would make it easier for users to understand how to use the tool.

show_help() {
    cat << EOF
Usage: wled-tools.sh [OPTIONS] COMMAND [ARGS...]

Options:
  -h, --help              Show this help message and exit.
  -t, --target <IP/Host>  Specify a single WLED device by IP address or hostname.
  -D, --discover          Discover multiple WLED devices using mDNS.
  -d, --directory <Path>  Specify a directory for saving backups (default: working directory).
  -f, --firmware <File>   Specify the firmware file for updating devices.
  -q, --quiet             Suppress logging output (also makes discover output hostnames only).

Commands:
  backup      Backup the current state of a WLED device or multiple discovered devices.
  update      Update the firmware of a WLED device or multiple discovered devices.
  discover    Discover WLED devices using mDNS and list their IP addresses and names.

+Examples:
+  # Discover all WLED devices on the network
+  ./wled-tools.sh discover
+
+  # Backup a specific WLED device
+  ./wled-tools.sh -t 192.168.1.100 backup
+
+  # Backup all discovered WLED devices to a specific directory
+  ./wled-tools.sh -D -d /path/to/backups backup
+
+  # Update firmware on all discovered WLED devices
+  ./wled-tools.sh -D -f /path/to/firmware.bin update
EOF
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dc5eaf3 and 646ec30.

📒 Files selected for processing (1)
  • tools/wled-tools (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: wled_build / Build Environments (usermods)
🔇 Additional comments (3)
tools/wled-tools (3)

71-86: Fix hostname parsing to handle spaces in device names.

The current parsing method assumes space-delimited output, but device hostnames might contain spaces, which would break the parsing logic.

Apply this fix to handle delimitation properly:

-   mapfile -t raw_devices < <(avahi-browse _wled._tcp --terminate -r -p | awk -F';' '/^=/ {print $7, $8, $9}')
+   # Use a unique delimiter unlikely to appear in hostnames (unit separator character)
+   mapfile -t raw_devices < <(avahi-browse _wled._tcp --terminate -r -p | awk -F';' '/^=/ {print $7"\x1F"$8"\x1F"$9}')

    local devices_array=()
    for device in "${raw_devices[@]}"; do
-       read -r hostname address port <<< "$device"
+       IFS=$'\x1F' read -r hostname address port <<< "$device"
        devices_array+=("$hostname" "$address" "$port")
    done

107-117: Excellent implementation of error handling in the backup function.

The code properly checks the curl operation result and handles errors well. It correctly removes temporary files on failure and provides appropriate error messages.


160-163: Well-implemented parameter validation.

The validation of command-line parameters that require arguments is properly implemented as suggested in a previous review.

Also applies to: 172-175, 180-183

LordMike and others added 3 commits April 6, 2025 01:11
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@blazoncek
Copy link
Collaborator

Thank you.
Would it be possible to add counterparts for Windows and Mac computers?
Some of the Linux tools are unavailable on mac even with Homebrew.

@LordMike
Copy link
Contributor Author

LordMike commented Apr 6, 2025 via email

@blazoncek
Copy link
Collaborator

I will be able to lend support/testing on mac.

@LordMike
Copy link
Contributor Author

LordMike commented Apr 6, 2025 via email

Copy link

coderabbitai bot commented Apr 6, 2025

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check if the wled-tools.sh script exists and examine its content
cat tools/wled-tools.sh

Length of output: 77


🏁 Script executed:

#!/bin/bash
# Search for the wled-tools.sh script in the repository
fd -t f "wled-tools.sh" 

# Let's also check the PR itself to see the file changes
git show HEAD

Length of output: 69658


🏁 Script executed:

#!/bin/bash
# Let's locate the wled-tools.sh file first
fd -t f "wled-tools.sh" 

Length of output: 23


🏁 Script executed:

#!/bin/bash
# Let's try to get the PR details to see the files changed
git show --pretty="" --name-only HEAD

# Alternatively, let's try to find the file using a broader search
find . -name "wled-tools.sh"

# Let's check the PR files through GitHub CLI if available
gh pr view 4625 --json files

Length of output: 20322


🏁 Script executed:

#!/bin/bash
# Let's examine the content of the tools/wled-tools file
cat tools/wled-tools

Length of output: 8561


@LordMike You're absolutely right. I've checked the current version of the wled-tools.sh script, and there's no mention of a verbose flag in either the help text or the argument processing section. The script is consistent and doesn't contain any -v or --verbose option.

The previous comment was incorrect, and I apologize for the confusion. This might have happened due to the multiple pushes in a short timeframe as you mentioned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants