Class: Crudboy::Cli

Inherits:
Object show all
Defined in:
lib/crudboy/cli.rb

Class Method Summary collapse

Class Method Details

.config_template_bundle(template_bundle) ⇒ Object



12
13
14
15
# File 'lib/crudboy/cli.rb', line 12

def config_template_bundle(template_bundle)
  return template_bundle if File.exist?(template_bundle)
  File.join(File.expand_path("~/.crudboy.d"), 'bundles', template_bundle)
end

.default_config_fileObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/crudboy/cli.rb', line 71

def default_config_file
  conf = File.expand_path('~/.crudboy.yml')
  return conf if File.file?(conf)
  conf = File.expand_path('~/.crudboy.yaml')
  return conf if File.file?(conf)
  conf = File.expand_path('~/.crudboy.d/init.yml')
  return conf if File.file?(conf)
  conf = File.expand_path('~/.crudboy.d/init.yaml')
  return conf if File.file?(conf)
end

.default_initializerObject



82
83
84
85
86
87
# File 'lib/crudboy/cli.rb', line 82

def default_initializer
  conf = File.expand_path('~/.crudboy.rb')
  return conf if File.file?(conf)
  conf = File.expand_path('~/.crudboy.d/init.rb')
  return conf if File.file?(conf)
end

.parse_options!Object



17
18
19
20
21
22
23
24
25
26
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
# File 'lib/crudboy/cli.rb', line 17

def parse_options!
  @options = OpenStruct.new(config_file: default_config_file,
                            initializer: default_initializer,
                            destination: Dir.pwd,
                            ssh: {})


  OptionParser.new do |opts|
    opts.banner = <<~EOF
    Usage: crudboy [options] [ruby file]

      If neither [ruby file] nor -e option specified, and STDIN is not a tty, a Pry REPL will be launched,
      otherwise the specified ruby file or -e option value or ruby code read from STDIN will be run, and no REPL launched

    EOF

    opts.on('-cCONFIG_FILE', '--conf=CONFIG_FILE', 'Specify config file, default is $HOME/.crudboy.yml, or $HOME/.crudboy.d/init.yml.') do |config_file|
      @options.config_file = config_file
    end

    opts.on('-iINITIALIZER', '--initializer=INITIALIZER', 'Specify initializer ruby file, default is $HOME/.crudboy.rb, or $HOME/.crudboy.d/init.rb.') do |initializer|
      @options.initializer = initializer
    end

    opts.on('-eENVIRON', '--env=ENVIRON', 'Specify config environment.') do |env|
      @options.env = env
    end

    opts.on('-tTABLE_NAME', '--table=TABLE_NAME', 'Specify table name') do |table_name|
      @options.table_name = table_name
    end

    opts.on('-mMODEL_NAME', '--model=MODEL_NAME', 'Specify model name') do |model_name|
      @options.model_name = model_name
    end

    opts.on('-oOUTPUT', '--output=OUTPUT', 'Specify output path, default: $PWD') do |destination|
      @options.destination = destination
    end

    opts.on('-bTEMPLATE_BUNDLE', '--bundle=TEMPLATE_BUNDLE', 'Specify template bundle, may be a path point to a .crudboy file or a directory') do |template_bundle|
      @options.template_bundle = config_template_bundle(template_bundle)
    end

    opts.on('', '--help', 'Prints this help') do
      puts opts
      exit
    end

  end.parse!

  @options.template_args = ARGV
end

.startObject



7
8
9
10
# File 'lib/crudboy/cli.rb', line 7

def start
  parse_options!
  App.new(@options).run!
end