Module: Dry::CommandLine

Defined in:
lib/dry-stack/command_line.rb

Constant Summary collapse

COMMANDS =
{}

Class Method Summary collapse

Class Method Details

.load_envObject



30
31
32
33
34
35
36
37
38
# File 'lib/dry-stack/command_line.rb', line 30

def load_env
  File.read('.env').lines.map(&:strip).grep_v(/^\s*#/).reject(&:empty?)
    .map {_1.split('=').map(&:strip).tap { |k,v|
      ENV[k] = v
    }}.to_h
 rescue =>e
   puts "Load env error: #{e.message}"
   raise 'Invalid .env file'
end

.run(args) ⇒ Object



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dry-stack/command_line.rb', line 46

def run(args)
  params = {}

  a, extra = ARGV.join(' ').split( / -- /)
  ARGV.replace a.split if a
  ARGV << '-h' if ARGV.empty?

  OptionParser.new do |o|
    o.version = "#{Dry::Stack::VERSION}"

    usage = [
      'dry-stack -s stackfile [options] COMMAND',
      'cat stackfile | dry-stack COMMAND',
      'dry-stack COMMAND < stack.drs'
    ]
    o.banner = "Version: #{o.version}\nUsage:\n\t#{usage.join "\n\t"}"
    o.separator ''
    o.separator 'Commands:'
    COMMANDS.each { |name, cmd| o.separator "#{' ' * 5}#{name} -  #{[cmd.help].flatten.join "\n#{' ' * (5+4 + name.size)}" }" }

    o.separator ''
    o.separator 'Options:'

    #  in all caps are required
    o.on('-s', '--stack STACK_NAME', 'Stack file')
    o.on('-e', '--env', 'Load .env file') { load_env }
    o.on('',   '--name STACK_NAME', 'Define stack name')
    o.on('',   '--ingress', 'Generate ingress labels') { true }
    o.on('',   '--traefik', 'Generate traefik labels') { true }
    o.on('',   '--traefik-tls', 'Generate traefik tls labels') { true }
    o.on('',   '--tls-domain domain', 'Domain for the traefik labels')
    o.on('',   '--host-sed /from/to/', 'Sed ingress host  /\\*/dev.*/')
    o.on('-n', '--no-env', 'Deprecated') { $stderr.puts 'warning: deprecated option: -n' } # TODO: remove
    o.on('-c', '--configuration name', 'Configuration name')
    COMMANDS.values.select{_1.options(o) if _1.respond_to? :options }

    o.on('-h', '--help') { puts o; exit }
    o.parse! args, into: params

    params.transform_keys!{_1.to_s.gsub('-','_').to_sym}
    params[:traefik_tls] = true if params[:tls_domain]
    params[:traefik] = true unless params.key? :traefik

    Dry.cmd_params = params

    raise 'Stack file not defined' if $stdin.tty? && !params[:stack]

    command = args.shift || ''
    raise "Unknown command: #{command}" unless COMMANDS.key?(command.to_sym)

    stack_text = File.read(params[:stack]) if params[:stack]
    stack_text ||= STDIN.read unless $stdin.tty?


    safe_eval stack_text, params # isolate context

    Stack.last_stack.name = params[:name] if params[:name]
    COMMANDS[command.to_sym].run Stack.last_stack, params, args, extra
  rescue => e
    puts e.message
    ENV['DEBUG'] ? raise : exit(1)
  end
end

.safe_eval(drs, params) ⇒ Object



40
41
42
43
44
# File 'lib/dry-stack/command_line.rb', line 40

def safe_eval(drs, params)
  Dry::Stack(params[:name], params[:configuration]) do
    eval drs, self.binding
  end
end