Module: Shiba::Configure

Defined in:
lib/shiba/configure.rb

Class Method Summary collapse

Class Method Details

.activerecord_configuration(config_path = "config/database.yml") ⇒ Object

avoiding Rails dependency on the cli tools for now. yanked from github.com/rails/rails/blob/v5.0.5/railties/lib/rails/application/configuration.rb



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/shiba/configure.rb', line 10

def self.activerecord_configuration(config_path = "config/database.yml")
  yaml = Pathname.new(config_path)

  config = if yaml && yaml.exist?
             require "yaml"
             require "erb"
             YAML.load(ERB.new(yaml.read).result) || {}
           end

  env     = ENV["RAILS_ENV"] || "test"
  config  = config[env]
  adapter = config.delete("adapter")

  if adapter == "mysql2"
    config["server"] = "mysql"
  else
    config["server"] = adapter
  end

  config
rescue Psych::SyntaxError => e
  raise "YAML syntax error occurred while parsing #{yaml.to_s}. " \
    "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
    "Error: #{e.message}"
rescue => e
  raise e, "Cannot load `#{path}`:\n#{e.message}", e.backtrace
end

.ci?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/shiba/configure.rb', line 38

def self.ci?
  ENV['CI'] || ENV['CONTINUOUS_INTEGRATION']
end

.make_options_parser(options, only_basics = false) ⇒ Object



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
138
139
140
141
142
143
144
# File 'lib/shiba/configure.rb', line 68

def self.make_options_parser(options, only_basics = false)
  OptionParser.new do |opts|
    # note that the key to the hash needs to stay the same as the
    # option name since we re-pass them
    opts.on("-s","--server SERVER_TYPE", "mysql|postgres") do |s|
      options["server"] = s
    end

    opts.on("-h","--host HOST", "sql host") do |h|
      options["host"] = h
    end

    opts.on("-d","--database DATABASE", "sql database") do |d|
      options["database"] = d
    end

    opts.on("-u","--username USER", "sql user") do |u|
      options["username"] = u
    end

    opts.on("-p","--password PASSWORD", "sql password") do |p|
      options["password"] = p
    end

    opts.on("-P","--port PORT", "server port") do |p|
      options["port"] = p
    end

    opts.on("-c","--config FILE", "location of shiba.yml") do |f|
      options["config"] = f
    end

    opts.on("-i","--index INDEX", "location of shiba_index.yml") do |i|
      options["index"] = i.to_i
    end

    opts.on("--default-extras-file", "The option file to read mysql configuration from") do |f|
      options["default_file"] = f
    end

    next if only_basics

    opts.on("--sql SQL", "analyze this sql") do |s|
      options["sql"] = s
    end

    opts.on("-f", "--file FILE", "location of file containing queries") do |f|
      options["file"] = f
    end

    opts.on("-j", "--json [FILE]", "write JSON report here. default: to stdout") do |f|
      if f
        options["json"] = File.open(f, 'w')
      else
        options["json"] = $stdout
      end
    end

    opts.on("--example_data_json FILE", "(dev only) generate example data for vue development testing") do |f|
      options['example_data_json'] = f
    end

    opts.on("-h", "--html FILE", "write html report here.") do |h|
      options["html"] = h
    end

    opts.on("-v", "--verbose", "print internal runtime information") do
       options["verbose"] = true
    end

    # This naming seems to be mysql convention, maybe we should just do our own thing though.
    opts.on("--login-path", "The option group from the mysql config file to read from") do |f|
      options["default_group"] = f
    end

  end
end

.mysql_config_pathObject



43
44
45
46
47
# File 'lib/shiba/configure.rb', line 43

def self.mysql_config_path
  paths = [ File.join(Dir.home, '.mylogin.cnf'), File.join(Dir.home, '.my.cnf')  ]

  paths.detect { |p| File.exist?(p) }
end

.read_config_file(option_file, default) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/shiba/configure.rb', line 49

def self.read_config_file(option_file, default)
  file_to_read = nil
  if option_file
    if !File.exist?(option_file)
      $stderr.puts "no such file: #{option_file}"
      exit 1
    end
    file_to_read = option_file
  elsif File.exist?(default)
    file_to_read = default
  end

  if file_to_read
    YAML.load_file(file_to_read)
  else
    {}
  end
end