Class: DataMapper::CLI

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



57
58
59
# File 'lib/dm-cli/cli.rb', line 57

def config
  @config
end

.optionsObject

Returns the value of attribute options.



57
58
59
# File 'lib/dm-cli/cli.rb', line 57

def options
  @options
end

Class Method Details

.configure(args) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/dm-cli/cli.rb', line 139

def configure(args)

  parse_args(args)

  @config[:environment] ||= "development"
  if @config[:config]
    @config.merge!(YAML::load_file(@config[:config]))
    @options = @config[:options]
  elsif @config[:yaml]
    @config.merge!(YAML::load_file(@config[:yaml]))
    @options = @config[@config[:environment]] || @config[@config[:environment].to_sym]
    raise "Options for environment '#{@config[:environment]}' are missing." if @options.nil?
  else
    @options = {
      :adapter  => @config[:adapter],
      :username => @config[:username],
      :password => @config[:password],
      :host     => @config[:host],
      :database => @config[:database]
    }
  end
  if !ARGV.empty?
    @config[:connection_string] = ARGV.shift
  end

end

.load_modelsObject



166
167
168
# File 'lib/dm-cli/cli.rb', line 166

def load_models
  Pathname.glob("#{config[:models]}/**/*.rb") { |file| load file }
end

.parse_args(argv = ARGV) ⇒ Object



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/dm-cli/cli.rb', line 59

def parse_args(argv = ARGV)
  @config ||= {}

  # Build a parser for the command line arguments
  OptionParser.new do |opt|
    opt.define_head "DataMapper CLI"
    opt.banner = usage

    opt.on("-m", "--models MODELS", "The directory to load models from.") do |models|
      @config[:models] = Pathname(models)
    end

    opt.on("-c", "--config FILE", "Entire configuration structure, useful for testing scenarios.") do |config_file|
      @config = YAML::load_file Pathname(config_file)
    end

    opt.on("--merb", "--rails", "Loads application settings: config/database.yml, app/models.") do
      @config[:models] = Pathname("app/models")
      @config[:yaml]   = Pathname("config/database.yml")
    end

    opt.on("-y", "--yaml YAML", "The database connection configuration yaml file.") do |yaml_file|
      if (yaml = Pathname(yaml_file)).file?
        @config[:yaml] = yaml
      elsif (yaml = Pathname("#{Dir.getwd}/#{yaml_file}")).file?
        @config[:yaml] = yaml
      else
        raise "yaml file was specifed as #{yaml_file} but does not exist."
      end
    end

    opt.on("-l", "--log LOGFILE", "A string representing the logfile to use. Also accepts STDERR and STDOUT") do |log_file|
      @config[:log_file] = log_file
    end

    opt.on("-e", "--environment STRING", "Run merb in the correct mode(development, production, testing)") do |environment|
      @config[:environment] = environment
    end

    opt.on("-a", "--adapter ADAPTER", "Number of merb daemons to run.") do |adapter|
      @config[:adapter] = adapter
    end

    opt.on("-u", "--username USERNAME", "The user to connect to the database as.") do |username|
      @config[:username] = username
    end

    opt.on("-p", "--password PASSWORD", "The password to connect to the database with") do |password|
      @config[:password] = password
    end

    opt.on("-h", "--host HOSTNAME", "Host to connect to.") do |host|
      @config[:host] = host
    end

    opt.on("-s", "--socket SOCKET", "The socket to connect to.") do |socket|
      @config[:socket] = socket
    end

    opt.on("-o", "--port PORT", "The port to connect to.") do |port|
      @config[:port] = port
    end

    opt.on("-d", "--database DATABASENAME", "Name of the database to connect to.") do |database_name|
      @config[:database] = database_name
    end

    opt.on("-P", "--plugins PLUGIN,PLUGIN...", "A list of dm-plugins to require", Array) do |plugins|
      @config[:plugins] = plugins
    end

    opt.on("-?", "-H", "--help", "Show this help message") do
      puts opt
      exit
    end

  end.parse!(argv)

end

.require_pluginsObject



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/dm-cli/cli.rb', line 170

def require_plugins
  # make sure we're loading dm plugins!
  plugins = config[:plugins].map {|p| (p =~ /^dm/) ? p : "dm-" + p }
  plugins.each do |plugin|
    begin
      require plugin
      puts "required #{plugin}."
    rescue LoadError => e
      puts "couldn't load #{plugin}."
    end
  end
end

.setup_loggerObject



183
184
185
186
187
188
189
190
191
# File 'lib/dm-cli/cli.rb', line 183

def setup_logger
  if config[:log_file] =~ /^std(?:out|err)$/i
    log = Object.full_const_get(config[:log_file].upcase)
  else
    log = Pathname(config[:log_file])
  end

  DataMapper::Logger.new(log, :debug)
end

.start(argv = ARGV) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/dm-cli/cli.rb', line 193

def start(argv = ARGV)
  if (ARGV.nil? || ARGV.empty?)
    puts DataMapper::CLI.usage
    exit 1
  end

  begin
    configure(argv)

    require_plugins if config[:plugins]

    setup_logger if config[:log_file]

    if config[:connection_string]
      DataMapper.setup(:default, config[:connection_string])
      puts "DataMapper has been loaded using '#{config[:connection_string]}'"
    else
      DataMapper.setup(:default, options.dup)
      puts "DataMapper has been loaded using the '#{options[:adapter] || options["adapter"]}' database '#{options[:database] || options["database"]}' on '#{options[:host] || options["host"]}' as '#{options[:username] || options["username"]}'"
    end
    load_models if config[:models]
    ENV["IRBRC"] ||= DataMapper::CLI::BinDir + "/.irbrc" # Do not change this please. This should NOT be DataMapper.root
    IRB.start
  rescue => error
    puts error.message
    exit
  end

end

.usageObject



13
14
15
16
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
# File 'lib/dm-cli/cli.rb', line 13

def usage
  <<-USAGE

dm - Data Mapper CLI

  Usage Examples\n#{'='*80}

* If one argument is given the CLI assumes it is a connection string:
  $ dm mysql://root@localhost/test_development

  The connection string has the format:
    adapter://user:password@host:port/database
  Where adapter is in: {mysql, pgsql, sqlite...} and the user/password is optional

  Note that if there are any non-optional arguments specified, the first is
  assumed to be a database connection string which will be used instead of any
  database specified by options.

* Load the database by specifying cli options
  $ dm -a mysql -u root -h localhost -d test_development

* Load the database using a yaml config file and specifying the environment to use
  $ dm --yaml config/database.yml -e development

* Load everything from a config file, this example is equivalent to the above
  $ dm --config config/development.yml

* Load the database and some model files from a directory, specifying the environment
  $ dm --yaml config/database.yml -e development --models app/models

* Load an assumed structure of a typical merb application
  $ dm --merb -e development

  This is similar to merb -i without the merb framework being loaded.

* Load the dm-validations and dm-timestamps plugins before connecting to the db
  $ dm -P validations,dm-timestamps mysql://root@localhost/test_development

  If dm- isn't at the start of the file, it will be prepended.


USAGE
end