Class: Yaml::Sort::Cli
- Inherits:
-
Object
- Object
- Yaml::Sort::Cli
- Defined in:
- lib/yaml/sort/cli.rb
Instance Method Summary collapse
-
#execute(argv, kernel = Kernel) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
-
#initialize ⇒ Cli
constructor
A new instance of Cli.
- #process_document(filename, options) ⇒ Object
- #read_document(filename) ⇒ Object
- #show_diff(filename, actual, expected) ⇒ Object
- #sort_yaml(yaml, filename) ⇒ Object
- #write_output(yaml, sorted_yaml, filename, options) ⇒ Object
Constructor Details
Instance Method Details
#execute(argv, kernel = Kernel) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
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 |
# File 'lib/yaml/sort/cli.rb', line 15 def execute(argv, kernel = Kernel) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity = { in_place: false, lint: false, } OptionParser.new do |opts| opts. = "Usage: yaml-sort [options] [filename]" opts.on("-i", "--in-place", "Update files in-place") do [:in_place] = true end opts.on("-l", "--lint", "Ensure files content is sorted as expected") do [:lint] = true end end.parse!(argv) if ![:in_place] && ![:lint] && argv.count > 1 warn "Sorting multiple YAML document to stdout does not make sense" return 1 end if [:in_place] && argv.count < 1 warn "Cannot sort in-place when reading from stdin" return 1 end @exit_code = 0 if argv.empty? process_document(nil, ) else argv.each do |filename| process_document(filename, ) end end kernel.exit(@exit_code) end |
#process_document(filename, options) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/yaml/sort/cli.rb', line 54 def process_document(filename, ) yaml = read_document(filename) sorted_yaml = sort_yaml(yaml, filename) write_output(yaml, sorted_yaml, filename, ) rescue Racc::ParseError => e warn(e.) @exit_code = 1 end |
#read_document(filename) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/yaml/sort/cli.rb', line 63 def read_document(filename) if filename File.read(filename) else $stdin.read end end |
#show_diff(filename, actual, expected) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/yaml/sort/cli.rb', line 91 def show_diff(filename, actual, expected) filename ||= "<stdin>" a = Tempfile.new a.write(actual) a.close b = Tempfile.new b.write(expected) b.close warn "diff #{File.join("a", filename)} #{File.join("b", filename)}" warn `diff -u --label "#{File.join("a", filename)}" #{a.path} --label "#{File.join("b", filename)}" #{b.path}` end |
#sort_yaml(yaml, filename) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/yaml/sort/cli.rb', line 71 def sort_yaml(yaml, filename) document = @parser.parse(yaml, filename: filename) @parser.sort_anchors! document = document.sort "---\n#{document}\n" end |
#write_output(yaml, sorted_yaml, filename, options) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/yaml/sort/cli.rb', line 78 def write_output(yaml, sorted_yaml, filename, ) if [:in_place] File.write(filename, sorted_yaml) elsif [:lint] if yaml != sorted_yaml show_diff(filename, yaml, sorted_yaml) @exit_code = 1 end else puts sorted_yaml end end |