File: cli.rb

package info (click to toggle)
ruby-dotenv 2.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 244 kB
  • ctags: 56
  • sloc: ruby: 664; makefile: 9
file content (36 lines) | stat: -rw-r--r-- 711 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
require "dotenv"

module Dotenv
  # The CLI is a class responsible of handling all the command line interface
  # logic.
  class CLI
    attr_reader :argv

    def initialize(argv = [])
      @argv = argv.dup
    end

    def run
      filenames = parse_filenames || []
      begin
        Dotenv.load!(*filenames)
      rescue Errno::ENOENT => e
        abort e.message
      else
        exec(*argv) unless argv.empty?
      end
    end

    private

    def parse_filenames
      pos = argv.index("-f")
      return nil unless pos
      # drop the -f
      argv.delete_at pos
      # parse one or more comma-separated .env files
      require "csv"
      CSV.parse_line argv.delete_at(pos)
    end
  end
end