Class: MarkdownLint::CLI

Inherits:
Object
  • Object
show all
Includes:
Mixlib::CLI
Defined in:
lib/mdl/cli.rb

Overview

Our Mixlib::CLI class

Constant Summary collapse

CONFIG_FILE =
'.mdlrc'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.probe_config_file(path) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/mdl/cli.rb', line 198

def self.probe_config_file(path)
  expanded_path = File.expand_path(path)
  return expanded_path if File.exist?(expanded_path)

  # Look for a file up from the working dir
  Pathname.new(expanded_path).ascend do |p|
    next unless p.directory?

    config_file = p.join(CONFIG_FILE)
    return config_file if File.exist?(config_file)
  end
  nil
end

.toggle_list(parts, to_sym = false) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/mdl/cli.rb', line 181

def self.toggle_list(parts, to_sym = false)
  parts = parts.split(',') if parts.instance_of?(String)
  if parts.instance_of?(Array)
    inc = parts.reject { |p| p.start_with?('~') }
    exc = parts.select { |p| p.start_with?('~') }.map { |p| p[1..] }
    if to_sym
      inc.map!(&:to_sym)
      exc.map!(&:to_sym)
    end
    { :include => inc, :exclude => exc }
  else
    # We already converted the string into a list of include/exclude
    # pairs, so just return as is
    parts
  end
end

Instance Method Details

#run(argv = ARGV) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/mdl/cli.rb', line 141

def run(argv = ARGV)
  parse_options(argv)

  # Load the config file if it's present
  filename = CLI.probe_config_file(config[:config_file])

  # Only fall back to ~/.mdlrc if we are using the default value for -c
  if filename.nil? && (config[:config_file] == CONFIG_FILE)
    filename = File.expand_path("~/#{CONFIG_FILE}")
  elsif filename.nil? && config[:config_file] != CONFIG_FILE
    warn "Config file '#{config[:config_file]}' not found"
    exit 3
  end

  if !filename.nil? && File.exist?(filename)
    MarkdownLint::Config.from_file(filename.to_s)
    puts "Loaded config from #{filename}" if config[:verbose]
  end

  # Put values in the config file
  MarkdownLint::Config.merge!(config)

  # Set the correct format for any rules/tags configuration loaded from
  # the config file. Ideally this would probably be done as part of the
  # config class itself rather than here.
  unless MarkdownLint::Config[:rules].nil?
    MarkdownLint::Config[:rules] = CLI.toggle_list(
      MarkdownLint::Config[:rules],
    )
  end
  unless MarkdownLint::Config[:tags].nil?
    MarkdownLint::Config[:tags] = CLI.toggle_list(
      MarkdownLint::Config[:tags], true
    )
  end

  # Read from stdin if we didn't provide a filename
  cli_arguments << '-' if cli_arguments.empty? && !config[:list_rules]
end