Files
comma-scripts/,env

69 lines
1.1 KiB
Ruby
Executable File

#!/usr/bin/env ruby
help = %[
,env [OPTIONS] [file]
Show all environment variables in .env file. Not touching shell.
Please execute "eval $(,env)" if you what export to shell.
OPTIONS
-h --help
print help info
--unset
unset all environments in target file
file
default is .env
]
file = ".env"
is_unset = false
for arg in ARGV
case arg
when "-h", "--help"
puts help
exit
when "--unset"
is_unset = true
else
file = arg
end
end
unless File.exist? file
puts "#{file} not exists"
exit 1
end
def simple_parse_env(line)
line = line.strip
return [nil, nil] if line.empty? || line.start_with?('#')
key, value = line.split('=', 2)
return [nil, nil] unless value # 如果没有等号则忽略
value = value.split('#', 2).first.strip
value = value.gsub(/\A['"]|['"]\z/, '')
[key.strip, value]
end
File.foreach(file) do |line|
unless line.start_with? "#"
key, value = simple_parse_env(line)
if key and value
if is_unset
puts "export #{key}="
else
puts "export #{key}=#{value}"
end
end
end
end