Class: Lutaml::Jsonschema::Cli

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

Instance Method Summary collapse

Instance Method Details

#combine(*schema_files) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lutaml/jsonschema/cli.rb', line 38

def combine(*schema_files)
  raise Error, "No schema files provided" if schema_files.empty?

  schema_set = SchemaSet.load_from_files(*schema_files)
  combined = Combiner.new.combine(schema_set)
  json = JSON.pretty_generate(JSON.parse(combined.to_json))

  if options[:output] == "-"
    $stdout.write(json)
  else
    File.write(options[:output], json)
    puts "Combined schema written to #{options[:output]}"
  end
rescue Errno::ENOENT => e
  abort "Error: #{e.message}"
end

#spa(*schema_files) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lutaml/jsonschema/cli.rb', line 18

def spa(*schema_files)
  raise Error, "No schema files provided" if schema_files.empty?

  config = load_config(options[:config])
  config.output_path = options[:output] if options[:output]
  config.title = options[:title] if options[:title]
  config.theme = options[:theme] if options[:theme]

  schema_set = SchemaSet.load_from_files(*schema_files)
  generator = Spa::Generator.new(schema_set, config.output_path,
                                 metadata: config.)
  generator.generate
  puts "SPA documentation generated at #{config.output_path}/index.html"
rescue Errno::ENOENT => e
  abort "Error: #{e.message}"
end

#validate(schema_file) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lutaml/jsonschema/cli.rb', line 56

def validate(schema_file)
  data = File.read(schema_file)
  json = JSON.parse(data)
  Schema.from_json(data)

  errors = validate_structure(json)
  if errors.empty?
    puts "#{schema_file}: valid"
  else
    errors.each { |e| warn "  #{e}" }
    abort "#{schema_file}: #{errors.length} validation error(s)"
  end
rescue JSON::ParserError => e
  abort "Invalid JSON: #{e.message}"
rescue Errno::ENOENT => e
  abort "Error: #{e.message}"
end

#versionObject



75
76
77
# File 'lib/lutaml/jsonschema/cli.rb', line 75

def version
  puts "lutaml-jsonschema #{VERSION}"
end