| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"fmt" |
| 6 |
"io/ioutil" |
| 7 |
"os" |
| 8 |
|
| 9 |
"github.com/urfave/cli" |
| 10 |
|
| 11 |
diff "github.com/yudai/gojsondiff" |
| 12 |
"github.com/yudai/gojsondiff/formatter" |
| 13 |
) |
| 14 |
|
| 15 |
func main() { |
| 16 |
app := cli.NewApp() |
| 17 |
app.Name = "jd" |
| 18 |
app.Usage = "JSON Diff" |
| 19 |
app.Version = "0.0.2" |
| 20 |
|
| 21 |
app.Flags = []cli.Flag{ |
| 22 |
cli.StringFlag{ |
| 23 |
Name: "format, f", |
| 24 |
Value: "ascii", |
| 25 |
Usage: "Diff Output Format (ascii, delta)", |
| 26 |
EnvVar: "DIFF_FORMAT", |
| 27 |
}, |
| 28 |
cli.BoolFlag{ |
| 29 |
Name: "coloring, c", |
| 30 |
Usage: "Enable coloring in the ASCII mode (not available in the delta mode)", |
| 31 |
EnvVar: "COLORING", |
| 32 |
}, |
| 33 |
cli.BoolFlag{ |
| 34 |
Name: "quiet, q", |
| 35 |
Usage: "Suppress output, if no differences are found", |
| 36 |
EnvVar: "QUIET", |
| 37 |
}, |
| 38 |
} |
| 39 |
|
| 40 |
app.Action = func(c *cli.Context) error { |
| 41 |
if len(c.Args()) < 2 { |
| 42 |
fmt.Println("Not enough arguments.\n") |
| 43 |
fmt.Printf("Usage: %s json_file another_json_file\n", app.Name) |
| 44 |
os.Exit(1) |
| 45 |
} |
| 46 |
|
| 47 |
aFilePath := c.Args()[0] |
| 48 |
bFilePath := c.Args()[1] |
| 49 |
|
| 50 |
// Prepare your JSON string as `[]byte`, not `string` |
| 51 |
aString, err := ioutil.ReadFile(aFilePath) |
| 52 |
if err != nil { |
| 53 |
fmt.Printf("Failed to open file '%s': %s\n", aFilePath, err.Error()) |
| 54 |
os.Exit(2) |
| 55 |
} |
| 56 |
|
| 57 |
// Another JSON string |
| 58 |
bString, err := ioutil.ReadFile(bFilePath) |
| 59 |
if err != nil { |
| 60 |
fmt.Printf("Failed to open file '%s': %s\n", bFilePath, err.Error()) |
| 61 |
os.Exit(2) |
| 62 |
} |
| 63 |
|
| 64 |
// Then, compare them |
| 65 |
differ := diff.New() |
| 66 |
d, err := differ.Compare(aString, bString) |
| 67 |
if err != nil { |
| 68 |
fmt.Printf("Failed to unmarshal file: %s\n", err.Error()) |
| 69 |
os.Exit(3) |
| 70 |
} |
| 71 |
|
| 72 |
// Output the result |
| 73 |
if d.Modified() || !c.Bool("quiet") { |
| 74 |
format := c.String("format") |
| 75 |
var diffString string |
| 76 |
if format == "ascii" { |
| 77 |
var aJson map[string]interface{} |
| 78 |
json.Unmarshal(aString, &aJson) |
| 79 |
|
| 80 |
config := formatter.AsciiFormatterConfig{ |
| 81 |
ShowArrayIndex: true, |
| 82 |
Coloring: c.Bool("coloring"), |
| 83 |
} |
| 84 |
|
| 85 |
formatter := formatter.NewAsciiFormatter(aJson, config) |
| 86 |
diffString, err = formatter.Format(d) |
| 87 |
if err != nil { |
| 88 |
// No error can occur |
| 89 |
} |
| 90 |
} else if format == "delta" { |
| 91 |
formatter := formatter.NewDeltaFormatter() |
| 92 |
diffString, err = formatter.Format(d) |
| 93 |
if err != nil { |
| 94 |
// No error can occur |
| 95 |
} |
| 96 |
} else { |
| 97 |
fmt.Printf("Unknown Foramt %s\n", format) |
| 98 |
os.Exit(4) |
| 99 |
} |
| 100 |
|
| 101 |
fmt.Print(diffString) |
| 102 |
return cli.NewExitError("", 1) |
| 103 |
} |
| 104 |
return nil |
| 105 |
} |
| 106 |
|
| 107 |
app.Run(os.Args) |
| 108 |
} |