Skip to content

Commit

Permalink
Add srcenv JSON output
Browse files Browse the repository at this point in the history
  • Loading branch information
ins0mniaque committed Nov 26, 2024
1 parent e1866ad commit ee1204d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
5 changes: 4 additions & 1 deletion srcenv
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ ${BOLD}${UNDERLINE}Options${NORMAL}:
${BOLD}--elvish ${NORMAL}Format the output as an Elvish script
${BOLD}--env ${NORMAL}Format the output as a .env file
${BOLD}--fish ${NORMAL}Format the output as a Fish script
${BOLD}--json ${NORMAL}Format the output as JSON
${BOLD}--ksh, --pdksh ${NORMAL}Format the output as a Ksh script
${BOLD}--launchctl ${NORMAL}Format the output as a launchctl calls (macOS)
${BOLD}--murex ${NORMAL}Format the output as a Murex script
Expand Down Expand Up @@ -184,6 +185,7 @@ while [ $# -gt 0 ]; do
--nu|--nushell) fmt '$env.\($k) = \(.[$k]|@sh)' '$env.\($k) = null'; shift ;;
--pwsh|--powershell) fmt '$Env:\($k) = \(.[$k]|@sh)' '$Env:\($k) = $null'; shift ;;
--env) fmt '\($k)=\(.[$k]|@sh)' '\($k)='; shift ;;
--json) format=.; shift ;;
--bat|--cmd) fmt 'set \"\($k)=\(.[$k] | gsub("\n"; "%LF%") | gsub("\""; "^\""))\"' \
'set \($k)=';
prefix='"set LFM=^\n\n\nset LF=^^^%LFM%%LFM%^%LFM%%LFM%\n" + ([';
Expand All @@ -210,7 +212,8 @@ while [ $# -gt 0 ]; do
esac
done

[ -n "$backup" ] && setbackup
[ -n "$backup" ] && setbackup
[ "$format" = . ] && keys=.

jq --argjson snapshot "$snapshot" \
${undo:+--argjson} ${undo:+undo} ${undo:+"${SRCENV_UNDO:-{}}"} \
Expand Down
3 changes: 3 additions & 0 deletions srcenv.1
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ Format the output as a .env file.
\-\-fish
Format the output as a Fish script.
.TP
\-\-json
Format the output as JSON.
.TP
\-\-ksh, \-\-pdksh
Format the output as a Ksh script.
.TP
Expand Down
4 changes: 4 additions & 0 deletions srcenv.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ Options

: Format the output as a Fish script.

\--json

: Format the output as JSON.

\--ksh, \--pdksh

: Format the output as a Ksh script.
Expand Down
27 changes: 27 additions & 0 deletions srcenv.tests
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,27 @@ trap cleanup EXIT INT HUP TERM
lf='
'

{ json_output=$(cat); } << 'EOF'
{
"SRCENV_TEST_ESCAPE": "$NOT_EXPANDED\\t\\r\\n'\"",
"SRCENV_TEST_MULTILINE": "\n6\n7\n42",
"SRCENV_TEST_C": "42",
"SRCENV_TEST_B": "7",
"SRCENV_TEST_A": "6"
}
EOF

{ json_backup_output=$(cat); } << 'EOF'
{
"SRCENV_TEST_ESCAPE": "$NOT_EXPANDED\\t\\r\\n'\"",
"SRCENV_TEST_MULTILINE": "\n6\n7\n42",
"SRCENV_TEST_C": "42",
"SRCENV_TEST_B": "7",
"SRCENV_TEST_A": "6",
"SRCENV_UNDO": "{\"SRCENV_TEST_A\":null,\"SRCENV_TEST_B\":null,\"SRCENV_TEST_C\":null,\"SRCENV_TEST_ESCAPE\":null,\"SRCENV_TEST_MULTILINE\":null,\"SRCENV_UNDO\":null}"
}
EOF

{ env_output=$(cat); } << 'EOF'
SRCENV_TEST_A='6'
SRCENV_TEST_B='7'
Expand Down Expand Up @@ -269,6 +290,7 @@ test "./srcenv --nushell $env_input" "$nu_output"
test "./srcenv --pwsh $env_input" "$pwsh_output"
test "./srcenv --powershell $env_input" "$pwsh_output"
test "./srcenv --env $env_input" "$env_output"
test "./srcenv --json $env_input" "$json_output"

test "./srcenv --ash --backup $env_input" "$posix_output$lf$posix_backup_output"
test "./srcenv --bash --backup $env_input" "$posix_output$lf$posix_backup_output"
Expand All @@ -291,6 +313,7 @@ test "./srcenv --nushell --backup $env_input" "$nu_output$lf$nu_backup_output
test "./srcenv --pwsh --backup $env_input" "$pwsh_output$lf$pwsh_backup_output"
test "./srcenv --powershell --backup $env_input" "$pwsh_output$lf$pwsh_backup_output"
test "./srcenv --env --backup $env_input" "$env_output$lf$env_backup_output"
test "./srcenv --json --backup $env_input" "$json_backup_output"

test "./srcenv --ash --local $env_input" ""
test "./srcenv --bash --local $env_input" ""
Expand All @@ -313,6 +336,7 @@ test "./srcenv --nushell --local $env_input" ""
test "./srcenv --pwsh --local $env_input" ""
test "./srcenv --powershell --local $env_input" ""
test "./srcenv --env --local $env_input" ""
test "./srcenv --json --local $env_input" "{}"

test "./srcenv --ash --local --backup $env_input" ""
test "./srcenv --bash --local --backup $env_input" ""
Expand All @@ -335,6 +359,7 @@ test "./srcenv --nushell --local --backup $env_input" ""
test "./srcenv --pwsh --local --backup $env_input" ""
test "./srcenv --powershell --local --backup $env_input" ""
test "./srcenv --env --local --backup $env_input" ""
test "./srcenv --json --local --backup $env_input" "{}"

test "./srcenv $posix_input" "$env_output"
# test "./srcenv --arg $(cat $posix_input)" "$env_output" # TODO: Fix test framework arg support
Expand All @@ -361,6 +386,7 @@ test "./srcenv --nushell $posix_input" "$nu_output"
test "./srcenv --pwsh $posix_input" "$pwsh_output"
test "./srcenv --powershell $posix_input" "$pwsh_output"
test "./srcenv --env $posix_input" "$env_output"
test "./srcenv --json $posix_input" "$json_output"

test "./srcenv --ash --backup $posix_input" "$posix_output$lf$posix_backup_output"
test "./srcenv --bash --backup $posix_input" "$posix_output$lf$posix_backup_output"
Expand All @@ -383,6 +409,7 @@ test "./srcenv --nushell --backup $posix_input" "$nu_output$lf$nu_backup_outp
test "./srcenv --pwsh --backup $posix_input" "$pwsh_output$lf$pwsh_backup_output"
test "./srcenv --powershell --backup $posix_input" "$pwsh_output$lf$pwsh_backup_output"
test "./srcenv --env --backup $posix_input" "$env_output$lf$env_backup_output"
test "./srcenv --json --backup $posix_input" "$json_backup_output"

end

Expand Down

0 comments on commit ee1204d

Please sign in to comment.