Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sahilm committed Aug 3, 2017
0 parents commit cf2b8fa
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Sahil Muthoo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
45 changes: 45 additions & 0 deletions README.md
@@ -0,0 +1,45 @@
# yamldiff

A CLI tool to diff two YAML/JSON files.

Nothing fancy about the code, all the heavy liftin' is done by:

* [go-yaml](https://github.com/go-yaml/yaml/) - for YAML parsin'
* [godebug](https://github.com/kylelemons/godebug/) - for diffin'
* [aurora](https://github.com/logrusorgru/aurora) - for fancy printin'
* [go-flags](https://github.com/jessevdk/go-flags) - for flaggin'
* [The Go stdlib](https://golang.org/pkg/) - for everythin'

Thanks to all the contributors of the above libraries.

## Installation

`go get -u github.com/sahilm/yamldiff`

## Usage

`yamldiff --yamlfile1 /path/to/yamlfile1.yml --yamlfile2 /path/to/yamlfile2.yml`

## License

The MIT License (MIT)

Copyright (c) 2017 Sahil Muthoo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
94 changes: 94 additions & 0 deletions main.go
@@ -0,0 +1,94 @@
package main

import (
"fmt"
"io/ioutil"
"os"

"strings"

"github.com/jessevdk/go-flags"
"github.com/kylelemons/godebug/pretty"
"github.com/logrusorgru/aurora"
"gopkg.in/yaml.v2"
)

func main() {
var opts struct {
Yamlfile1 string `long:"yamlfile1" description:"first YAML file" required:"true"`
Yamlfile2 string `long:"yamlfile2" description:"second YAML file" required:"true"`
}

_, err := flags.Parse(&opts)
if err != nil {
os.Exit(1)
}

errors := stat(opts.Yamlfile1, opts.Yamlfile2)
failOnErr(errors...)

yaml1, err := unmarshal(opts.Yamlfile1)
if err != nil {
failOnErr(err)
}
yaml2, err := unmarshal(opts.Yamlfile2)
if err != nil {
failOnErr(err)
}

diff := computeDiff(yaml1, yaml2)
if diff != "" {
fmt.Printf("%v\n%v\n", aurora.Bold("diff"), diff)
} else {
fmt.Println(aurora.Bold("no diff"))
}
}

func stat(filenames ...string) []error {
var errs []error
for _, filename := range filenames {
_, err := os.Stat(filename)
if err != nil {
errs = append(errs, fmt.Errorf("cannot find file: %v. Does it exist?", filename))
}
}
return errs
}

func unmarshal(filename string) (map[interface{}]interface{}, error) {
contents, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var ret map[interface{}]interface{}
yaml.Unmarshal(contents, &ret)
return ret, nil
}

func failOnErr(errs ...error) {
if len(errs) > 0 {
var errMessages []string
for _, err := range errs {
errMessages = append(errMessages, err.Error())
}
fmt.Fprintf(os.Stderr, "%v\n\n", aurora.Red(strings.Join(errMessages, "\n")))
os.Exit(1)
}
}

func computeDiff(a interface{}, b interface{}) string {
var stringers []fmt.Stringer
for _, s := range strings.Split(pretty.Compare(a, b), "\n") {
switch {
case strings.HasPrefix(s, "+"):
stringers = append(stringers, aurora.Bold(aurora.Green(s)))
case strings.HasPrefix(s, "-"):
stringers = append(stringers, aurora.Bold(aurora.Red(s)))
}
}
var s []string
for _, stringer := range stringers {
s = append(s, stringer.String())
}
return strings.Join(s, "\n")
}

0 comments on commit cf2b8fa

Please sign in to comment.