Skip to content

Commit 6848e5f

Browse files
committed
Initial commit
1 parent 554c8c8 commit 6848e5f

File tree

10 files changed

+637
-0
lines changed

10 files changed

+637
-0
lines changed

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# IDE
2+
.idea/
3+
.vscode/
4+
*.swp
5+
*.swo
6+
7+
# OS
8+
.DS_Store
9+
Thumbs.db
10+
11+
# Go
12+
bin/
13+
pkg/
14+
*.exe
15+
*.test
16+
*.out

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025
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.

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,56 @@
11
# env-diff
2+
23
Compare .env files and detect missing or extra variables to catch configuration issues before deployment
4+
5+
## Features
6+
7+
- Parse .env and .env.example files with support for comments and blank lines
8+
- Detect missing variables (in .env.example but not in .env)
9+
- Detect extra variables (in .env but not in .env.example)
10+
- Detect value mismatches when both files have the same key
11+
- Preserve and display inline comments from env files
12+
- Color-coded diff output (red for missing, yellow for extra, green for matching)
13+
- Exit with non-zero status code when differences are found (CI/CD friendly)
14+
- Support comparing any two env files (not just .env and .env.example)
15+
- Show line numbers for each variable in the source files
16+
- Ignore commented-out variables (lines starting with #)
17+
- Handle quoted values and special characters correctly
18+
- Provide summary statistics (total variables, missing count, extra count)
19+
20+
## Installation
21+
22+
```bash
23+
# Clone the repository
24+
git clone https://github.com/KurtWeston/env-diff.git
25+
cd env-diff
26+
27+
# Install dependencies
28+
go build
29+
```
30+
31+
## Usage
32+
33+
```bash
34+
./main
35+
```
36+
37+
## Built With
38+
39+
- go
40+
41+
## Dependencies
42+
43+
- `github.com/spf13/cobra`
44+
- `github.com/fatih/color`
45+
46+
## Contributing
47+
48+
1. Fork the repository
49+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
50+
3. Commit your changes (`git commit -m 'Add amazing feature'`)
51+
4. Push to the branch (`git push origin feature/amazing-feature`)
52+
5. Open a Pull Request
53+
54+
## License
55+
56+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

differ.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
7+
"github.com/fatih/color"
8+
)
9+
10+
type DiffResult struct {
11+
File1 string
12+
File2 string
13+
Missing []EnvVar
14+
Extra []EnvVar
15+
Mismatched []MismatchedVar
16+
Matching []EnvVar
17+
}
18+
19+
type MismatchedVar struct {
20+
Key string
21+
Var1 EnvVar
22+
Var2 EnvVar
23+
}
24+
25+
func CompareEnvFiles(env1, env2 *EnvFile, name1, name2 string) *DiffResult {
26+
result := &DiffResult{
27+
File1: name1,
28+
File2: name2,
29+
Missing: []EnvVar{},
30+
Extra: []EnvVar{},
31+
Mismatched: []MismatchedVar{},
32+
Matching: []EnvVar{},
33+
}
34+
35+
for key, var2 := range env2.Vars {
36+
if var1, exists := env1.Vars[key]; exists {
37+
if var1.Value != var2.Value {
38+
result.Mismatched = append(result.Mismatched, MismatchedVar{Key: key, Var1: var1, Var2: var2})
39+
} else {
40+
result.Matching = append(result.Matching, var1)
41+
}
42+
} else {
43+
result.Missing = append(result.Missing, var2)
44+
}
45+
}
46+
47+
for key, var1 := range env1.Vars {
48+
if _, exists := env2.Vars[key]; !exists {
49+
result.Extra = append(result.Extra, var1)
50+
}
51+
}
52+
53+
sort.Slice(result.Missing, func(i, j int) bool { return result.Missing[i].Key < result.Missing[j].Key })
54+
sort.Slice(result.Extra, func(i, j int) bool { return result.Extra[i].Key < result.Extra[j].Key })
55+
sort.Slice(result.Mismatched, func(i, j int) bool { return result.Mismatched[i].Key < result.Mismatched[j].Key })
56+
57+
return result
58+
}
59+
60+
func (d *DiffResult) HasDifferences() bool {
61+
return len(d.Missing) > 0 || len(d.Extra) > 0 || len(d.Mismatched) > 0
62+
}
63+
64+
func (d *DiffResult) Print(noColor, quiet bool) {
65+
red := color.New(color.FgRed).SprintFunc()
66+
yellow := color.New(color.FgYellow).SprintFunc()
67+
green := color.New(color.FgGreen).SprintFunc()
68+
cyan := color.New(color.FgCyan).SprintFunc()
69+
70+
if noColor {
71+
color.NoColor = true
72+
}
73+
74+
if !quiet {
75+
if len(d.Missing) > 0 {
76+
fmt.Printf("\n%s\n", red("Missing variables (in "+d.File2+" but not in "+d.File1+"):"))
77+
for _, v := range d.Missing {
78+
fmt.Printf(" %s %s (line %d)\n", red("-"), cyan(v.Key), v.Line)
79+
if v.Comment != "" {
80+
fmt.Printf(" # %s\n", v.Comment)
81+
}
82+
}
83+
}
84+
85+
if len(d.Extra) > 0 {
86+
fmt.Printf("\n%s\n", yellow("Extra variables (in "+d.File1+" but not in "+d.File2+"):"))
87+
for _, v := range d.Extra {
88+
fmt.Printf(" %s %s (line %d)\n", yellow("+"), cyan(v.Key), v.Line)
89+
}
90+
}
91+
92+
if len(d.Mismatched) > 0 {
93+
fmt.Printf("\n%s\n", yellow("Mismatched values:"))
94+
for _, m := range d.Mismatched {
95+
fmt.Printf(" %s\n", cyan(m.Key))
96+
fmt.Printf(" %s: %s (line %d)\n", d.File1, m.Var1.Value, m.Var1.Line)
97+
fmt.Printf(" %s: %s (line %d)\n", d.File2, m.Var2.Value, m.Var2.Line)
98+
}
99+
}
100+
}
101+
102+
fmt.Printf("\n%s\n", green("Summary:"))
103+
fmt.Printf(" Total in %s: %d\n", d.File1, len(d.Extra)+len(d.Mismatched)+len(d.Matching))
104+
fmt.Printf(" Total in %s: %d\n", d.File2, len(d.Missing)+len(d.Mismatched)+len(d.Matching))
105+
fmt.Printf(" %s: %d\n", red("Missing"), len(d.Missing))
106+
fmt.Printf(" %s: %d\n", yellow("Extra"), len(d.Extra))
107+
fmt.Printf(" %s: %d\n", yellow("Mismatched"), len(d.Mismatched))
108+
fmt.Printf(" %s: %d\n", green("Matching"), len(d.Matching))
109+
110+
if d.HasDifferences() {
111+
fmt.Printf("\n%s\n", red("❌ Differences found!"))
112+
} else {
113+
fmt.Printf("\n%s\n", green("✓ Files are in sync!"))
114+
}
115+
}

differ_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestCompareEnvFiles(t *testing.T) {
8+
t.Run("detect missing variables", func(t *testing.T) {
9+
env1 := &EnvFile{
10+
Path: "file1",
11+
Vars: map[string]EnvVar{
12+
"KEY1": {Key: "KEY1", Value: "val1", Line: 1},
13+
},
14+
}
15+
env2 := &EnvFile{
16+
Path: "file2",
17+
Vars: map[string]EnvVar{
18+
"KEY1": {Key: "KEY1", Value: "val1", Line: 1},
19+
"KEY2": {Key: "KEY2", Value: "val2", Line: 2},
20+
},
21+
}
22+
23+
diff := CompareEnvFiles(env1, env2, "file1", "file2")
24+
25+
if len(diff.Missing) != 1 {
26+
t.Errorf("expected 1 missing var, got %d", len(diff.Missing))
27+
}
28+
if diff.Missing[0].Key != "KEY2" {
29+
t.Errorf("expected missing KEY2, got %s", diff.Missing[0].Key)
30+
}
31+
})
32+
33+
t.Run("detect extra variables", func(t *testing.T) {
34+
env1 := &EnvFile{
35+
Path: "file1",
36+
Vars: map[string]EnvVar{
37+
"KEY1": {Key: "KEY1", Value: "val1", Line: 1},
38+
"KEY2": {Key: "KEY2", Value: "val2", Line: 2},
39+
},
40+
}
41+
env2 := &EnvFile{
42+
Path: "file2",
43+
Vars: map[string]EnvVar{
44+
"KEY1": {Key: "KEY1", Value: "val1", Line: 1},
45+
},
46+
}
47+
48+
diff := CompareEnvFiles(env1, env2, "file1", "file2")
49+
50+
if len(diff.Extra) != 1 {
51+
t.Errorf("expected 1 extra var, got %d", len(diff.Extra))
52+
}
53+
})
54+
55+
t.Run("detect mismatched values", func(t *testing.T) {
56+
env1 := &EnvFile{
57+
Path: "file1",
58+
Vars: map[string]EnvVar{
59+
"KEY1": {Key: "KEY1", Value: "val1", Line: 1},
60+
},
61+
}
62+
env2 := &EnvFile{
63+
Path: "file2",
64+
Vars: map[string]EnvVar{
65+
"KEY1": {Key: "KEY1", Value: "val2", Line: 1},
66+
},
67+
}
68+
69+
diff := CompareEnvFiles(env1, env2, "file1", "file2")
70+
71+
if len(diff.Mismatched) != 1 {
72+
t.Errorf("expected 1 mismatch, got %d", len(diff.Mismatched))
73+
}
74+
})
75+
76+
t.Run("identical files", func(t *testing.T) {
77+
env1 := &EnvFile{
78+
Path: "file1",
79+
Vars: map[string]EnvVar{
80+
"KEY1": {Key: "KEY1", Value: "val1", Line: 1},
81+
},
82+
}
83+
env2 := &EnvFile{
84+
Path: "file2",
85+
Vars: map[string]EnvVar{
86+
"KEY1": {Key: "KEY1", Value: "val1", Line: 1},
87+
},
88+
}
89+
90+
diff := CompareEnvFiles(env1, env2, "file1", "file2")
91+
92+
if diff.HasDifferences() {
93+
t.Error("expected no differences for identical files")
94+
}
95+
if len(diff.Matching) != 1 {
96+
t.Errorf("expected 1 matching var, got %d", len(diff.Matching))
97+
}
98+
})
99+
}
100+
101+
func TestHasDifferences(t *testing.T) {
102+
tests := []struct {
103+
name string
104+
diff *DiffResult
105+
want bool
106+
}{
107+
{"no diffs", &DiffResult{}, false},
108+
{"has missing", &DiffResult{Missing: []EnvVar{{Key: "K"}}}, true},
109+
{"has extra", &DiffResult{Extra: []EnvVar{{Key: "K"}}}, true},
110+
{"has mismatch", &DiffResult{Mismatched: []MismatchedVar{{Key: "K"}}}, true},
111+
}
112+
113+
for _, tt := range tests {
114+
t.Run(tt.name, func(t *testing.T) {
115+
if got := tt.diff.HasDifferences(); got != tt.want {
116+
t.Errorf("HasDifferences() = %v, want %v", got, tt.want)
117+
}
118+
})
119+
}
120+
}

go.mod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module github.com/yourusername/env-diff
2+
3+
go 1.21
4+
5+
require (
6+
github.com/fatih/color v1.16.0
7+
github.com/spf13/cobra v1.8.0
8+
)
9+
10+
require (
11+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
12+
github.com/mattn/go-colorable v0.1.13 // indirect
13+
github.com/mattn/go-isatty v0.0.20 // indirect
14+
github.com/spf13/pflag v1.0.5 // indirect
15+
golang.org/x/sys v0.14.0 // indirect
16+
)

go.sum

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
2+
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
3+
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
4+
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
5+
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
6+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
7+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
8+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
9+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
10+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
11+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
12+
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
13+
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
14+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
15+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
16+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
17+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
18+
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
19+
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+27DHTzniMGhlvj9siN/CqBsk0KXU=
20+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
21+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)