dorian-json-compare 0.0.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/bin/json-compare +49 -6
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e553b6c11f2b505aec9d149fdf924f7b176aac5fab0cbfd60cbf95a5f1fa614f
4
- data.tar.gz: 6c10b55d1ad23e6ddef55abb304880ea9cb14d9609aedfe6fd737fea23ddab0c
3
+ metadata.gz: f2755a13feadb01ec042f0664d7a1c9a7d456e6a977a5abf0ec30048cdf277c4
4
+ data.tar.gz: cc893d3443d496e332541f45118a1003d2b7191d34bf1a4e24762f19c45c64ae
5
5
  SHA512:
6
- metadata.gz: 404397bd0402636c0821f3ac0911dbcb2a92f694f70c3d0d03f09914929417d69d2f186ddf438a7371e296b3c3d1ba0972c1a56524b6ad9afa9d2f2e48f07bd0
7
- data.tar.gz: d44af02eaf2ab67fd278ae1ca0af832f9dcb86c629482bc2a594e7ea527a410038a00eb5648f069543f8fda1bf3634531e92c2d06cad830d63cc00bdf8965ebc
6
+ metadata.gz: 8b9309557fe2e8567ea4b8946d4d1b769890773239b6985710b41d1e1e8902f7e4bb65b5f28224afdbabe22dfd57491e2548a325a56b37580543c10702afb355
7
+ data.tar.gz: e3c9a3eb1756a69dc1db8f346a47157b15f230fd254572afd64ee3f1c18b5ccd25aae43bc034f5db1ed230fe3e2b24fea47d54346c1903c7f5d7f56e7eb870ff
data/bin/json-compare CHANGED
@@ -1,12 +1,55 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- if ARGV.size != 1 || ARGV[0] == "--help" || ARGV[0] == "-h"
5
- puts "USAGE: ... | each CODE"
6
- exit
4
+ require "json"
5
+ require "dorian/arguments"
6
+ require "stringio"
7
+
8
+ def compare(hash1, hash2, current: "")
9
+ if hash1.is_a?(Array)
10
+ hash1.each.with_index { |_, i| compare(hash1[i], hash2[i], current:) }
11
+ elsif hash1.is_a?(String) || hash2.is_a?(String)
12
+ if !hash1.is_a?(String)
13
+ puts "key \"#{current}\" is a string in #{ARGV[1]}"
14
+ elsif !hash2.is_a?(String)
15
+ puts "key \"#{current}\" is a string in #{ARGV[0]}"
16
+ end
17
+ else
18
+ (hash1.keys + hash2.keys).uniq.each do |key|
19
+ full_key = [current, key].reject(&:empty?).join(".")
20
+ if !hash1.key?(key)
21
+ puts "missing key \"#{full_key}\" from #{ARGV[0]}"
22
+ elsif !hash2.key?(key)
23
+ puts "missing key \"#{full_key}\" from #{ARGV[1]}"
24
+ else
25
+ compare(hash1[key], hash2[key], current: full_key)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ def with_captured_stdout(&block)
32
+ original_stdout = $stdout
33
+ $stdout = StringIO.new
34
+ block.call
35
+ $stdout.string
36
+ ensure
37
+ $stdout = original_stdout
7
38
  end
8
39
 
9
- $stdin.each_line do |it|
10
- it.strip!
11
- eval(ARGV.first)
40
+ parsed = Dorian::Arguments.parse(version: { alias: :v }, help: { alias: :h })
41
+
42
+ abort parsed.help if parsed.options.help
43
+
44
+ if parsed.options.version
45
+ abort File.read(File.expand_path("../../VERSION", __FILE__))
12
46
  end
47
+
48
+ file1, file2 = parsed.files.map { |file| JSON.parse(File.read(file)) }
49
+ root1, root2 = parsed.arguments
50
+ file1 = file1[root1] if root1
51
+ file2 = file2[root2] if root2
52
+
53
+ output = with_captured_stdout { compare(file1, file2) }
54
+
55
+ abort output unless output.empty?
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dorian-json-compare
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-22 00:00:00.000000000 Z
11
+ date: 2024-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json