Add ability to register HRSCommenters on Structs. (#3609)

Clients can register HRSCommenters to cause additional info
to be included as comments when generating the human readable
encoding for Noms Structs.
This commit is contained in:
Dan Willhite
2017-09-13 17:21:08 -07:00
committed by GitHub
parent 26eb9e3713
commit 10ec10dc00
9 changed files with 203 additions and 13 deletions
+2
View File
@@ -168,6 +168,7 @@ See Spelling Values at https://github.com/attic-labs/noms/blob/master/doc/spelli
log.Flag("oneline", "show a summary of each commit on a single line").Bool()
log.Flag("graph", "show ascii-based commit hierarchy on left side of output").Bool()
log.Flag("show-value", "show commit value rather than diff information").Bool()
log.Flag("tz", "display formatted date comments in specified timezone, must be: local or utc").Enum("local", "utc")
log.Arg("path-spec", "").Required().String()
// merge
@@ -201,6 +202,7 @@ See Spelling Objects at https://github.com/attic-labs/noms/blob/master/doc/spell
`)
show.Flag("raw", "If true, dumps the raw binary version of the data").Bool()
show.Flag("stats", "If true, reports statistics related to the value").Bool()
show.Flag("tz", "display formatted date comments in specified timezone, must be: local or utc").Enum("local", "utc")
show.Arg("object", "a noms object").Required().String()
// sync
+1 -1
View File
@@ -164,7 +164,7 @@ func (s *nomsCommitTestSuite) TestNomsCommitMetadata() {
metaOld = metaNew
stdoutString, stderrString = s.MustRun(main, []string{"commit", "--allow-dupe=1", "--meta=message=bar", "--date=" + spec.CommitMetaDateFormat, dsName + ".value", sp.String()})
stdoutString, stderrString = s.MustRun(main, []string{"commit", "--allow-dupe=1", "--meta=message=bar", "--date=" + spec.CommitMetaDateFormat[:20], dsName + ".value", sp.String()})
s.Empty(stderrString)
s.Contains(stdoutString, "New head #")
+27 -6
View File
@@ -6,11 +6,13 @@ package main
import (
"bytes"
"errors"
"fmt"
"io"
"math"
"os"
"strings"
"time"
"github.com/attic-labs/noms/cmd/util"
"github.com/attic-labs/noms/go/config"
@@ -57,6 +59,7 @@ func setupLogFlags() *flag.FlagSet {
logFlagSet.BoolVar(&oneline, "oneline", false, "show a summary of each commit on a single line")
logFlagSet.BoolVar(&showGraph, "graph", false, "show ascii-based commit hierarchy on left side of output")
logFlagSet.BoolVar(&showValue, "show-value", false, "show commit value rather than diff information")
logFlagSet.StringVar(&tzName, "tz", "local", "display formatted date comments in specified timezone, must be: local or utc")
outputpager.RegisterOutputpagerFlags(logFlagSet)
verbose.RegisterVerboseFlags(logFlagSet)
return logFlagSet
@@ -66,6 +69,9 @@ func runLog(args []string) int {
useColor = shouldUseColor()
cfg := config.NewResolver()
tz, _ := locationFromTimezoneArg(tzName, nil)
datetime.RegisterHRSCommenter(tz)
resolved := cfg.ResolvePathSpec(args[0])
sp, err := spec.ForPath(resolved)
d.CheckErrorNoUsage(err)
@@ -107,7 +113,7 @@ func runLog(args []string) int {
go func(ch chan []byte, node LogNode) {
buff := &bytes.Buffer{}
printCommit(node, path, buff, database)
printCommit(node, path, buff, database, tz)
ch <- buff.Bytes()
}(ch, ln)
@@ -135,7 +141,7 @@ func runLog(args []string) int {
// Prints the information for one commit in the log, including ascii graph on left side of commits if
// -graph arg is true.
func printCommit(node LogNode, path types.Path, w io.Writer, db datas.Database) (err error) {
func printCommit(node LogNode, path types.Path, w io.Writer, db datas.Database, tz *time.Location) (err error) {
maxMetaFieldNameLength := func(commit types.Struct) int {
maxLen := 0
if m, ok := commit.MaybeGet(datas.MetaField); ok {
@@ -181,7 +187,7 @@ func printCommit(node LogNode, path types.Path, w io.Writer, db datas.Database)
lineno := 1
if maxLines != 0 {
lineno, err = writeMetaLines(node, maxLines, lineno, maxFieldNameLen, w)
lineno, err = writeMetaLines(node, maxLines, lineno, maxFieldNameLen, w, tz)
if err != nil && err != writers.MaxLinesErr {
fmt.Fprintf(w, "error: %s\n", err)
return
@@ -249,7 +255,7 @@ func genGraph(node LogNode, lineno int) string {
return string(buf)
}
func writeMetaLines(node LogNode, maxLines, lineno, maxLabelLen int, w io.Writer) (int, error) {
func writeMetaLines(node LogNode, maxLines, lineno, maxLabelLen int, w io.Writer, tz *time.Location) (int, error) {
if m, ok := node.commit.MaybeGet(datas.MetaField); ok {
genPrefix := func(w *writers.PrefixWriter) []byte {
return []byte(genGraph(node, int(w.NumLines)))
@@ -261,14 +267,16 @@ func writeMetaLines(node LogNode, maxLines, lineno, maxLabelLen int, w io.Writer
types.TypeOf(meta).Desc.(types.StructDesc).IterFields(func(fieldName string, t *types.Type, optional bool) {
v := meta.Get(fieldName)
fmt.Fprintf(pw, "%-*s", maxLabelLen+2, strings.Title(fieldName)+":")
// Encode dates as formatted string if this is a top-level meta
// field of type datetime.DateTimeType
if types.TypeOf(v).Equals(datetime.DateTimeType) {
var dt datetime.DateTime
dt.UnmarshalNoms(v)
fmt.Fprintf(pw, dt.Format(spec.CommitMetaDateFormat))
fmt.Fprintln(pw, dt.In(tz).Format(time.RFC3339))
} else {
types.WriteEncodedValue(pw, v)
}
fmt.Fprintf(pw, "\n")
fmt.Fprintln(pw)
})
})
return int(pw.NumLines), err
@@ -374,3 +382,16 @@ func min(i, j int) int {
}
return j
}
func locationFromTimezoneArg(tz string, defaultTZ *time.Location) (*time.Location, error) {
switch tz {
case "local":
return time.Local, nil
case "utc":
return time.UTC, nil
case "":
return defaultTZ, nil
default:
return nil, errors.New("value must be: local or utc")
}
}
+10 -2
View File
@@ -14,6 +14,7 @@ import (
"github.com/attic-labs/noms/go/config"
"github.com/attic-labs/noms/go/d"
"github.com/attic-labs/noms/go/types"
"github.com/attic-labs/noms/go/util/datetime"
"github.com/attic-labs/noms/go/util/outputpager"
"github.com/attic-labs/noms/go/util/verbose"
flag "github.com/juju/gnuflag"
@@ -28,8 +29,11 @@ var nomsShow = &util.Command{
Nargs: 1,
}
var showRaw = false
var showStats = false
var (
showRaw = false
showStats = false
tzName string
)
func setupShowFlags() *flag.FlagSet {
showFlagSet := flag.NewFlagSet("show", flag.ExitOnError)
@@ -37,6 +41,7 @@ func setupShowFlags() *flag.FlagSet {
verbose.RegisterVerboseFlags(showFlagSet)
showFlagSet.BoolVar(&showRaw, "raw", false, "If true, dumps the raw binary version of the data")
showFlagSet.BoolVar(&showStats, "stats", false, "If true, reports statistics related to the value")
showFlagSet.StringVar(&tzName, "tz", "local", "display formatted date comments in specified timezone, must be: local or utc")
return showFlagSet
}
@@ -69,6 +74,9 @@ func runShow(args []string) int {
return 0
}
tz, _ := locationFromTimezoneArg(tzName, nil)
datetime.RegisterHRSCommenter(tz)
pgr := outputpager.Start()
defer pgr.Stop()