Class: Json::Schema::Diff::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/json/schema/diff/cli.rb

Overview

Command-line interface for json-schema-diff

Provides a comprehensive CLI for comparing JSON files using JSON Schema guidance. Supports multiple output formats, validation options, and field filtering.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start(args) ⇒ void

This method returns an undefined value.

Entry point for the CLI application

Parameters:

  • args (Array<String>)

    Command-line arguments



15
16
17
# File 'lib/json/schema/diff/cli.rb', line 15

def self.start(args)
  new.run(args)
end

Instance Method Details

#run(args) ⇒ void

This method returns an undefined value.

Runs the CLI with the provided arguments

Parameters:

  • args (Array<String>)

    Command-line arguments including schema, old JSON, and new JSON files

Raises:

  • (SystemExit)

    Exits with status 1 on errors



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
53
54
55
56
57
58
59
60
61
62
# File 'lib/json/schema/diff/cli.rb', line 24

def run(args)
  options = parse_options(args)
  
  if args.length != 3
    puts "Usage: json-schema-diff [OPTIONS] SCHEMA OLD_JSON NEW_JSON"
    puts "Try 'json-schema-diff --help' for more information."
    exit 1
  end

  schema_file, old_file, new_file = args

  begin
    schema = SchemaParser.new(schema_file, validate_schema: options[:validate])
    comparer = Comparer.new(schema, options[:ignore_fields] || [])
    
    old_json = JSON.parse(File.read(old_file))
    new_json = JSON.parse(File.read(new_file))
    
    # Validate JSON files against schema if requested
    if options[:validate_json]
      schema.validate_json(old_json)
      schema.validate_json(new_json)
    end
    
    diff_result = comparer.compare(old_json, new_json)
    
    formatter = Formatter.new(options[:format], options[:color])
    puts formatter.format(diff_result)
  rescue JSON::ParserError => e
    puts "Error parsing JSON: #{e.message}"
    exit 1
  rescue Errno::ENOENT => e
    puts "File not found: #{e.message}"
    exit 1
  rescue Error => e
    puts "Error: #{e.message}"
    exit 1
  end
end