github.com/tacerus/ldifdiff@v0.0.0-20181030102753-4dccbe38183b/cmd/ldifdiff.go (about)

     1  //Compare two LDIF files and output the differences as a valid LDIF.
     2  //Bugs to https://github.com/nxadm/ldifdiff.
     3  //
     4  //    _       _       _       _       _       _       _       _
     5  // _-(_)-  _-(_)-  _-(_)-  _-(")-  _-(_)-  _-(_)-  _-(_)-  _-(_)-
     6  //*(___)  *(___)  *(___)  *%%%%%  *(___)  *(___)  *(___)  *(___)
     7  // // \\   // \\   // \\   // \\   // \\   // \\   // \\   // \\
     8  //
     9  //Usage:
    10  //ldifdiff <source> <target> [-i <attributes> ...] [-d]
    11  //ldifdiff -h
    12  //ldifdiff -v
    13  //Options:
    14  //-d, --dn
    15  //  Only print DNs instead of a full LDIF.
    16  //-i <attributes>, --ignore <attributes>
    17  //  Comma separated attribute list to be ignored.
    18  //  Multiple instances of this switch are allowed.
    19  //-h, --help
    20  //  Show this screen.
    21  //-v, --version
    22  //  Show version.
    23  package main
    24  
    25  import (
    26  	"fmt"
    27  	docopt "github.com/docopt/docopt-go"
    28  	"github.com/nxadm/ldifdiff"
    29  	"os"
    30  	"strings"
    31  )
    32  
    33  type Params struct {
    34  	Source, Target string
    35  	IgnoreAttr     []string
    36  	DnOnly         bool
    37  }
    38  
    39  var versionMsg = "ldifdiff " + ldifdiff.Version + " (" + ldifdiff.Author + ")."
    40  var usage = versionMsg + "\n" +
    41  	"Compare two LDIF files and output the differences as a valid LDIF.\n" +
    42  	"Bugs to " + ldifdiff.Repo + ".\n" + `
    43         _       _       _       _       _       _       _       _
    44      _-(_)-  _-(_)-  _-(_)-  _-(")-  _-(_)-  _-(_)-  _-(_)-  _-(_)-
    45    *(___)  *(___)  *(___)  *%%%%%  *(___)  *(___)  *(___)  *(___)
    46    // \\   // \\   // \\   // \\   // \\   // \\   // \\   // \\
    47  
    48  Usage:
    49    ldifdiff <source> <target> [-i <attributes> ...] [-d]
    50    ldifdiff -h
    51    ldifdiff -v
    52  Options:
    53    -d, --dn
    54      Only print DNs instead of a full LDIF.
    55    -i <attributes>, --ignore <attributes>
    56  	Comma separated attribute list to be ignored.
    57  	Multiple instances of this switch are allowed.
    58    -h, --help
    59    	Show this screen.
    60    -v, --version
    61    	Show version
    62  `
    63  
    64  func main() {
    65  	params := Params{}
    66  	params.parse()
    67  
    68  	/* DiffFromFiles the files */
    69  	var output string
    70  	var err error
    71  	switch params.DnOnly {
    72  	case true:
    73  		var outputList []string
    74  		outputList, err = ldifdiff.ListDiffDnFromFiles(params.Source, params.Target, params.IgnoreAttr)
    75  		output = strings.Join(outputList, "\n") + "\n"
    76  	default:
    77  		output, err = ldifdiff.DiffFromFiles(params.Source, params.Target, params.IgnoreAttr)
    78  	}
    79  
    80  	if err != nil {
    81  		fmt.Fprintf(os.Stderr, "%s\n", err)
    82  		os.Exit(1)
    83  	}
    84  
    85  	fmt.Printf("%s", output)
    86  }
    87  
    88  func (params *Params) parse() {
    89  	args, err := docopt.Parse(usage, nil, true, versionMsg, false)
    90  
    91  	switch {
    92  	case err != nil:
    93  		fmt.Fprintf(os.Stderr, "Error parsing the command line arguments:\n%v\n", err)
    94  		os.Exit(1)
    95  	case args["--dn"].(bool):
    96  		params.DnOnly = true
    97  		fallthrough
    98  	case args["--ignore"].([]string) != nil:
    99  		params.IgnoreAttr = args["--ignore"].([]string)
   100  		fallthrough
   101  	case args["<source>"].(string) != "":
   102  		params.Source = args["<source>"].(string)
   103  		fallthrough
   104  	case args["<target>"].(string) != "":
   105  		params.Target = args["<target>"].(string)
   106  	}
   107  
   108  	errMsgs := []string{}
   109  	switch {
   110  	case params.Source == params.Target:
   111  		fmt.Fprintln(os.Stderr, "Source and target ldif files are the same.")
   112  		os.Exit(1)
   113  	default:
   114  		if _, err := os.Stat(params.Source); err != nil {
   115  			errMsgs = append(errMsgs, err.Error())
   116  		}
   117  		if _, err := os.Stat(params.Target); err != nil {
   118  			errMsgs = append(errMsgs, err.Error())
   119  		}
   120  	}
   121  	if len(errMsgs) > 0 {
   122  		for _, msg := range errMsgs {
   123  			fmt.Fprintf(os.Stderr, "%s\n", msg)
   124  		}
   125  		os.Exit(1)
   126  	}
   127  
   128  }