dorian-json-compare 0.0.2 → 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 +16 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c0af6789d1fa30cffb9d5946310e53be88a5f84253fbb95eeb018b03cc371751
4
- data.tar.gz: 61b1527eaad789dbb5e6f540da35bb18a6787297618e189b9ab24cf52409246d
3
+ metadata.gz: f2755a13feadb01ec042f0664d7a1c9a7d456e6a977a5abf0ec30048cdf277c4
4
+ data.tar.gz: cc893d3443d496e332541f45118a1003d2b7191d34bf1a4e24762f19c45c64ae
5
5
  SHA512:
6
- metadata.gz: cf6fcd20a645948e2294e17ec958834ca77d8516f01daffe3ace7801e16386edb125dd83636b7e99dd8d86fb524b8ba121e0467a4a7aaeb18afbeee75a3f3187
7
- data.tar.gz: 76bad4954496fe12a31b5ab9c0f7da5b12741c82052ab6af0b0b3f0c0b2e4fbd5fe7531da0a1041bdece60d281a511f5e37a3d1ed4e76cd0a452de1caeac45e3
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.2
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-18 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
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dorian-arguments
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  description: compare multiple json files
28
42
  email: dorian@dorianmarie.com
29
43
  executables: