1
0

cli.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. require "dotenv"
  2. require "dotenv/version"
  3. require "dotenv/template"
  4. require "optparse"
  5. module Dotenv
  6. # The `dotenv` command line interface. Run `$ dotenv --help` to see usage.
  7. class CLI < OptionParser
  8. attr_reader :argv, :filenames, :overwrite
  9. def initialize(argv = [])
  10. @argv = argv.dup
  11. @filenames = []
  12. @ignore = false
  13. @overwrite = false
  14. super("Usage: dotenv [options]")
  15. separator ""
  16. on("-f FILES", Array, "List of env files to parse") do |list|
  17. @filenames = list
  18. end
  19. on("-i", "--ignore", "ignore missing env files") do
  20. @ignore = true
  21. end
  22. on("-o", "--overwrite", "overwrite existing ENV variables") do
  23. @overwrite = true
  24. end
  25. on("--overload") { @overwrite = true }
  26. on("-h", "--help", "Display help") do
  27. puts self
  28. exit
  29. end
  30. on("-v", "--version", "Show version") do
  31. puts "dotenv #{Dotenv::VERSION}"
  32. exit
  33. end
  34. on("-t", "--template=FILE", "Create a template env file") do |file|
  35. template = Dotenv::EnvTemplate.new(file)
  36. template.create_template
  37. end
  38. order!(@argv)
  39. end
  40. def run
  41. Dotenv.load(*@filenames, overwrite: @overwrite, ignore: @ignore)
  42. rescue Errno::ENOENT => e
  43. abort e.message
  44. else
  45. exec(*@argv) unless @argv.empty?
  46. end
  47. end
  48. end