class Dpl::Env::Env

Attributes

cmd[R]

opts allows unconventional ENV vars such as GOOGLECLOUDKEYFILE

env[R]

opts allows unconventional ENV vars such as GOOGLECLOUDKEYFILE

keys[R]

opts allows unconventional ENV vars such as GOOGLECLOUDKEYFILE

opts[R]

opts allows unconventional ENV vars such as GOOGLECLOUDKEYFILE

strs[R]

opts allows unconventional ENV vars such as GOOGLECLOUDKEYFILE

Public Class Methods

new(env, args) click to toggle source
# File lib/dpl/helper/env.rb, line 17
def initialize(env, args)
  @env = env
  @opts = args.last.is_a?(Hash) ? args.pop : {}
  @strs = args.map(&:to_s).map(&:upcase)
end

Public Instance Methods

description(cmd) click to toggle source
# File lib/dpl/helper/env.rb, line 30
def description(cmd)
  strs = self.strs.map { |str| "#{str}_" }
  strs += self.strs if opts[:allow_skip_underscore]
  strs = strs.size > 1 ? "[#{strs.sort.join('|')}]" : strs.join
  "Options can be given via env vars if prefixed with `#{strs}`. #{example(cmd)}"
end
example(cmd) click to toggle source
# File lib/dpl/helper/env.rb, line 37
def example(cmd)
  return unless opt = cmd.opts.detect { |option| option.secret? }

  env = strs.map { |str| "`#{str}_#{opt.name.upcase}=<#{opt.name}>`" }
  env += strs.map { |str| "`#{str}#{opt.name.upcase}=<#{opt.name}>`" } if opts[:allow_skip_underscore]
  "E.g. the option `--#{opt.name}` can be given as #{sentence(env)}."
end
sentence(strs) click to toggle source
# File lib/dpl/helper/env.rb, line 45
def sentence(strs)
  return strs.join if strs.size == 1

  [strs[0..-2].join(', '), strs[-1]].join(' or ')
end

Private Instance Methods

dealias(key) click to toggle source
# File lib/dpl/helper/env.rb, line 53
def dealias(key)
  opt = cmd.opts.detect { |option| option.aliases.include?(key) }
  opt ? opt.name : key
end
keys_for(str, key) click to toggle source
# File lib/dpl/helper/env.rb, line 68
def keys_for(str, key)
  keys = [["#{str}_", key.upcase].join]
  keys << [str, key.upcase].join if opts[:allow_skip_underscore]
  keys
end
unprefix(key) click to toggle source
# File lib/dpl/helper/env.rb, line 58
def unprefix(key)
  strs.inject(key) { |key, str| key.sub(/^#{str}_?/, '') }
end