Class: Lutaml::Cli::Uml::InfoCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/cli/uml/info_command.rb

Overview

InfoCommand displays package metadata

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ InfoCommand

Returns a new instance of InfoCommand.



11
12
13
# File 'lib/lutaml/cli/uml/info_command.rb', line 11

def initialize(options = {})
  @options = options.transform_keys(&:to_sym)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/lutaml/cli/uml/info_command.rb', line 9

def options
  @options
end

Class Method Details

.add_options_to(thor_class, _method_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/lutaml/cli/uml/info_command.rb', line 15

def self.add_options_to(thor_class, _method_name)
  thor_class.long_desc <<-DESC
  Display metadata and statistics for a LUR package without loading
  the full repository.

  Example:
    lutaml uml info model.lur

    lutaml uml info model.lur --format json
  DESC

  thor_class.option :format, type: :string, default: "text",
                             desc: "Output format (text|yaml|json)"
end

Instance Method Details

#run(lur_path) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/lutaml/cli/uml/info_command.rb', line 30

def run(lur_path) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  unless File.exist?(lur_path)
    puts OutputFormatter.error("Package file not found: #{lur_path}")
    raise Thor::Error, "Package file not found: #{lur_path}"
  end

  require "zip"
  require "yaml"

  begin
    Zip::File.open(lur_path) do |zip|
       = zip.find_entry("metadata.yaml")
      unless 
        puts OutputFormatter.error("Invalid package: missing metadata")
        raise Thor::Error, "Invalid package: missing metadata"
      end

      # Permit all Lutaml::Uml classes for safe loading
      uml_constants = Lutaml::Uml.constants
      uml_classes = uml_constants.filter_map do |const_name|
        constant_value = Lutaml::Uml.const_get(const_name)
        constant_value if constant_value.is_a?(Class)
      end
      permitted_classes = [Symbol, Time, Date, DateTime, uml_classes]
        .flatten

       = YAML.safe_load(
        .get_input_stream.read,
        permitted_classes: permitted_classes,
        aliases: true,
      )

      if options[:format] == "text"
        display_package_info()
      else
        puts OutputFormatter.format(, format: options[:format])
      end
    end
  rescue Thor::Error
    raise
  rescue StandardError => e
    puts OutputFormatter.error("Failed to read package info: " \
                               "#{e.message}")
    raise Thor::Error, "Failed to read package info: #{e.message}"
  end
end