Skip to content

Commit

Permalink
add compare command and a sample config file
Browse files Browse the repository at this point in the history
  • Loading branch information
dorianmariecom committed Sep 6, 2024
1 parent f10a31e commit 3eb536a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
71 changes: 71 additions & 0 deletions lib/dorian/bin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ def run
arguments.delete("commit")
@command = :commit
command_commit
when :compare
arguments.delete("compare")
@command = :compare
command_compare
else
arguments.delete("read")
@command = :read
Expand Down Expand Up @@ -241,6 +245,24 @@ def everything
read_stdin_files + stdin_arguments + read_files + arguments
end

def command_compare
file_1, file_2 = files
key_1, key_2 = arguments
read_1, read_2 = files.map.with_index do |file, index|
read = reads(File.read(file))

if arguments[index] && read.from_deep_struct.has_key?(arguments[index])
read[arguments[index]]
elsif arguments[index]
nil
else
read
end
end

compare(read_1, read_2, file_1:, file_2:)
end

def command_each
each(everything) do |input|
each(lines(reads(input)), progress: true) { |line| evaluates(it: line) }
Expand Down Expand Up @@ -752,6 +774,55 @@ def post(url, headers: {}, body: {})
http.request(request).body
end

def compare(content_1, content_2, path: ".", file_1:, file_2:)
content_1 = content_1.from_deep_struct
content_2 = content_2.from_deep_struct

if content_1.is_a?(Hash) && content_2.is_a?(Hash)
(content_1.keys + content_2.keys).uniq.each do |key|
new_path = path == "." ? "#{path}#{key}" : "#{path}.#{key}"

if content_1[key] && !content_2[key]
warn "#{new_path} present in #{file_1} but not in #{file_2}"
next
elsif !content_1[key] && content_2[key]
warn "#{new_path} present in #{file_2} but not in #{file_1}"
next
end

compare(
content_1[key], content_2[key],
path: new_path,
file_1:,
file_2:
)
end
elsif content_1.is_a?(Array) && content_2.is_a?(Array)
(0...([content_1.size, content_2.size].max)).each do |index|
new_path = "#{path}[#{index}]"
if content_1[index] && !content_2[index]
warn "#{new_path} present in #{file_1} but not in #{file_2}"
next
elsif !content_1[index] && content_2[index]
warn "#{new_path} present in #{file_2} but not in #{file_1}"
next
end

compare(
content_1[index], content_2[index],
path: new_path,
file_1:,
file_2:
)
end
elsif content_1.class != content_2.class
warn(
"#{path} has #{content_1.class} for #{file_1} " \
"and #{content_2.class} for #{file_2}"
)
end
end

def short(string)
string[0..5000]
end
Expand Down
22 changes: 22 additions & 0 deletions samples/config_2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
app_config:
name: My Web App
version: 1.0.0
environment: production
debug_mode: false
database:
host: localhost
port: 5432
username: db_user
name: my_web_app_db
logging:
level: info
log_to_file: true
file_path: "/var/log/my_web_app.log"
features:
authentication: true
api: true
web_sockets: false
allowed_hosts:
- example.com
- api.example.com

0 comments on commit 3eb536a

Please sign in to comment.