github.com/ndau/noms@v1.0.5/cmd/noms/noms_diff.go (about)

     1  // Copyright 2016 Attic Labs, Inc. All rights reserved.
     2  // Licensed under the Apache License, version 2.0:
     3  // http://www.apache.org/licenses/LICENSE-2.0
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/attic-labs/kingpin"
    11  	"github.com/ndau/noms/cmd/util"
    12  	"github.com/ndau/noms/go/config"
    13  	"github.com/ndau/noms/go/d"
    14  	"github.com/ndau/noms/go/diff"
    15  	"github.com/ndau/noms/go/util/outputpager"
    16  )
    17  
    18  func nomsDiff(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
    19  	cmd := noms.Command("diff", "Shows the difference between two values.")
    20  	stat := cmd.Flag("stat", "writes a summary of the changes instead").Bool()
    21  	o1 := cmd.Arg("val1", "first value - see Spelling Values at https://github.com/ndau/noms/blob/master/doc/spelling.md").Required().String()
    22  	o2 := cmd.Arg("val2", "second value - see Spelling Values at https://github.com/ndau/noms/blob/master/doc/spelling.md").Required().String()
    23  	outputpager.RegisterOutputpagerFlags(cmd)
    24  
    25  	return cmd, func(input string) int {
    26  		cfg := config.NewResolver()
    27  		db1, value1, err := cfg.GetPath(*o1)
    28  		d.CheckErrorNoUsage(err)
    29  		if value1 == nil {
    30  			d.CheckErrorNoUsage(fmt.Errorf("Value not found: %s", *o1))
    31  		}
    32  		defer db1.Close()
    33  
    34  		db2, value2, err := cfg.GetPath(*o2)
    35  		d.CheckErrorNoUsage(err)
    36  		if value2 == nil {
    37  			d.CheckErrorNoUsage(fmt.Errorf("Value not found: %s", *o2))
    38  		}
    39  		defer db2.Close()
    40  
    41  		if *stat {
    42  			diff.Summary(value1, value2)
    43  			return 0
    44  		}
    45  
    46  		pgr := outputpager.Start()
    47  		defer pgr.Stop()
    48  
    49  		diff.PrintDiff(pgr.Writer, value1, value2, false)
    50  		return 0
    51  	}
    52  }