Class: Canon::Commands::FormatCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/commands/format_command.rb

Overview

Command for canonicalizing files

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FormatCommand

Returns a new instance of FormatCommand.



11
12
13
# File 'lib/canon/commands/format_command.rb', line 11

def initialize(options = {})
  @options = options
end

Instance Method Details

#run(input_file) ⇒ Object

rubocop:disable Metrics/MethodLength



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
# File 'lib/canon/commands/format_command.rb', line 16

def run(input_file)
  # Read input file
  content = File.read(input_file, encoding: "utf-8")

  # Detect or use specified format
  format = detect_format(input_file)

  # Format based on mode
  result = format_content(content, format)

  # Output
  if @options[:output]
    File.write(@options[:output], result)
    mode_name = @options[:mode] == "pretty" ? "Pretty-printed" : "Canonicalized"
    puts "#{mode_name} #{format.upcase} written to #{@options[:output]}"
  else
    puts result
  end
rescue Errno::ENOENT
  abort "Error: File '#{input_file}' not found"
rescue Canon::Error => e
  abort "Error: #{e.message}"
rescue StandardError => e
  abort "Error processing file: #{e.message}"
end