-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathldifdiff.go
128 lines (116 loc) · 3.31 KB
/
ldifdiff.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//Compare two LDIF files and output the differences as a valid LDIF.
//Bugs to https://github.com/nxadm/ldifdiff.
//
// _ _ _ _ _ _ _ _
// _-(_)- _-(_)- _-(_)- _-(")- _-(_)- _-(_)- _-(_)- _-(_)-
//*(___) *(___) *(___) *%%%%% *(___) *(___) *(___) *(___)
// // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\
//
//Usage:
//ldifdiff <source> <target> [-i <attributes> ...] [-d]
//ldifdiff -h
//ldifdiff -v
//Options:
//-d, --dn
// Only print DNs instead of a full LDIF.
//-i <attributes>, --ignore <attributes>
// Comma separated attribute list to be ignored.
// Multiple instances of this switch are allowed.
//-h, --help
// Show this screen.
//-v, --version
// Show version.
package main
import (
"fmt"
docopt "github.com/docopt/docopt-go"
"github.com/nxadm/ldifdiff"
"os"
"strings"
)
type Params struct {
Source, Target string
IgnoreAttr []string
DnOnly bool
}
var versionMsg = "ldifdiff " + ldifdiff.Version + " (" + ldifdiff.Author + ")."
var usage = versionMsg + "\n" +
"Compare two LDIF files and output the differences as a valid LDIF.\n" +
"Bugs to " + ldifdiff.Repo + ".\n" + `
_ _ _ _ _ _ _ _
_-(_)- _-(_)- _-(_)- _-(")- _-(_)- _-(_)- _-(_)- _-(_)-
*(___) *(___) *(___) *%%%%% *(___) *(___) *(___) *(___)
// \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\
Usage:
ldifdiff <source> <target> [-i <attributes> ...] [-d]
ldifdiff -h
ldifdiff -v
Options:
-d, --dn
Only print DNs instead of a full LDIF.
-i <attributes>, --ignore <attributes>
Comma separated attribute list to be ignored.
Multiple instances of this switch are allowed.
-h, --help
Show this screen.
-v, --version
Show version
`
func main() {
params := Params{}
params.parse()
/* DiffFromFiles the files */
var output string
var err error
switch params.DnOnly {
case true:
var outputList []string
outputList, err = ldifdiff.ListDiffDnFromFiles(params.Source, params.Target, params.IgnoreAttr)
output = strings.Join(outputList, "\n") + "\n"
default:
output, err = ldifdiff.DiffFromFiles(params.Source, params.Target, params.IgnoreAttr)
}
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
fmt.Printf("%s", output)
}
func (params *Params) parse() {
args, err := docopt.Parse(usage, nil, true, versionMsg, false)
switch {
case err != nil:
fmt.Fprintf(os.Stderr, "Error parsing the command line arguments:\n%v\n", err)
os.Exit(1)
case args["--dn"].(bool):
params.DnOnly = true
fallthrough
case args["--ignore"].([]string) != nil:
params.IgnoreAttr = args["--ignore"].([]string)
fallthrough
case args["<source>"].(string) != "":
params.Source = args["<source>"].(string)
fallthrough
case args["<target>"].(string) != "":
params.Target = args["<target>"].(string)
}
errMsgs := []string{}
switch {
case params.Source == params.Target:
fmt.Fprintln(os.Stderr, "Source and target ldif files are the same.")
os.Exit(1)
default:
if _, err := os.Stat(params.Source); err != nil {
errMsgs = append(errMsgs, err.Error())
}
if _, err := os.Stat(params.Target); err != nil {
errMsgs = append(errMsgs, err.Error())
}
}
if len(errMsgs) > 0 {
for _, msg := range errMsgs {
fmt.Fprintf(os.Stderr, "%s\n", msg)
}
os.Exit(1)
}
}