1
0
mirror of https://github.com/hustcer/deepseek-review.git synced 2026-05-13 05:16:05 +08:00

refactor: A better from env parser (#194)

* refactor: A better `from env` parser

* refactor: A better `from env` parser
This commit is contained in:
Justin Ma
2025-11-29 22:45:55 +08:00
committed by GitHub
parent edcd90e48b
commit 0c001b0ee2
2 changed files with 55 additions and 56 deletions

View File

@@ -37,7 +37,7 @@ jobs:
As a senior Nushell engineer, perform comprehensive script review with focus on: As a senior Nushell engineer, perform comprehensive script review with focus on:
### 1. Core Requirements: ### 1. Core Requirements:
- Validate Nu 0.90+ compatibility - Validate Nu 0.100+ compatibility
- Check structured data handling - Check structured data handling
- Verify pipeline efficiency - Verify pipeline efficiency
- Assess module organization - Assess module organization
@@ -55,7 +55,7 @@ jobs:
- Parallel execution opportunities - Parallel execution opportunities
**Rules:** **Rules:**
- Target Nu 0.90+ features - Target Nu 0.100+ features
- Highlight data flow vulnerabilities - Highlight data flow vulnerabilities
- Suggest structured data optimizations - Suggest structured data optimizations
- Keep feedback Nu-specific - Keep feedback Nu-specific

View File

@@ -84,61 +84,60 @@ export def check-nushell [--debug] {
# May be used like this: open .env | load-env # May be used like this: open .env | load-env
# Works with quoted and unquoted .env files # Works with quoted and unquoted .env files
export def "from env" []: string -> record { export def "from env" []: string -> record {
lines let input = $in
| where { |line| not ($line | str trim | str starts-with '#') }
| parse "{key}={value}" # Process escape sequences in double-quoted values using str replace chain
| update key { |row| # Use NUL char as placeholder to avoid replacement conflicts
$row.key | str trim | str replace -r '^export\s+' '' let process_escapes = {|content: string|
} $content
| update value { |row| | str replace -a '\\' (char nul) # Placeholder for \\ to avoid conflicts
let val = ($row.value | str trim)
match $val {
# Handle double-quoted values
$v if ($v | str starts-with '"') => {
let matched = ($v | parse -r '^"(?P<content>(?:[^"\\]|\\.)*)"')
if ($matched | is-empty) {
$v | str trim -c '"'
} else {
$matched | get 0.content
| str replace -a '\n' (char nl) | str replace -a '\n' (char nl)
| str replace -a '\r' (char cr) | str replace -a '\r' (char cr)
| str replace -a '\t' (char tab) | str replace -a '\t' (char tab)
| str replace -a '\"' '"' | str replace -a '\"' '"'
| str replace -a (char nul) '\' # Restore \\ to single \
}
# Parse double-quoted value with escape sequence support
let parse_double_quoted = {|val: string|
let matched = ($val | parse -r '^"(?P<content>(?:[^"\\]|\\.)*)"')
if ($matched | is-empty) { $val | str trim -c '"' } else { do $process_escapes $matched.0.content }
}
# Parse single-quoted value (no escape processing)
let parse_single_quoted = {|val: string|
let matched = ($val | parse -r "^'(?P<content>[^']*)'")
if ($matched | is-empty) { $val | str trim -c "'" } else { $matched.0.content }
}
# Parse unquoted value: handle escaped hash (\#) and strip inline comments
let parse_unquoted = {|val: string|
$val
| str replace -a '\#' (char nul) # Placeholder for \#
| split row '#' # Split by comment delimiter
| first # Take content before first #
| str replace -a (char nul) '#' # Restore \# to #
| str trim
}
# Parse value based on its format
let parse_value = {|val: string|
match $val {
$v if ($v | str starts-with '"') => { do $parse_double_quoted $v }
$v if ($v | str starts-with "'") => { do $parse_single_quoted $v }
_ => { do $parse_unquoted $val }
} }
} }
# Handle single-quoted values
$v if ($v | str starts-with "'") => { let parsed = $input | lines
let match = ($v | parse -r "^'(?P<content>[^']*)'") | str trim
if ($match | is-empty) { $v | str trim -c "'" } else { $match | get 0.content } | compact -e
} | where {|line| not ($line | str starts-with '#') }
# Handle unquoted values | parse "{key}={value}"
_ => { | update key {|row| $row.key | str trim | str replace -r '^export\s+' '' }
let chars = ($val | split chars) | update value {|row| do $parse_value ($row.value | str trim) }
let total = ($chars | length)
mut idx = 0 if ($parsed | is-empty) { {} } else { $parsed | transpose -r -d -l }
mut acc = ''
while $idx < $total {
let ch = ($chars | get $idx)
if $ch == "\\" {
let next = ($chars | get -o ($idx + 1))
if $next == '#' {
$acc = $acc + '#'
$idx = $idx + 2
continue
}
$acc = $acc + $ch
$idx = $idx + 1
continue
}
if $ch == '#' { break }
$acc = $acc + $ch
$idx = $idx + 1
}
$acc | str trim
}
}
}
| transpose -r -d
} }
# Compact the record by removing empty columns # Compact the record by removing empty columns