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

feature: print output instead of executing #25

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ When commands aren't double quoted, you need to escape semicolon with slash `\;`
fenv "source ~/.nvm/nvm.sh; nvm --help"
```

You can also choose to print the output instead of setting the environment:

```fish
fenv -p export PYTHON=python2
```

will print

```fish
set -g -x PYTHON python2
```

instead of executing the command. This can be used to translate a foreign environment file to a native fish file.


# Caveats

Expand Down
10 changes: 9 additions & 1 deletion functions/fenv.fish
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ function fenv -d "Run bash scripts and import variables modified by them"
return 0
end

fenv.main $argv
argparse 'p/print' -- $argv

if test -n $_flag_print
set operation "print"
else
set operation "apply"
end

fenv.main $operation $argv
return $status
else
echo (set_color red)'error:' (set_color normal)'parameter missing'
Expand Down
13 changes: 10 additions & 3 deletions functions/fenv.main.fish
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
# SOFTWARE.


function fenv.main
set program $argv
function fenv.main -a operation -a rest
set program $rest
set divider (fenv.parse.divider)
set previous_env (bash -c 'env')

Expand All @@ -45,7 +45,14 @@ function fenv.main

set new_env (fenv.parse.after $program_execution)

fenv.apply (fenv.parse.diff $previous_env $divider $new_env)
if [ $operation = "apply" ]
fenv.apply (fenv.parse.diff $previous_env $divider $new_env)
else if [ $operation = "print" ]
fenv.print (fenv.parse.diff $previous_env $divider $new_env)
else
printf "Invalid command: %s\n" $operation
return $program_status
end

test (count $program_output) -gt 1
and printf "%s\n" $program_output[1..-2]
Expand Down
18 changes: 18 additions & 0 deletions functions/fenv.print.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function fenv.print
set variables $argv

set output ()

for variable in $variables
set key (echo $variable | sed 's/=.*//')
set value (echo $variable | sed 's/[^=]*=//')

if test "$key" = 'PATH'
set value (echo $value | tr ':' '\n')
end

set -a output "set -g -x $key $value"
end

string join \n $output
end