Class: Dcc::Cli::Cli
- Inherits:
-
Thor
- Object
- Thor
- Dcc::Cli::Cli
- Defined in:
- lib/dcc/cli/cli.rb
Class Method Summary collapse
-
.ensure_loaded! ⇒ Object
Lazy-load the model contexts the first time a CLI command runs.
- .exit_on_failure? ⇒ Boolean
Instance Method Summary collapse
- #convert(format, file) ⇒ Object
- #diff(file1, file2) ⇒ Object
- #extract(target, file) ⇒ Object
- #inspect(file) ⇒ Object
- #validate(schema, file) ⇒ Object
- #version ⇒ Object
Class Method Details
.ensure_loaded! ⇒ Object
Lazy-load the model contexts the first time a CLI command runs. This avoids autoload side effects when users just want the gem library (no CLI).
20 21 22 23 |
# File 'lib/dcc/cli/cli.rb', line 20 def ensure_loaded! ::Dcc::V3.load_all! ::Dcc::V2.load_all! end |
.exit_on_failure? ⇒ Boolean
26 27 28 |
# File 'lib/dcc/cli/cli.rb', line 26 def self.exit_on_failure? true end |
Instance Method Details
#convert(format, file) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/dcc/cli/cli.rb', line 64 def convert(format, file) Cli.ensure_loaded! xml = File.read(file) dcc = Dcc.parse(xml) result = case format when "json" then ::Dcc::Convert::Json.call(dcc) when "yaml" then ::Dcc::Convert::Yaml.call(dcc) else abort "Format not yet implemented: #{format}" end output = result.to_s if [:output] File.write([:output], output) else $stdout.puts(output) end end |
#diff(file1, file2) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/dcc/cli/cli.rb', line 120 def diff(file1, file2) Cli.ensure_loaded! dcc1 = Dcc.parse(File.read(file1)) dcc2 = Dcc.parse(File.read(file2)) diff = ::Dcc::Diff.call(dcc1, dcc2) ::Dcc::Cli::Formatters.print(diff, format: [:format]) end |
#extract(target, file) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/dcc/cli/cli.rb', line 89 def extract(target, file) Cli.ensure_loaded! dcc = Dcc.parse(File.read(file)) case target when "files" files = ::Dcc::Extract::File.each(dcc) files = files.select { |f| f.ring.to_s == [:ring] } if [:ring] if [:index] f = files[[:index].to_i] || abort("No file at index #{[:index]}") output_to(f.data, [:output], f.file_name) else ::Dcc::Cli::Formatters.print_files(files) end else abort "Unknown extract target: #{target}" end end |
#inspect(file) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/dcc/cli/cli.rb', line 110 def inspect(file) Cli.ensure_loaded! dcc = Dcc.parse(File.read(file)) report = ::Dcc::Inspect::Report.call(dcc) ::Dcc::Cli::Formatters.print(report, format: [:format]) end |
#validate(schema, file) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/dcc/cli/cli.rb', line 43 def validate(schema, file) Cli.ensure_loaded! xml = File.read(file) version = [:version] result = case schema when "xsd" then ::Dcc::Validate::Xsd.call(xml, version: version) when "schematron" then ::Dcc::Validate::Schematron.call(Dcc.parse(xml)) when "all" ::Dcc::Validate::Xsd.call(xml, version: version) .merge(::Dcc::Validate::Schematron.call(Dcc.parse(xml))) else abort "Unknown validation target: #{schema} (expected xsd|schematron|all)" end ::Dcc::Cli::Formatters.print(result, format: [:format]) exit(1) unless result.ok? end |