github.com/jbendotnet/noms@v0.0.0-20190904222105-c43e4293ea92/cmd/noms/noms_show.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  	"bytes"
     9  	"fmt"
    10  	"io"
    11  	"os"
    12  
    13  	"github.com/attic-labs/kingpin"
    14  	"github.com/attic-labs/noms/cmd/util"
    15  	"github.com/attic-labs/noms/go/config"
    16  	"github.com/attic-labs/noms/go/d"
    17  	"github.com/attic-labs/noms/go/types"
    18  	"github.com/attic-labs/noms/go/util/datetime"
    19  	"github.com/attic-labs/noms/go/util/outputpager"
    20  )
    21  
    22  func nomsShow(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
    23  	cmd := noms.Command("show", "Print Noms values.")
    24  	showRaw := cmd.Flag("raw", "dump the value in binary format").Bool()
    25  	showStats := cmd.Flag("stats", "report statics related to the value").Bool()
    26  	tzName := cmd.Flag("tz", "display formatted date comments in specified timezone, must be: local or utc").Default("local").String()
    27  	path := cmd.Arg("path", "value to display - see Spelling Values at https://github.com/attic-labs/noms/blob/master/doc/spelling.md").Required().String()
    28  
    29  	return cmd, func(_ string) int {
    30  		cfg := config.NewResolver()
    31  		database, value, err := cfg.GetPath(*path)
    32  		d.CheckErrorNoUsage(err)
    33  		defer database.Close()
    34  
    35  		if value == nil {
    36  			fmt.Fprintf(os.Stderr, "Value not found: %s\n", *path)
    37  			return 0
    38  		}
    39  
    40  		if *showRaw && *showStats {
    41  			fmt.Fprintln(os.Stderr, "--raw and --stats are mutually exclusive")
    42  			return 0
    43  		}
    44  
    45  		if *showRaw {
    46  			ch := types.EncodeValue(value)
    47  			buf := bytes.NewBuffer(ch.Data())
    48  			_, err = io.Copy(os.Stdout, buf)
    49  			d.CheckError(err)
    50  			return 0
    51  		}
    52  
    53  		if *showStats {
    54  			types.WriteValueStats(os.Stdout, value, database)
    55  			return 0
    56  		}
    57  
    58  		tz, _ := locationFromTimezoneArg(*tzName, nil)
    59  		datetime.RegisterHRSCommenter(tz)
    60  
    61  		pgr := outputpager.Start()
    62  		defer pgr.Stop()
    63  
    64  		types.WriteEncodedValue(pgr.Writer, value)
    65  		fmt.Fprintln(pgr.Writer)
    66  		return 0
    67  	}
    68  }