class Cauchy::Cli

Public Instance Methods

apply(schema_name = nil) click to toggle source
# File lib/cauchy/cli.rb, line 33
def apply(schema_name = nil)
  verify_project!
  Migrator.migrate(
    client, schema_path, schema_name,
    options.slice('reindex', 'close_index').symbolize_keys
  )
rescue Elastic::CannotUpdateNonDynamicSettingsError => e
  Cauchy.logger.warn e.to_s
  Cauchy.logger.warn 'Provide --close-index in order to perform this update'
rescue MigrationError => e
  Cauchy.logger.warn e.to_s
end
init() click to toggle source
# File lib/cauchy/cli.rb, line 20
def init
  init_project
end
new(name) click to toggle source
# File lib/cauchy/cli.rb, line 25
def new(name)
  verify_project!
  generate_schema name
end
status(schema_name = nil) click to toggle source
# File lib/cauchy/cli.rb, line 47
def status(schema_name = nil)
  verify_project!
  Migrator.status(client, schema_path, schema_name)
rescue MigrationError => e
  Cauchy.logger.warn e.to_s
end
version() click to toggle source
# File lib/cauchy/cli.rb, line 55
def version
  Cauchy.logger.info "Couchy v#{Cauchy::VERSION}"
end

Private Instance Methods

client() click to toggle source
# File lib/cauchy/cli.rb, line 73
def client
  @client ||= Elastic::Client.new(config)
end
config() click to toggle source
# File lib/cauchy/cli.rb, line 77
def config
  @config ||= begin
    if File.exists?(config_path)
      YAML.load_file(config_path).deep_symbolize_keys
    else
      { url: ENV.fetch('ELASTICSEARCH_URL', 'localhost:9200') }
    end
  end
end
config_path() click to toggle source
# File lib/cauchy/cli.rb, line 65
def config_path
  @config_path ||= File.join project_path, 'config.yml'
end
generate_schema(name) click to toggle source
# File lib/cauchy/cli.rb, line 125
    def generate_schema(name)
      name = normalize_schema_name(name)
      path = schema_path("#{name}.rb")

      if File.exists? path
        return unless yes? "Schema file exists at #{path}, overwrite? (y/n)?"
      end

      Cauchy.logger.info "Creating a new schema `#{name}` at #{path}"

      File.open(path, 'w+') { |f| f.print <<-RB }
Cauchy::IndexSchema.define(:#{name}) do

  settings do
    # {
    #   number_of_replicas: 2
    # }
  end

  mappings do
    # {
    #   #{name.singularize}: {
    #     properties: {
    #       name: { type: 'string' }
    #     }
    #   }
    # }
  end

end
RB
    end
init_project() click to toggle source
# File lib/cauchy/cli.rb, line 103
    def init_project
      unless Dir.exists? schema_path
        Cauchy.logger.debug "Creating schema directory at #{schema_path}"
        FileUtils.mkdir_p schema_path
      end

      unless File.exists? config_path
        Cauchy.logger.debug "Creating config file at #{config_path}"
        File.open(config_path, 'w+') { |f| f.print <<-YAML }
# Supports elasticsearch/transport options. See: http://www.rubydoc.info/gems/elasticsearch-transport
# This file is optional, you can also set ELASTICSEARCH_URL in your environment
hosts:
  - host: localhost
    port: 9200
    scheme: http
YAML
      end

      Cauchy.logger.info "\nWant to run cauchy from anywhere? Add the following to your shell config:"
      Cauchy.logger.debug "export CAUCHY_PATH=#{project_path}"
    end
normalize_schema_name(name) click to toggle source
# File lib/cauchy/cli.rb, line 91
def normalize_schema_name(name)
  name.gsub(/[^a-z0-9\-]+/i, '_').downcase
end
project_path() click to toggle source
# File lib/cauchy/cli.rb, line 61
def project_path
  @project_path ||= File.expand_path options['path']
end
schema_path(schema_file = nil) click to toggle source
# File lib/cauchy/cli.rb, line 69
def schema_path(schema_file = nil)
  File.join *[project_path, 'schema', schema_file].compact
end
verify_project!() click to toggle source
# File lib/cauchy/cli.rb, line 95
def verify_project!
  unless Dir.exists? schema_path
    Cauchy.logger.fatal "Unable to locate schema directory #{schema_path}"
    Cauchy.logger.info 'Did you setup a project with `cauchy init`?'
    exit
  end
end
yaml_config() click to toggle source
# File lib/cauchy/cli.rb, line 87
def yaml_config

end