Class: Dcc::Cli::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/dcc/cli/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

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

Returns:

  • (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 options[:output]
    File.write(options[: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: options[: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 == options[:ring] } if options[:ring]
    if options[:index]
      f = files[options[:index].to_i] || abort("No file at index #{options[:index]}")
      output_to(f.data, options[: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: options[: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 = options[: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: options[:format])
  exit(1) unless result.ok?
end

#versionObject



129
130
131
# File 'lib/dcc/cli/cli.rb', line 129

def version
  puts "dcc #{::Dcc::VERSION}"
end