Class: DotEnvAndroid::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/dotenv-android/cli.rb

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



17
18
19
20
21
22
23
24
25
# File 'lib/dotenv-android/cli.rb', line 17

def initialize
  @options = parse_options

  @ui = DotEnvAndroid::UI.new(@options.verbose, @options.debug)

  assert_options

  DotEnvAndroid::Generator.new(@options).start
end

Instance Method Details

#assert_optionsObject



80
81
82
83
# File 'lib/dotenv-android/cli.rb', line 80

def assert_options
  prefix = '[ERROR]'
  @ui.fail("#{prefix} --source required") unless @options.source
end

#parse_optionsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dotenv-android/cli.rb', line 27

def parse_options
  Dotenv.load('.env')

  options = Options.new
  options.verbose = false
  options.debug = false

  # rubocop:disable Metrics/BlockLength
  opt_parser = OptionParser.new do |opts|
    opts.banner = 'Usage: dotenv-android [options]'

    opts.on('-v', '--version', 'Print version') do
      puts DotEnvAndroid::Version.get
      exit
    end
    opts.on('-s', '--source DIR', 'Source code directory to check for requested environment variables') do |source|
      options.source = source
      options.out = Pathname.new(source).join('Env.kt') # set default
    end
    opts.on('--verbose', 'Verbose output') do
      options.verbose = true
    end
    opts.on('--debug', 'Debug output (also turns on verbose)') do
      options.verbose = true
      options.debug = true
    end
    opts.on('--package', 'Package name to add to the top of the generated Env.kt file (example: com.yourdomain.app, or PACKAGE_NAME environment variable found in .env)') do |package_name| # rubocop:disable Metrics/LineLength
      options.package_name = package_name
    end
    opts.on('-o', '--out FILE', 'Output file (example: Path/Env.kt)') do |out|
      options.out = out
    end
    opts.on('-h', '--help', 'Prints this help') do
      puts opts
      exit
    end
  end
  # rubocop:enable Metrics/BlockLength

  help = opt_parser.help
  abort(help) if ARGV.empty?

  opt_parser.parse!(ARGV)

  if options.package_name.nil? 
    options.package_name = ENV["PACKAGE_NAME"]

    @ui.fail("Cannot find package name in .env file with key, PACKAGE_NAME, or as a CLI argument") if options.package_name.nil?
  end 

  options
end