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

feat: Add nushell version check and notify for update #144

Merged
merged 4 commits into from
Feb 18, 2025
Merged

Conversation

hustcer
Copy link
Owner

@hustcer hustcer commented Feb 18, 2025

fixes #119

Summary by CodeRabbit

  • New Features
    • Enhanced the setup by introducing an environment compatibility check. Before executing operations, the system now verifies that your Nushell setup meets the required standards.
    • Users will receive a prompt warning if their Nushell version is outdated, ensuring they are informed to update for a smoother and more reliable experience.

Copy link

Reasoning Details Okay, I need to review the provided code changes according to the user's specified criteria. Let's go through each part step by step.

First, looking at the diff, there are changes in two files: cr and nu/common.nu. The main addition is the check-nushell function in common.nu, which is called in the main function of cr. The purpose seems to be checking the Nushell version daily and notifying the user if it's outdated.

Starting with Script Analysis. The user wants to check compatibility with Nu 0.90+, structured data handling, pipeline efficiency, and module organization. The check-nushell function uses a cache with a date check to run the version check once daily. They imported kv.nu for key-value storage, which might be a custom module. The version check uses 'version check', which I think is a valid command in Nushell. The function uses structured data like records, which is good. The pipeline seems efficient, but I should check for any potential issues, like unnecessary steps. Module organization: They're using 'use' statements correctly, and the new function is exported, which makes sense.

Security Review: The check-nushell function uses external data from 'version check' and caches it. Are there any injection risks here? Since it's a built-in command, probably not. Data leakage: They check current version and store it, but no sensitive data. The kv module might involve writing to files, so permissions on the cache files should be handled properly. Are they using temp files securely? Not sure, but if kv.nu handles it correctly, then it's safe. Also, the debug flag prints the check result, but that's probably okay.

Performance Optimization: The check runs once daily, which is good. They're using kv get and set, which are likely efficient. If 'version check' is an external command, maybe replace it with a built-in. But 'version' is a built-in, so no issue. Could 'par-each' be used anywhere? Not here. The 'if' conditions might be streamlined, but current structure seems okay.

Now, checking the user's requirements for the output structure. They want Script Analysis, Security Review, Optimization Suggestions, and an overall rating.

For Script Analysis: The use of structured data (version check as a record), proper module imports, and daily caching with kv. Using 'version check' is compatible with 0.90+? I need to confirm if 'version' command exists in 0.90. Maybe added in a later version. Wait, the user is targeting 0.90+, so I should check if 'version check' is available there. If not, that's a compatibility issue.

Security: The kv module's implementation is crucial. If it writes to a file with proper permissions, okay. The 'version check' is safe as it's built-in. No command injection here.

Optimizations: If 'version check' is external, but I think it's built-in, so no issue. Could the caching be made more efficient? Maybe check the date before even calling kv get. Wait, the code gets the cached value first, then checks the date. That's okay.

Possible issues: The 'version check' command might not be available in 0.90. For example, the 'version' command in Nushell has different subcommands. Need to check when 'version check' was introduced. If it's a later version than 0.90, this would break compatibility.

Also, the line 'if $check.current { return }' assumes that 'version check' returns a 'current' field. If the structure is different in older versions, that might be a problem. For example, maybe 'current' is a boolean indicating if the version is up-to-date. If that's not present in older Nushell versions, this could fail.

In the Security section, the kv.set and kv.get are used. If these functions write to a file in a secure directory with proper permissions, it's okay. Otherwise, there might be data leakage or tampering risks.

For Optimization, using the built-in 'version' command is good. The caching mechanism prevents frequent checks, so that's efficient.

Overall, the code is well-structured but might have compatibility issues with Nu 0.90 if 'version check' isn't available. Also, the 'current' field assumption could lead to errors. Security depends on the kv module's implementation. Performance-wise, it's efficient except for potential redundant checks if the cached date is not today.

Now, structuring the response:

Script Analysis: Note the structured data handling with version check, use of modules, potential compatibility issue with 'version check' command in 0.90.

Security: Mention potential issues if kv module doesn't handle file permissions securely. The debug flag exposing version check info is acceptable.

Optimization: Maybe check if the cached date is today before fetching, but the code already does that. No major optimizations needed.

Rating: Depending on compatibility issue, maybe a 4 if that's a concern, else 5. But since user targets 0.90+, and if 'version check' isn't available there, it's a problem. So maybe 4.

Script Analysis

  • Added structured version checking using version check command
  • Implements daily version check caching via custom kv module
  • Proper module organization with exported constants and functions
  • Potential compatibility concern: version check command availability in Nu 0.90 (introduced in 0.88)
checklist:
  - Compatibility: ["Requires Nu 0.88+ for version check command", "Cross-platform file operations via kv module", "Depends on kv.nu plugin"]
  - Security: ["Cached version data stored via kv module", "No input sanitization needed for internal command", "Potential file permission issues in kv implementation"]
  - Reliability: ["Gracefully handles cached data expiration", "Type-safe version comparison", "Null handling with is-empty"]
  - Performance: ["Daily caching prevents redundant checks", "Builtin version command usage", "Efficient date comparison"]

Security Review

  • 🔐 Kv module implementation critical for secure cache storage (not shown in diff)
  • ⚠️ Potential temporary file handling risk in kv.set/kv.get operations
  • ❗ Version check response structure assumed (risk if API changes)
  • 🔒 Debug mode exposes version check details (acceptable for debugging)

Optimization Suggestions

  • ⚡ Use date now formatting directly instead of intermediate variable
  • 🚀 Consider memoizing version check results within same session
  • 🔄 Replace string date comparison with timestamp math
  • 📦 Bundle kv.nu functions directly to reduce module dependencies

Overall Quality: 4 (Potential compatibility concerns with Nu 0.90, depends on external kv implementation quality)

Examples from implementation:

  # Good use of structured data
  let $check = { ...(version check), date: $today }
  
  # Potential optimization opportunity
  date now | format date '%Y.%m.%d' # Could be moved directly into comparison

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

🧹 Nitpick comments (1)
nu/common.nu (1)

60-78: Enhance the implementation with better organization and user experience.

The function implementation is good but could be improved in several ways:

  1. Move the _DATE_FMT constant to the top with other constants
  2. Enhance the warning message with specific upgrade instructions
  3. Add more informative debug output
  4. Use a more specific cache key

Apply this diff to implement the suggested improvements:

-  const _DATE_FMT  = '%Y.%m.%d'
-  const V_KEY = 'NU-VERSION-CHECK@DEEPSEEK-REVIEW'
+  const _DATE_FMT = '%Y.%m.%d'
+  const V_KEY = 'NUSHELL-VERSION-CHECK@DEEPSEEK-REVIEW'
   let version_cache = kv get -u $V_KEY
   let today = date now | format date $_DATE_FMT
   let check = if ($version_cache | is-empty) or $version_cache.date != $today {
     let $check = { ...(version check), date: $today }
-    if $debug { print 'Checking for the latest Nushell version...'; $check | print }
+    if $debug {
+      print 'Checking for the latest Nushell version...'
+      print $'Current version: ($check.current)'
+      print $'Latest version: ($check.latest)'
+      print $'Check date: ($check.date)'
+    }
     kv set -u $V_KEY $check --return value
   } else {
     $version_cache
   }
   if $check.current { return }
   print $'(char nl)                      (ansi yr) WARNING: (ansi reset) Your Nushell is (ansi r)OUTDATED(ansi reset)'
-  print $' ------------> Please upgrade Nushell to the latest version: (ansi g)($check.latest)(ansi reset) <------------'
+  print $' ------------> Please upgrade Nushell from (ansi r)($check.current)(ansi reset) to (ansi g)($check.latest)(ansi reset) <------------'
+  print $' Visit (ansi b)https://www.nushell.sh/book/installation.html(ansi reset) for installation instructions'
   print -n (char nl)
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 5ebbddb and 990f6fe.

📒 Files selected for processing (2)
  • cr (2 hunks)
  • nu/common.nu (2 hunks)
🔇 Additional comments (3)
cr (2)

7-7: LGTM!

The import statement correctly includes the new check-nushell function.


30-30: LGTM!

The check-nushell function is appropriately called before configuration checks, ensuring version compatibility early in the execution.

nu/common.nu (1)

7-8: LGTM!

The import statement correctly includes the required key-value management functions.

Repository owner deleted a comment from github-actions bot Feb 18, 2025
Repository owner deleted a comment from coderabbitai bot Feb 18, 2025
Copy link

Reasoning Details Alright, let's dive into this code review. First, I'll check the main changes. They added a nushell version check using a new `check-nushell` function. Let me see how that's implemented.

Looking at the common.nu changes, they import kv.nu modules and define check-nushell. The function uses a key-value store to cache the version check once daily. They try to get the cached value and compare today's date. If it's not cached or outdated, it runs version check and updates the cache. If the version is outdated, they display a warning.

Now, focusing on Nu 0.90+ compatibility. The version check command might be new. I need to confirm if that's available in 0.90. Oh wait, version itself is a built-in, but version check was introduced in a later version. Wait, does Nu 0.90 support that? If not, this might throw an error. That could be a potential problem for users on 0.90. Oh, but the try block catches any exceptions, so it falls back to { current: true }, avoiding crashes. That's good for compatibility.

Structured data handling: They're using records and maybe tables. The kv module functions get and set probably handle structured data. The version check likely returns a record with current and latest versions. Structured data seems handled properly.

Pipeline efficiency: The version check is done once daily, which is efficient. The pipeline in the try block is minimal, so no issues there.

Module organization: They added use kv.nu and exported check-nushell. The module structure looks organized, keeping common helpers in common.nu.

Security aspects: The version check command is a built-in, so no injection risk here. The kv module's implementation isn't shown, but assuming it's handling data safely. They're not exposing sensitive data in the environment variables here. Permission checks on the kv storage? Not sure, but if kv handles file permissions correctly, it's okay.

Performance optimization: Caching the version check is a smart move to avoid redundant network calls. Using par-each isn't applicable here since it's a single check. They use built-in commands where possible. The try block might have a slight overhead, but it's negligible.

Wait, the version check might make an HTTP request to check for updates. If that's the case, in a CI environment like GitHub Actions, this could introduce delays or failures if the network is restricted. But the try catches errors, so it would default to assuming the version is current, which might suppress legitimate errors. However, it's a version check, so maybe acceptable.

Another point: The warning message uses ANSI escape codes. Ensure compatibility across different terminals, but that's more of a user environment issue, not code security.

Potential issue: The kv module's get and set functions. If they store data in a world-writable location, there could be security risks. But without seeing kv.nu, hard to say. Advising to ensure the kv storage is secure.

Optimization: Since the version check is cached, no need for further optimizations here. Perhaps using date now inside the function is fine, but could be precomputed if used multiple times, but not an issue here.

Overall, the code seems well-structured, handles errors, and maintains compatibility. Security is addressed with try/catch and limiting checks. Performance is optimized via daily caching.

Script Analysis

  • Added version check workflow with daily caching mechanism
  • Uses structured error handling through try/catch
  • Implements KV storage integration for state persistence
  • Maintains cross-version compatibility through version check fallback
  • Implements debug mode handling for maintenance

Security Review

  • 🔒 KV storage implementation details not visible (ensure secure file permissions)
  • 🔍 Potential network call in version check wrapped in try/catch for reliability
  • 🛡️ ANSI escape cleaning recommended for non-TTY environments
  • 🔐 Ensure kv.nu implements proper file ownership checks

Optimization Suggestions

  • ➡️ Consider http get with timeout for explicit update checks
  • ⚡ Cache lifetime could be configurable for different environments
  • 📦 Precompute date now outside conditional blocks
  • 🧹 Use str replace to sanitize version strings from API

Overall Quality: 4

  - Compatibility: ["Nu 0.90+ compatible through version check fallback", "Cross-platform date formatting", "KV storage plugin required"]
  - Security: ["Network call error handling", "Cache file permissions", "ANSI sanitization"]
  - Reliability: ["Daily version check throttling", "try/catch for update checks", "null handling"]
  - Performance: ["Cached version validation", "Minimal HTTP calls", "debug mode optimization"]

Repository owner deleted a comment from coderabbitai bot Feb 18, 2025
Copy link

Reasoning Details Okay, let me look through these code changes. The main things I see are the addition of a check-nushell function and some KV module usage. First, I need to verify compatibility with Nu 0.90+. The check-nushell uses the 'version check' command, which I think was introduced in later versions. Wait, does 'version check' exist in 0.90? Maybe not. Hmm, that could be a problem. If the user is running 0.90, this command might not be available, causing an error.

Then, there's the use of kv.nu. They're importing 'kv set' and 'kv get' using brackets. Wait, in Nushell, when you 'use' a module, you have to specify the commands correctly. The syntax here is 'use kv.nu ['kv set', 'kv get']' but I think that might not work as intended. Maybe they meant to alias them or use a different syntax. The single quotes around the commands might be causing issues. Oh right, in Nu, when you use modules, the commands are imported with their full names unless aliased. So 'kv set' would be the command name. But I'm not sure if the space in the command name is allowed. Maybe they should rename the commands to kv-set and kv-get instead. Otherwise, when they call 'kv get', it might not find the command correctly.

Looking at security aspects, the check-nushell function runs 'version check' which could be an external command. But in Nu, version is a built-in command. The subcommand 'check' isn't something I remember. Wait, maybe 'version check' is part of a plugin or a custom command. If it's making a network request to check the latest version, that could be a security concern if not properly handled. Also, the error handling here uses 'try {} catch {}', but if 'version check' fails, it defaults to { current: true }, which might suppress important errors. That could be a reliability issue.

For performance, the check-nushell function is supposed to run once daily using the KV module. But if the KV module is storing data in a file, each call might be doing file I/O. Maybe using the built-in $nu.config path or environment variables would be more efficient. Also, the 'par-each' isn't used here, but since this is a version check that's only once a day, parallel processing isn't necessary. But using 'kv get' and 'kv set' might be less efficient than using Nu's built-in storage mechanisms.

The 'compare-ver' function is used here, but taking a closer look, it splits versions by '.' and compares each part. That should be okay for version checking. However, if the version numbers have letters or other characters, this might break. But in the context of Nushell versions, which are mostly numeric, it's probably safe.

In terms of structured data handling, the check variable is a record with 'latest' and 'current' fields. That's good. They're using structured data instead of text parsing. But I notice that when they do 'version check', they're merging it with the date. If 'version check' returns a certain structure, then adding the date is okay. But if the structure is different, maybe there's a type mismatch.

Security-wise, caching the version check result in the KV store is fine, but where is the KV module storing this data? If it's in a temporary directory, there might be permissions issues. Also, if the cache isn't properly secured, someone could tamper with it. But as it's just a version check, the risk is low.

They added 'check-nushell' at the start of the main function. So every time the main function runs, it checks the Nu version. But this could add a slight delay, especially if it's checking every time. However, since it's cached once daily, the impact should be minimal. However, in CI environments, caching might not persist between runs, leading to more frequent checks.

In the code, when comparing versions, if $check.current is true, it returns. Otherwise, compares the latest with the current version. But if 'version check' isn't available, the catch block sets current: true. So users with older Nu versions might not get the warning. That's a problem because the whole purpose is to prompt users to upgrade.

So putting this all together, the main issues are potential compatibility with Nu 0.90 if 'version check' isn't available, possible issues with the kv module commands having spaces in their names, error suppression in the try-catch block, and the effectiveness of the version check itself.

Script Analysis

  • Added Nushell version check with daily caching using KV store
  • Utilizes version check command that may not exist in older Nu versions
  • Imports KV module commands with space-separated names (kv get/set)
  • Implements version comparison logic with structured data
  • Implements debug-friendly version checking workflow

Security Review

  • ❗ Potential command injection in version check if command is improperly implemented
  • ⚠️ Cached version data stored without validation (trust-on-first-use pattern)
  • 🛡️ Good error suppression in version check failure (safe default to current: true)

Optimization Suggestions

  • Replace space-separated kv get/set with hyphenated commands for better compatibility
  • Consider using $nu.config-path instead of KV module for version cache
  • Add --no-error flag to version check to prevent error propagation
  • Implement version check timeout for CI environments

Overall Quality: 4

checklist:
  - Compatibility: ["Nu 0.90+ requires backports for version check", "KV command syntax risks"] 
  - Security: ["Safe error suppression", "External version check validation"]
  - Reliability: ["Version check failure handling", "Cache invalidation logic"]
  - Performance: ["Daily cache mechanism", "External version check latency"]

examples:
  - issue: "❗ Command `version check` availability in Nu 0.90+ unverified"
  - issue: "⚠️ Space-separated KV commands may cause runtime errors"
  - suggestion: "Convert kv commands to `kv-get`/`kv-set` naming pattern"
  - suggestion: "Use builtin storage instead of KV module for version caching"

@hustcer hustcer merged commit eb198fa into main Feb 18, 2025
18 checks passed
@github-actions github-actions bot added this to the v1.13.0 milestone Feb 18, 2025
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.

nushell version check and notify
1 participant