diff --git a/srcenv b/srcenv index 5ce1c90..79bcb98 100755 --- a/srcenv +++ b/srcenv @@ -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 @@ -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" + (['; @@ -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:-{}}"} \ diff --git a/srcenv.1 b/srcenv.1 index da0d328..fcf6a75 100644 --- a/srcenv.1 +++ b/srcenv.1 @@ -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 diff --git a/srcenv.1.md b/srcenv.1.md index 5d0d28f..08f2593 100644 --- a/srcenv.1.md +++ b/srcenv.1.md @@ -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. diff --git a/srcenv.tests b/srcenv.tests index 5fcc7f8..89d872f 100755 --- a/srcenv.tests +++ b/srcenv.tests @@ -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' @@ -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" @@ -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" "" @@ -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" "" @@ -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 @@ -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" @@ -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