Skip to content

Instantly share code, notes, and snippets.

@RSully
Last active January 18, 2018 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RSully/0dcb20fa6dd41327bfe5a2260ea00f1c to your computer and use it in GitHub Desktop.
Save RSully/0dcb20fa6dd41327bfe5a2260ea00f1c to your computer and use it in GitHub Desktop.
Source *sh environments from fish.
function set_bourne
set --local line $argv[1] # the line X=Y
set --local exp $argv[2] # 1/0: export the variable
set --local key (echo $line | perl -pe 's/^([A-Za-z0-9_]+)=(.*)/$1/g')
set --local dataraw (echo $line | perl -pe 's/^([A-Za-z0-9_]+)=(.*)/$2/g')
set --local data $dataraw
# echo "in set_bourne for $key with $exp"
# echo "old value: $$key"
# string join \n $$key
# echo "dataraw: $dataraw"
# Replace ':' with ' ' for fish arrays
# except for those in the blacklist, FISH_BOURNE_ARRAY_BLACKLIST
# echo "is '$key' in FISH_BOURNE_ARRAY_BLACKLIST? ($FISH_BOURNE_ARRAY_BLACKLIST)"
if not contains $key $FISH_BOURNE_ARRAY_BLACKLIST
# no --local here because that would scope to
# this inner conditional block
# set data (echo $data | tr ':' ' ')
set data (string split ':' $data)
end
# Expand variables
# set --local data (eval echo \"$data\")
# set --local data (eval printf '\%s\n' $data)
eval set --local data "$data"
# echo "final: $data"
# string join \n $data
set --local flags 'g'
if test $exp -eq 1
# handle exports
set flags $flags x
end
eval set "-"(string join '' $flags) $key $data
# echo "result:"
# string join \n $$key
end
function source_bourne
set --local file $argv[1]
# echo "SOURCING $file"
if not test -e $file
echo "error: file does not exist: $file" 1>&2
return 1
end
cat $file | while read line
switch $line
case '#*' ''
continue
case 'alias *'
eval $line
case 'source *'
# TODO: support arguments
set --local data (echo $line | sed 's/^source //')
set --local datafile (eval echo \"$data\")
source_bourne $datafile
case 'export *=*'
set --local line (echo $line | sed 's/^export //')
set_bourne $line 1
case '*=*'
set_bourne $line 0
case '*'
echo "error: unsupported: $line" # 1>&2
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment