github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/cmd/noms/noms_show.go (about) 1 // Copyright 2019 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // This file incorporates work covered by the following copyright and 16 // permission notice: 17 // 18 // Copyright 2016 Attic Labs, Inc. All rights reserved. 19 // Licensed under the Apache License, version 2.0: 20 // http://www.apache.org/licenses/LICENSE-2.0 21 22 package main 23 24 import ( 25 "bytes" 26 "context" 27 "fmt" 28 "io" 29 "os" 30 31 flag "github.com/juju/gnuflag" 32 33 "github.com/dolthub/dolt/go/store/cmd/noms/util" 34 "github.com/dolthub/dolt/go/store/config" 35 "github.com/dolthub/dolt/go/store/types" 36 "github.com/dolthub/dolt/go/store/util/datetime" 37 "github.com/dolthub/dolt/go/store/util/outputpager" 38 "github.com/dolthub/dolt/go/store/util/verbose" 39 ) 40 41 var nomsShow = &util.Command{ 42 Run: runShow, 43 UsageLine: "show [flags] <object>", 44 Short: "Shows a serialization of a Noms object", 45 Long: "See Spelling Objects at https://github.com/attic-labs/noms/blob/master/doc/spelling.md for details on the object argument.", 46 Flags: setupShowFlags, 47 Nargs: 1, 48 } 49 50 var ( 51 showRaw = false 52 showStats = false 53 showPages = false 54 tzName string 55 ) 56 57 func setupShowFlags() *flag.FlagSet { 58 showFlagSet := flag.NewFlagSet("show", flag.ExitOnError) 59 outputpager.RegisterOutputpagerFlags(showFlagSet) 60 verbose.RegisterVerboseFlags(showFlagSet) 61 showFlagSet.BoolVar(&showPages, "page", false, "If true output is shown in an output pager") 62 showFlagSet.BoolVar(&showRaw, "raw", false, "If true, dumps the raw binary version of the data") 63 showFlagSet.BoolVar(&showStats, "stats", false, "If true, reports statistics related to the value") 64 showFlagSet.StringVar(&tzName, "tz", "local", "display formatted date comments in specified timezone, must be: local or utc") 65 return showFlagSet 66 } 67 68 func runShow(ctx context.Context, args []string) int { 69 cfg := config.NewResolver() 70 database, value, err := cfg.GetPath(ctx, args[0]) 71 util.CheckErrorNoUsage(err) 72 defer database.Close() 73 74 if value == nil { 75 fmt.Fprintf(os.Stderr, "Object not found: %s\n", args[0]) 76 return 0 77 } 78 79 if showRaw && showStats { 80 fmt.Fprintln(os.Stderr, "--raw and --stats are mutually exclusive") 81 return 0 82 } 83 84 if showRaw { 85 ch, err := types.EncodeValue(value, database.Format()) 86 util.CheckError(err) 87 buf := bytes.NewBuffer(ch.Data()) 88 _, err = io.Copy(os.Stdout, buf) 89 util.CheckError(err) 90 return 0 91 } 92 93 if showStats { 94 types.WriteValueStats(ctx, os.Stdout, value, database) 95 return 0 96 } 97 98 tz, _ := locationFromTimezoneArg(tzName, nil) 99 datetime.RegisterHRSCommenter(tz) 100 101 if showPages { 102 pgr := outputpager.Start() 103 defer pgr.Stop() 104 105 types.WriteEncodedValue(ctx, pgr.Writer, value) 106 fmt.Fprintln(pgr.Writer) 107 } else { 108 types.WriteEncodedValue(ctx, os.Stdout, value) 109 } 110 111 return 0 112 }