-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.go
More file actions
70 lines (60 loc) · 1.37 KB
/
main.go
File metadata and controls
70 lines (60 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Command pkg-diff-example implements a subset of the diff command using
// github.com/pkg/diff.
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/pkg/diff"
"github.com/pkg/diff/write"
)
var color = flag.Bool("color", false, "colorize the output")
// check logs a fatal error and exits if err is not nil.
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func usage() {
fmt.Fprintf(os.Stderr, "pkg-diff-example [flags] file1 file2\n")
flag.PrintDefaults()
os.Exit(2)
}
func main() {
log.SetPrefix("pkg-diff-example: ")
log.SetFlags(0)
flag.Usage = usage
flag.Parse()
var aName, bName string
var a, b interface{}
switch flag.NArg() {
case 2:
// A human has invoked us.
aName, bName = flag.Arg(0), flag.Arg(1)
case 7, 9:
// We are a git external diff tool.
// We have been passed the following arguments:
// path old-file old-hex old-mode new-file new-hex new-mode [new-path similarity-metrics]
aName = "a/" + flag.Arg(0)
if flag.NArg() == 7 {
bName = "b/" + flag.Arg(0)
} else {
bName = "b/" + flag.Arg(7)
}
var err error
a, err = ioutil.ReadFile(flag.Arg(1))
check(err)
b, err = ioutil.ReadFile(flag.Arg(4))
check(err)
default:
flag.Usage()
}
var opts []write.Option
if *color {
opts = append(opts, write.TerminalColor())
}
err := diff.Text(aName, bName, a, b, os.Stdout, opts...)
check(err)
}