philiprehberger-env_diff 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +22 -0
- data/LICENSE +21 -0
- data/README.md +99 -0
- data/lib/philiprehberger/env_diff/diff.rb +79 -0
- data/lib/philiprehberger/env_diff/parser.rb +59 -0
- data/lib/philiprehberger/env_diff/version.rb +7 -0
- data/lib/philiprehberger/env_diff.rb +38 -0
- metadata +55 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: f899f72163df3e8b9123a330fe7318f4d0321af12ff8e09c775aac59ec2d1e2b
|
|
4
|
+
data.tar.gz: ecc0e0a18cb186726f5f3513ff0295ec75f524572aa3f34e69010de63b6823d1
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 9fc0c09f9ef69d24068a860505f798d296b22c008d0fdd73fb16558de7484b738240fe85fc00df6baf4aaa585f4547340ad561681cf5c6a595673f3c5e148033
|
|
7
|
+
data.tar.gz: 5a51de272a472f5b59503ace4dc4db44de75fe996ea55aa4c4a91b0940bec471742f77c575e5cf0732702e15af34a457f5447869e4cbd4e90db2e6ee2b8a3be5
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this gem will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.1.2] - 2026-03-13
|
|
11
|
+
|
|
12
|
+
### Fixed
|
|
13
|
+
- Fix RuboCop ExtraSpacing offense in gemspec metadata
|
|
14
|
+
|
|
15
|
+
## [0.1.0] - 2026-03-13
|
|
16
|
+
|
|
17
|
+
### Added
|
|
18
|
+
- Initial release
|
|
19
|
+
- Compare two environment variable hashes and report added, removed, changed, and unchanged keys
|
|
20
|
+
- Parse `.env` files with support for comments, blank lines, quoted values, and `export` prefix
|
|
21
|
+
- `Diff` class with `changed?` predicate and human-readable `summary`
|
|
22
|
+
- Module-level `compare`, `from_hash`, and `from_env_file` convenience methods
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 philiprehberger
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# philiprehberger-env_diff
|
|
2
|
+
|
|
3
|
+
[](https://github.com/philiprehberger/rb-env-diff/actions/workflows/ci.yml)
|
|
4
|
+
[](https://rubygems.org/gems/philiprehberger-env_diff)
|
|
5
|
+
|
|
6
|
+
Compare environment variables across environments and report differences.
|
|
7
|
+
|
|
8
|
+
## Requirements
|
|
9
|
+
|
|
10
|
+
- Ruby >= 3.1
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
Add to your Gemfile:
|
|
15
|
+
|
|
16
|
+
```ruby
|
|
17
|
+
gem "philiprehberger-env_diff"
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Then run:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
bundle install
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Or install directly:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
gem install philiprehberger-env_diff
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
```ruby
|
|
35
|
+
require "philiprehberger/env_diff"
|
|
36
|
+
|
|
37
|
+
# Compare two hashes
|
|
38
|
+
source = { "DATABASE_URL" => "postgres://localhost/dev", "SECRET" => "abc", "OLD_KEY" => "remove_me" }
|
|
39
|
+
target = { "DATABASE_URL" => "postgres://prod-host/app", "SECRET" => "abc", "NEW_KEY" => "added" }
|
|
40
|
+
|
|
41
|
+
diff = Philiprehberger::EnvDiff.compare(source, target)
|
|
42
|
+
|
|
43
|
+
diff.added # => ["NEW_KEY"]
|
|
44
|
+
diff.removed # => ["OLD_KEY"]
|
|
45
|
+
diff.changed # => { "DATABASE_URL" => { source: "postgres://localhost/dev", target: "postgres://prod-host/app" } }
|
|
46
|
+
diff.unchanged # => ["SECRET"]
|
|
47
|
+
diff.changed? # => true
|
|
48
|
+
puts diff.summary
|
|
49
|
+
# + NEW_KEY
|
|
50
|
+
# - OLD_KEY
|
|
51
|
+
# ~ DATABASE_URL: "postgres://localhost/dev" -> "postgres://prod-host/app"
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Compare .env files
|
|
55
|
+
|
|
56
|
+
```ruby
|
|
57
|
+
diff = Philiprehberger::EnvDiff.from_env_file(".env.development", ".env.production")
|
|
58
|
+
puts diff.summary
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Parse .env files
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
vars = Philiprehberger::EnvDiff::Parser.parse(<<~ENV)
|
|
65
|
+
# Database config
|
|
66
|
+
DATABASE_URL=postgres://localhost/dev
|
|
67
|
+
export SECRET_KEY="my-secret"
|
|
68
|
+
APP_NAME='my-app'
|
|
69
|
+
ENV
|
|
70
|
+
# => { "DATABASE_URL" => "postgres://localhost/dev", "SECRET_KEY" => "my-secret", "APP_NAME" => "my-app" }
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## API
|
|
74
|
+
|
|
75
|
+
| Method / Class | Description |
|
|
76
|
+
|----------------|-------------|
|
|
77
|
+
| `EnvDiff.compare(source, target)` | Compare two hashes and return a `Diff` |
|
|
78
|
+
| `EnvDiff.from_hash(hash_a, hash_b)` | Alias for `compare` |
|
|
79
|
+
| `EnvDiff.from_env_file(path_a, path_b)` | Parse two `.env` files and compare them |
|
|
80
|
+
| `Diff#added` | Array of keys in target but not source |
|
|
81
|
+
| `Diff#removed` | Array of keys in source but not target |
|
|
82
|
+
| `Diff#changed` | Hash of keys with different values |
|
|
83
|
+
| `Diff#unchanged` | Array of keys with identical values |
|
|
84
|
+
| `Diff#changed?` | `true` if any differences exist |
|
|
85
|
+
| `Diff#summary` | Human-readable multiline diff string |
|
|
86
|
+
| `Parser.parse(content)` | Parse `.env` string into a hash |
|
|
87
|
+
| `Parser.parse_file(path:)` | Read and parse a `.env` file |
|
|
88
|
+
|
|
89
|
+
## Development
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
bundle install
|
|
93
|
+
bundle exec rspec # Run tests
|
|
94
|
+
bundle exec rubocop # Check code style
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## License
|
|
98
|
+
|
|
99
|
+
MIT
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Philiprehberger
|
|
4
|
+
module EnvDiff
|
|
5
|
+
# Represents the result of comparing two sets of environment variables.
|
|
6
|
+
class Diff
|
|
7
|
+
# @return [Array<String>] keys present in target but not source
|
|
8
|
+
attr_reader :added
|
|
9
|
+
|
|
10
|
+
# @return [Array<String>] keys present in source but not target
|
|
11
|
+
attr_reader :removed
|
|
12
|
+
|
|
13
|
+
# @return [Hash{String => Hash}] keys with different values ({ key => { source:, target: } })
|
|
14
|
+
attr_reader :changed
|
|
15
|
+
|
|
16
|
+
# @return [Array<String>] keys with identical values in both sets
|
|
17
|
+
attr_reader :unchanged
|
|
18
|
+
|
|
19
|
+
# Build a diff from two environment hashes.
|
|
20
|
+
#
|
|
21
|
+
# @param source [Hash] the baseline environment hash
|
|
22
|
+
# @param target [Hash] the environment hash to compare against
|
|
23
|
+
def initialize(source, target)
|
|
24
|
+
@added = (target.keys - source.keys).sort
|
|
25
|
+
@removed = (source.keys - target.keys).sort
|
|
26
|
+
@changed = build_changed(source, target)
|
|
27
|
+
@unchanged = build_unchanged(source, target)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Whether there are any differences between source and target.
|
|
31
|
+
#
|
|
32
|
+
# @return [Boolean] true if added, removed, or changed are non-empty
|
|
33
|
+
def changed?
|
|
34
|
+
!@added.empty? || !@removed.empty? || !@changed.empty?
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Human-readable multiline summary of all differences.
|
|
38
|
+
#
|
|
39
|
+
# @return [String] formatted summary
|
|
40
|
+
def summary
|
|
41
|
+
lines = []
|
|
42
|
+
append_added(lines)
|
|
43
|
+
append_removed(lines)
|
|
44
|
+
append_changed(lines)
|
|
45
|
+
lines.empty? ? "No differences found." : lines.join("\n")
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
def build_changed(source, target)
|
|
51
|
+
common = source.keys & target.keys
|
|
52
|
+
common.each_with_object({}) do |key, hash|
|
|
53
|
+
next if source[key] == target[key]
|
|
54
|
+
|
|
55
|
+
hash[key] = { source: source[key], target: target[key] }
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def build_unchanged(source, target)
|
|
60
|
+
common = source.keys & target.keys
|
|
61
|
+
common.select { |key| source[key] == target[key] }.sort
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def append_added(lines)
|
|
65
|
+
@added.each { |key| lines << "+ #{key}" }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def append_removed(lines)
|
|
69
|
+
@removed.each { |key| lines << "- #{key}" }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def append_changed(lines)
|
|
73
|
+
@changed.each do |key, vals|
|
|
74
|
+
lines << "~ #{key}: #{vals[:source].inspect} -> #{vals[:target].inspect}"
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Philiprehberger
|
|
4
|
+
module EnvDiff
|
|
5
|
+
# Parses .env file content into a key-value hash.
|
|
6
|
+
module Parser
|
|
7
|
+
# Parse a string of env file content into a hash.
|
|
8
|
+
#
|
|
9
|
+
# @param content [String] the raw .env file content
|
|
10
|
+
# @return [Hash{String => String}] parsed key-value pairs
|
|
11
|
+
def self.parse(content)
|
|
12
|
+
result = {}
|
|
13
|
+
content.each_line do |line|
|
|
14
|
+
key, value = parse_line(line.strip)
|
|
15
|
+
result[key] = value if key
|
|
16
|
+
end
|
|
17
|
+
result
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Read a file and parse its content.
|
|
21
|
+
#
|
|
22
|
+
# @param path [String] path to the .env file
|
|
23
|
+
# @return [Hash{String => String}] parsed key-value pairs
|
|
24
|
+
# @raise [Errno::ENOENT] if the file does not exist
|
|
25
|
+
def self.parse_file(path:)
|
|
26
|
+
parse(File.read(path))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Parse a single line from an env file.
|
|
30
|
+
#
|
|
31
|
+
# @param line [String] a stripped line
|
|
32
|
+
# @return [Array(String, String), nil] key-value pair or nil
|
|
33
|
+
def self.parse_line(line)
|
|
34
|
+
return nil if line.empty? || line.start_with?("#")
|
|
35
|
+
|
|
36
|
+
line = line.sub(/\Aexport\s+/, "")
|
|
37
|
+
key, _, value = line.partition("=")
|
|
38
|
+
return nil if key.empty? || value.nil?
|
|
39
|
+
|
|
40
|
+
[key.strip, unquote(value.strip)]
|
|
41
|
+
end
|
|
42
|
+
private_class_method :parse_line
|
|
43
|
+
|
|
44
|
+
# Remove surrounding quotes from a value.
|
|
45
|
+
#
|
|
46
|
+
# @param value [String] the raw value
|
|
47
|
+
# @return [String] the unquoted value
|
|
48
|
+
def self.unquote(value)
|
|
49
|
+
if (value.start_with?('"') && value.end_with?('"')) ||
|
|
50
|
+
(value.start_with?("'") && value.end_with?("'"))
|
|
51
|
+
return value[1..-2]
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
value
|
|
55
|
+
end
|
|
56
|
+
private_class_method :unquote
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "env_diff/version"
|
|
4
|
+
require_relative "env_diff/diff"
|
|
5
|
+
require_relative "env_diff/parser"
|
|
6
|
+
|
|
7
|
+
module Philiprehberger
|
|
8
|
+
module EnvDiff
|
|
9
|
+
class Error < StandardError; end
|
|
10
|
+
|
|
11
|
+
# Compare two environment hashes and return a Diff.
|
|
12
|
+
#
|
|
13
|
+
# @param source [Hash] the baseline environment hash
|
|
14
|
+
# @param target [Hash] the environment hash to compare against
|
|
15
|
+
# @return [Diff] the computed differences
|
|
16
|
+
def self.compare(source, target)
|
|
17
|
+
Diff.new(source, target)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Alias for compare — compare two hashes.
|
|
21
|
+
#
|
|
22
|
+
# @param hash_a [Hash] the baseline environment hash
|
|
23
|
+
# @param hash_b [Hash] the environment hash to compare against
|
|
24
|
+
# @return [Diff] the computed differences
|
|
25
|
+
def self.from_hash(hash_a, hash_b)
|
|
26
|
+
compare(hash_a, hash_b)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Parse two .env files and compare them.
|
|
30
|
+
#
|
|
31
|
+
# @param path_a [String] path to the source .env file
|
|
32
|
+
# @param path_b [String] path to the target .env file
|
|
33
|
+
# @return [Diff] the computed differences
|
|
34
|
+
def self.from_env_file(path_a, path_b)
|
|
35
|
+
compare(Parser.parse_file(path: path_a), Parser.parse_file(path: path_b))
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: philiprehberger-env_diff
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Philip Rehberger
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-03-13 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Parse .env files or environment hashes, compare them, and get a clear
|
|
14
|
+
report of added, removed, changed, and unchanged variables.
|
|
15
|
+
email:
|
|
16
|
+
- me@philiprehberger.com
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- CHANGELOG.md
|
|
22
|
+
- LICENSE
|
|
23
|
+
- README.md
|
|
24
|
+
- lib/philiprehberger/env_diff.rb
|
|
25
|
+
- lib/philiprehberger/env_diff/diff.rb
|
|
26
|
+
- lib/philiprehberger/env_diff/parser.rb
|
|
27
|
+
- lib/philiprehberger/env_diff/version.rb
|
|
28
|
+
homepage: https://github.com/philiprehberger/rb-env-diff
|
|
29
|
+
licenses:
|
|
30
|
+
- MIT
|
|
31
|
+
metadata:
|
|
32
|
+
homepage_uri: https://github.com/philiprehberger/rb-env-diff
|
|
33
|
+
source_code_uri: https://github.com/philiprehberger/rb-env-diff
|
|
34
|
+
changelog_uri: https://github.com/philiprehberger/rb-env-diff/blob/main/CHANGELOG.md
|
|
35
|
+
rubygems_mfa_required: 'true'
|
|
36
|
+
post_install_message:
|
|
37
|
+
rdoc_options: []
|
|
38
|
+
require_paths:
|
|
39
|
+
- lib
|
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
41
|
+
requirements:
|
|
42
|
+
- - ">="
|
|
43
|
+
- !ruby/object:Gem::Version
|
|
44
|
+
version: 3.1.0
|
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
46
|
+
requirements:
|
|
47
|
+
- - ">="
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: '0'
|
|
50
|
+
requirements: []
|
|
51
|
+
rubygems_version: 3.5.22
|
|
52
|
+
signing_key:
|
|
53
|
+
specification_version: 4
|
|
54
|
+
summary: Compare environment variables across environments and report differences
|
|
55
|
+
test_files: []
|