mirror of
https://github.com/dolthub/dolt.git
synced 2026-05-04 11:30:14 -05:00
cmd/noms: Contextify sub-commands.
This commit is contained in:
@@ -30,7 +30,7 @@ func NewCommitIterator(db datas.Database, commit types.Struct) *CommitIterator {
|
||||
// parents, new branches are added to the branchlist so that they can be traversed in order. When
|
||||
// more than one branch contains the same node, that indicates that the branches are converging and so
|
||||
// the branchlist will have branches removed to reflect that.
|
||||
func (iter *CommitIterator) Next() (LogNode, bool) {
|
||||
func (iter *CommitIterator) Next(ctx context.Context) (LogNode, bool) {
|
||||
if iter.branches.IsEmpty() {
|
||||
return LogNode{}, false
|
||||
}
|
||||
@@ -49,9 +49,9 @@ func (iter *CommitIterator) Next() (LogNode, bool) {
|
||||
// If this commit has parents, then a branch is splitting. Create a branch for each of the parents
|
||||
// and splice that into the iterators list of branches.
|
||||
branches := branchList{}
|
||||
parents := commitRefsFromSet(br.commit.Get(datas.ParentsField).(types.Set))
|
||||
parents := commitRefsFromSet(ctx, br.commit.Get(datas.ParentsField).(types.Set))
|
||||
for _, p := range parents {
|
||||
b := branch{cr: p, commit: iter.db.ReadValue(context.Background(), p.TargetHash()).(types.Struct)}
|
||||
b := branch{cr: p, commit: iter.db.ReadValue(ctx, p.TargetHash()).(types.Struct)}
|
||||
branches = append(branches, b)
|
||||
}
|
||||
iter.branches = iter.branches.Splice(col, 1, branches...)
|
||||
@@ -156,9 +156,9 @@ func (bl branchList) RemoveBranches(indexes []int) branchList {
|
||||
return bl
|
||||
}
|
||||
|
||||
func commitRefsFromSet(set types.Set) []types.Ref {
|
||||
func commitRefsFromSet(ctx context.Context, set types.Set) []types.Ref {
|
||||
res := []types.Ref{}
|
||||
set.IterAll(context.Background(), func(v types.Value) {
|
||||
set.IterAll(ctx, func(v types.Value) {
|
||||
res = append(res, v.(types.Ref))
|
||||
})
|
||||
return res
|
||||
|
||||
+3
-2
@@ -5,6 +5,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
@@ -88,7 +89,7 @@ func main() {
|
||||
|
||||
// install kingpin handlers
|
||||
for _, cmdFunction := range kingpinCommands {
|
||||
command, handler := cmdFunction(noms)
|
||||
command, handler := cmdFunction(context.Background(), noms)
|
||||
handlers[command.FullCommand()] = handler
|
||||
}
|
||||
|
||||
@@ -122,7 +123,7 @@ func main() {
|
||||
if cmd.Nargs != 0 && len(args) < cmd.Nargs {
|
||||
cmd.Usage()
|
||||
}
|
||||
exitCode := cmd.Run(args)
|
||||
exitCode := cmd.Run(context.Background(), args)
|
||||
if exitCode != 0 {
|
||||
exit.Exit(exitCode)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"runtime"
|
||||
"strconv"
|
||||
|
||||
@@ -14,7 +15,7 @@ import (
|
||||
"github.com/attic-labs/noms/go/d"
|
||||
)
|
||||
|
||||
func nomsBlob(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
|
||||
func nomsBlob(ctx context.Context, noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
|
||||
blob := noms.Command("blob", "interact with blobs")
|
||||
|
||||
blobPut := blob.Command("put", "imports a blob to a dataset")
|
||||
@@ -29,9 +30,9 @@ func nomsBlob(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandle
|
||||
return blob, func(input string) int {
|
||||
switch input {
|
||||
case blobPut.FullCommand():
|
||||
return nomsBlobPut(*putFile, *putDs, *concurrency)
|
||||
return nomsBlobPut(ctx, *putFile, *putDs, *concurrency)
|
||||
case blobGet.FullCommand():
|
||||
return nomsBlobGet(*getDs, *getPath)
|
||||
return nomsBlobGet(ctx, *getDs, *getPath)
|
||||
}
|
||||
d.Panic("notreached")
|
||||
return 1
|
||||
|
||||
@@ -20,10 +20,10 @@ import (
|
||||
humanize "github.com/dustin/go-humanize"
|
||||
)
|
||||
|
||||
func nomsBlobGet(ds string, filePath string) int {
|
||||
func nomsBlobGet(ctx context.Context, ds string, filePath string) int {
|
||||
cfg := config.NewResolver()
|
||||
var blob types.Blob
|
||||
if db, val, err := cfg.GetPath(context.Background(), ds); err != nil {
|
||||
if db, val, err := cfg.GetPath(ctx, ds); err != nil {
|
||||
d.CheckErrorNoUsage(err)
|
||||
} else if val == nil {
|
||||
d.CheckErrorNoUsage(fmt.Errorf("No value at %s", ds))
|
||||
@@ -37,7 +37,7 @@ func nomsBlobGet(ds string, filePath string) int {
|
||||
defer profile.MaybeStartProfile().Stop()
|
||||
|
||||
if filePath == "" {
|
||||
blob.Copy(context.Background(), os.Stdout)
|
||||
blob.Copy(ctx, os.Stdout)
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ func nomsBlobGet(ds string, filePath string) int {
|
||||
preader, pwriter := io.Pipe()
|
||||
|
||||
go func() {
|
||||
blob.Copy(context.Background(), pwriter)
|
||||
blob.Copy(ctx, pwriter)
|
||||
pwriter.Close()
|
||||
}()
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
"github.com/attic-labs/noms/go/util/profile"
|
||||
)
|
||||
|
||||
func nomsBlobPut(filePath string, dsPath string, concurrency int) int {
|
||||
func nomsBlobPut(ctx context.Context, filePath string, dsPath string, concurrency int) int {
|
||||
info, err := os.Stat(filePath)
|
||||
if err != nil {
|
||||
d.CheckError(errors.New("couldn't stat file"))
|
||||
@@ -46,16 +46,16 @@ func nomsBlobPut(filePath string, dsPath string, concurrency int) int {
|
||||
}
|
||||
|
||||
cfg := config.NewResolver()
|
||||
db, ds, err := cfg.GetDataset(context.Background(), dsPath)
|
||||
db, ds, err := cfg.GetDataset(ctx, dsPath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Could not create dataset: %s\n", err)
|
||||
return 1
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
blob := types.NewBlob(context.Background(), db, readers...)
|
||||
blob := types.NewBlob(ctx, db, readers...)
|
||||
|
||||
_, err = db.CommitValue(context.Background(), ds, blob)
|
||||
_, err = db.CommitValue(ctx, ds, blob)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error committing: %s\n", err)
|
||||
return 1
|
||||
|
||||
@@ -78,7 +78,7 @@ type chunkData struct {
|
||||
decompSuccess bool
|
||||
}
|
||||
|
||||
func runCat(args []string) int {
|
||||
func runCat(ctx context.Context, args []string) int {
|
||||
if len(args) < 1 {
|
||||
fmt.Fprintln(os.Stderr, "Not enough arguments")
|
||||
return 0
|
||||
@@ -134,7 +134,7 @@ func runCat(args []string) int {
|
||||
|
||||
//Want a clean db every loop
|
||||
sp, _ := spec.ForDatabase("mem")
|
||||
db := sp.GetDatabase(context.Background())
|
||||
db := sp.GetDatabase(ctx)
|
||||
value := types.DecodeValue(chunk, db)
|
||||
|
||||
fmt.Printf(" chunk[%d].raw.len: %d\n", cidx, len(currCD.compressed))
|
||||
@@ -153,7 +153,7 @@ func runCat(args []string) int {
|
||||
|
||||
fmt.Printf(" chunk[%d].value.kind: %s\n", cidx, value.Kind())
|
||||
fmt.Printf(" chunk[%d].value:\n\n", cidx)
|
||||
printValue(os.Stdout, value, filepath.Dir(chunkFile)+"::#"+b32Hash)
|
||||
printValue(ctx, os.Stdout, value, filepath.Dir(chunkFile)+"::#"+b32Hash)
|
||||
fmt.Println()
|
||||
|
||||
fmt.Println()
|
||||
@@ -269,7 +269,7 @@ func parseChunks(bytes []byte, pos int, sizes []int) (int, []chunkData) {
|
||||
return pos, cd
|
||||
}
|
||||
|
||||
func printValue(w io.Writer, v types.Value, valSpec string) {
|
||||
func printValue(ctx context.Context, w io.Writer, v types.Value, valSpec string) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
msg := " Failed to write the value " + valSpec + "\n"
|
||||
@@ -277,7 +277,7 @@ func printValue(w io.Writer, v types.Value, valSpec string) {
|
||||
}
|
||||
}()
|
||||
|
||||
types.WriteEncodedValue(context.Background(), w, v)
|
||||
types.WriteEncodedValue(ctx, w, v)
|
||||
}
|
||||
|
||||
func hexStr(bytes []byte) string {
|
||||
|
||||
@@ -39,9 +39,9 @@ func setupCommitFlags() *flag.FlagSet {
|
||||
return commitFlagSet
|
||||
}
|
||||
|
||||
func runCommit(args []string) int {
|
||||
func runCommit(ctx context.Context, args []string) int {
|
||||
cfg := config.NewResolver()
|
||||
db, ds, err := cfg.GetDataset(context.Background(), args[len(args)-1])
|
||||
db, ds, err := cfg.GetDataset(ctx, args[len(args)-1])
|
||||
d.CheckError(err)
|
||||
defer db.Close()
|
||||
|
||||
@@ -56,7 +56,7 @@ func runCommit(args []string) int {
|
||||
absPath, err := spec.NewAbsolutePath(path)
|
||||
d.CheckError(err)
|
||||
|
||||
value := absPath.Resolve(context.Background(), db)
|
||||
value := absPath.Resolve(ctx, db)
|
||||
if value == nil {
|
||||
d.CheckErrorNoUsage(errors.New(fmt.Sprintf("Error resolving value: %s", path)))
|
||||
}
|
||||
@@ -70,10 +70,10 @@ func runCommit(args []string) int {
|
||||
}
|
||||
}
|
||||
|
||||
meta, err := spec.CreateCommitMetaStruct(context.Background(), db, "", "", nil, nil)
|
||||
meta, err := spec.CreateCommitMetaStruct(ctx, db, "", "", nil, nil)
|
||||
d.CheckErrorNoUsage(err)
|
||||
|
||||
ds, err = db.Commit(context.Background(), ds, value, datas.CommitOptions{Meta: meta})
|
||||
ds, err = db.Commit(ctx, ds, value, datas.CommitOptions{Meta: meta})
|
||||
d.CheckErrorNoUsage(err)
|
||||
|
||||
if oldCommitExists {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
@@ -27,7 +28,7 @@ func setupConfigFlags() *flag.FlagSet {
|
||||
return flag.NewFlagSet("config", flag.ExitOnError)
|
||||
}
|
||||
|
||||
func runConfig(args []string) int {
|
||||
func runConfig(ctx context.Context, args []string) int {
|
||||
c, err := config.FindNomsConfig()
|
||||
if err == config.NoConfig {
|
||||
fmt.Fprintf(os.Stdout, "no config active\n")
|
||||
|
||||
@@ -37,16 +37,16 @@ func setupDiffFlags() *flag.FlagSet {
|
||||
return diffFlagSet
|
||||
}
|
||||
|
||||
func runDiff(args []string) int {
|
||||
func runDiff(ctx context.Context, args []string) int {
|
||||
cfg := config.NewResolver()
|
||||
db1, value1, err := cfg.GetPath(context.Background(), args[0])
|
||||
db1, value1, err := cfg.GetPath(ctx, args[0])
|
||||
d.CheckErrorNoUsage(err)
|
||||
if value1 == nil {
|
||||
d.CheckErrorNoUsage(fmt.Errorf("Object not found: %s", args[0]))
|
||||
}
|
||||
defer db1.Close()
|
||||
|
||||
db2, value2, err := cfg.GetPath(context.Background(), args[1])
|
||||
db2, value2, err := cfg.GetPath(ctx, args[1])
|
||||
d.CheckErrorNoUsage(err)
|
||||
if value2 == nil {
|
||||
d.CheckErrorNoUsage(fmt.Errorf("Object not found: %s", args[1]))
|
||||
@@ -54,13 +54,13 @@ func runDiff(args []string) int {
|
||||
defer db2.Close()
|
||||
|
||||
if stat {
|
||||
diff.Summary(context.Background(), value1, value2)
|
||||
diff.Summary(ctx, value1, value2)
|
||||
return 0
|
||||
}
|
||||
|
||||
pgr := outputpager.Start()
|
||||
defer pgr.Stop()
|
||||
|
||||
diff.PrintDiff(context.Background(), pgr.Writer, value1, value2, false)
|
||||
diff.PrintDiff(ctx, pgr.Writer, value1, value2, false)
|
||||
return 0
|
||||
}
|
||||
|
||||
+5
-5
@@ -34,10 +34,10 @@ func setupDsFlags() *flag.FlagSet {
|
||||
return dsFlagSet
|
||||
}
|
||||
|
||||
func runDs(args []string) int {
|
||||
func runDs(ctx context.Context, args []string) int {
|
||||
cfg := config.NewResolver()
|
||||
if toDelete != "" {
|
||||
db, set, err := cfg.GetDataset(context.Background(), toDelete)
|
||||
db, set, err := cfg.GetDataset(ctx, toDelete)
|
||||
d.CheckError(err)
|
||||
defer db.Close()
|
||||
|
||||
@@ -46,7 +46,7 @@ func runDs(args []string) int {
|
||||
d.CheckError(fmt.Errorf("Dataset %v not found", set.ID()))
|
||||
}
|
||||
|
||||
_, err = set.Database().Delete(context.Background(), set)
|
||||
_, err = set.Database().Delete(ctx, set)
|
||||
d.CheckError(err)
|
||||
|
||||
fmt.Printf("Deleted %v (was #%v)\n", toDelete, oldCommitRef.TargetHash().String())
|
||||
@@ -55,11 +55,11 @@ func runDs(args []string) int {
|
||||
if len(args) >= 1 {
|
||||
dbSpec = args[0]
|
||||
}
|
||||
store, err := cfg.GetDatabase(context.Background(), dbSpec)
|
||||
store, err := cfg.GetDatabase(ctx, dbSpec)
|
||||
d.CheckError(err)
|
||||
defer store.Close()
|
||||
|
||||
store.Datasets(context.Background()).IterAll(context.Background(), func(k, v types.Value) {
|
||||
store.Datasets(ctx).IterAll(ctx, func(k, v types.Value) {
|
||||
fmt.Println(k)
|
||||
})
|
||||
}
|
||||
|
||||
+20
-20
@@ -17,7 +17,7 @@ import (
|
||||
"github.com/attic-labs/noms/go/types"
|
||||
)
|
||||
|
||||
func nomsList(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
|
||||
func nomsList(ctx context.Context, noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
|
||||
list := noms.Command("list", "interact with lists")
|
||||
|
||||
listNew := list.Command("new", "creates a new list")
|
||||
@@ -41,51 +41,51 @@ func nomsList(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandle
|
||||
return list, func(input string) int {
|
||||
switch input {
|
||||
case listNew.FullCommand():
|
||||
return nomsListNew(*newDb, *newEntries)
|
||||
return nomsListNew(ctx, *newDb, *newEntries)
|
||||
case listAppend.FullCommand():
|
||||
return nomsListAppend(*appendSpec, *appendEntries)
|
||||
return nomsListAppend(ctx, *appendSpec, *appendEntries)
|
||||
case listInsert.FullCommand():
|
||||
return nomsListInsert(*insertSpec, *insertAt, *insertEntries)
|
||||
return nomsListInsert(ctx, *insertSpec, *insertAt, *insertEntries)
|
||||
case listDel.FullCommand():
|
||||
return nomsListDel(*delSpec, *delPos, *delLen)
|
||||
return nomsListDel(ctx, *delSpec, *delPos, *delLen)
|
||||
}
|
||||
d.Panic("notreached")
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
func nomsListNew(dbStr string, args []string) int {
|
||||
func nomsListNew(ctx context.Context, dbStr string, args []string) int {
|
||||
sp, err := spec.ForDatabase(dbStr)
|
||||
d.PanicIfError(err)
|
||||
applyListInserts(sp, types.NewList(context.Background(), sp.GetDatabase(context.Background())), nil, 0, args)
|
||||
applyListInserts(ctx, sp, types.NewList(ctx, sp.GetDatabase(ctx)), nil, 0, args)
|
||||
return 0
|
||||
}
|
||||
|
||||
func nomsListAppend(specStr string, args []string) int {
|
||||
func nomsListAppend(ctx context.Context, specStr string, args []string) int {
|
||||
sp, err := spec.ForPath(specStr)
|
||||
d.PanicIfError(err)
|
||||
rootVal, basePath := splitPath(sp)
|
||||
rootVal, basePath := splitPath(ctx, sp)
|
||||
if list, ok := rootVal.(types.List); ok {
|
||||
applyListInserts(sp, rootVal, basePath, list.Len(), args)
|
||||
applyListInserts(ctx, sp, rootVal, basePath, list.Len(), args)
|
||||
} else {
|
||||
d.CheckErrorNoUsage(fmt.Errorf("%s is not a list", specStr))
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func nomsListInsert(specStr string, pos uint64, args []string) int {
|
||||
func nomsListInsert(ctx context.Context, specStr string, pos uint64, args []string) int {
|
||||
sp, err := spec.ForPath(specStr)
|
||||
d.PanicIfError(err)
|
||||
rootVal, basePath := splitPath(sp)
|
||||
applyListInserts(sp, rootVal, basePath, pos, args)
|
||||
rootVal, basePath := splitPath(ctx, sp)
|
||||
applyListInserts(ctx, sp, rootVal, basePath, pos, args)
|
||||
return 0
|
||||
}
|
||||
|
||||
func nomsListDel(specStr string, pos uint64, len uint64) int {
|
||||
func nomsListDel(ctx context.Context, specStr string, pos uint64, len uint64) int {
|
||||
sp, err := spec.ForPath(specStr)
|
||||
d.PanicIfError(err)
|
||||
|
||||
rootVal, basePath := splitPath(sp)
|
||||
rootVal, basePath := splitPath(ctx, sp)
|
||||
patch := diff.Patch{}
|
||||
// TODO: if len-pos is large this will start to become problematic
|
||||
for i := pos; i < pos+len; i++ {
|
||||
@@ -95,19 +95,19 @@ func nomsListDel(specStr string, pos uint64, len uint64) int {
|
||||
})
|
||||
}
|
||||
|
||||
appplyPatch(sp, rootVal, basePath, patch)
|
||||
appplyPatch(ctx, sp, rootVal, basePath, patch)
|
||||
return 0
|
||||
}
|
||||
|
||||
func applyListInserts(sp spec.Spec, rootVal types.Value, basePath types.Path, pos uint64, args []string) {
|
||||
func applyListInserts(ctx context.Context, sp spec.Spec, rootVal types.Value, basePath types.Path, pos uint64, args []string) {
|
||||
if rootVal == nil {
|
||||
d.CheckErrorNoUsage(fmt.Errorf("No value at: %s", sp.String()))
|
||||
return
|
||||
}
|
||||
db := sp.GetDatabase(context.Background())
|
||||
db := sp.GetDatabase(ctx)
|
||||
patch := diff.Patch{}
|
||||
for i := 0; i < len(args); i++ {
|
||||
vv, err := argumentToValue(args[i], db)
|
||||
vv, err := argumentToValue(ctx, args[i], db)
|
||||
if err != nil {
|
||||
d.CheckError(fmt.Errorf("Invalid value: %s at position %d: %s", args[i], i, err))
|
||||
}
|
||||
@@ -117,5 +117,5 @@ func applyListInserts(sp spec.Spec, rootVal types.Value, basePath types.Path, po
|
||||
NewValue: vv,
|
||||
})
|
||||
}
|
||||
appplyPatch(sp, rootVal, basePath, patch)
|
||||
appplyPatch(ctx, sp, rootVal, basePath, patch)
|
||||
}
|
||||
|
||||
+23
-23
@@ -66,7 +66,7 @@ func setupLogFlags() *flag.FlagSet {
|
||||
return logFlagSet
|
||||
}
|
||||
|
||||
func runLog(args []string) int {
|
||||
func runLog(ctx context.Context, args []string) int {
|
||||
useColor = shouldUseColor()
|
||||
cfg := config.NewResolver()
|
||||
|
||||
@@ -78,13 +78,13 @@ func runLog(args []string) int {
|
||||
d.CheckErrorNoUsage(err)
|
||||
defer sp.Close()
|
||||
|
||||
pinned, ok := sp.Pin(context.Background())
|
||||
pinned, ok := sp.Pin(ctx)
|
||||
if !ok {
|
||||
fmt.Fprintf(os.Stderr, "Cannot resolve spec: %s\n", args[0])
|
||||
return 1
|
||||
}
|
||||
defer pinned.Close()
|
||||
database := pinned.GetDatabase(context.Background())
|
||||
database := pinned.GetDatabase(ctx)
|
||||
|
||||
absPath := pinned.Path
|
||||
path := absPath.Path
|
||||
@@ -92,7 +92,7 @@ func runLog(args []string) int {
|
||||
path = types.MustParsePath(".value")
|
||||
}
|
||||
|
||||
origCommit, ok := database.ReadValue(context.Background(), absPath.Hash).(types.Struct)
|
||||
origCommit, ok := database.ReadValue(ctx, absPath.Hash).(types.Struct)
|
||||
if !ok || !datas.IsCommit(origCommit) {
|
||||
d.CheckError(fmt.Errorf("%s does not reference a Commit object", args[0]))
|
||||
}
|
||||
@@ -108,13 +108,13 @@ func runLog(args []string) int {
|
||||
var done = false
|
||||
|
||||
go func() {
|
||||
for ln, ok := iter.Next(); !done && ok && displayed < maxCommits; ln, ok = iter.Next() {
|
||||
for ln, ok := iter.Next(ctx); !done && ok && displayed < maxCommits; ln, ok = iter.Next(ctx) {
|
||||
ch := make(chan []byte)
|
||||
bytesChan <- ch
|
||||
|
||||
go func(ch chan []byte, node LogNode) {
|
||||
buff := &bytes.Buffer{}
|
||||
printCommit(node, path, buff, database, tz)
|
||||
printCommit(ctx, node, path, buff, database, tz)
|
||||
ch <- buff.Bytes()
|
||||
}(ch, ln)
|
||||
|
||||
@@ -142,7 +142,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, tz *time.Location) (err error) {
|
||||
func printCommit(ctx context.Context, 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 {
|
||||
@@ -163,7 +163,7 @@ func printCommit(node LogNode, path types.Path, w io.Writer, db datas.Database,
|
||||
|
||||
parentLabel := "Parent"
|
||||
parentValue := "None"
|
||||
parents := commitRefsFromSet(node.commit.Get(datas.ParentsField).(types.Set))
|
||||
parents := commitRefsFromSet(ctx, node.commit.Get(datas.ParentsField).(types.Set))
|
||||
if len(parents) > 1 {
|
||||
pstrings := make([]string, len(parents))
|
||||
for i, p := range parents {
|
||||
@@ -188,16 +188,16 @@ 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, tz)
|
||||
lineno, err = writeMetaLines(ctx, node, maxLines, lineno, maxFieldNameLen, w, tz)
|
||||
if err != nil && err != writers.MaxLinesErr {
|
||||
fmt.Fprintf(w, "error: %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
if showValue {
|
||||
_, err = writeCommitLines(node, path, maxLines, lineno, w, db)
|
||||
_, err = writeCommitLines(ctx, node, path, maxLines, lineno, w, db)
|
||||
} else {
|
||||
_, err = writeDiffLines(node, path, db, maxLines, lineno, w)
|
||||
_, err = writeDiffLines(ctx, node, path, db, maxLines, lineno, w)
|
||||
}
|
||||
}
|
||||
return
|
||||
@@ -256,7 +256,7 @@ func genGraph(node LogNode, lineno int) string {
|
||||
return string(buf)
|
||||
}
|
||||
|
||||
func writeMetaLines(node LogNode, maxLines, lineno, maxLabelLen int, w io.Writer, tz *time.Location) (int, error) {
|
||||
func writeMetaLines(ctx context.Context, 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)))
|
||||
@@ -272,10 +272,10 @@ func writeMetaLines(node LogNode, maxLines, lineno, maxLabelLen int, w io.Writer
|
||||
// field of type datetime.DateTimeType
|
||||
if types.TypeOf(v).Equals(datetime.DateTimeType) {
|
||||
var dt datetime.DateTime
|
||||
dt.UnmarshalNoms(context.Background(), v)
|
||||
dt.UnmarshalNoms(ctx, v)
|
||||
fmt.Fprintln(pw, dt.In(tz).Format(time.RFC3339))
|
||||
} else {
|
||||
types.WriteEncodedValue(context.Background(), pw, v)
|
||||
types.WriteEncodedValue(ctx, pw, v)
|
||||
}
|
||||
fmt.Fprintln(pw)
|
||||
})
|
||||
@@ -285,17 +285,17 @@ func writeMetaLines(node LogNode, maxLines, lineno, maxLabelLen int, w io.Writer
|
||||
return lineno, nil
|
||||
}
|
||||
|
||||
func writeCommitLines(node LogNode, path types.Path, maxLines, lineno int, w io.Writer, db datas.Database) (lineCnt int, err error) {
|
||||
func writeCommitLines(ctx context.Context, node LogNode, path types.Path, maxLines, lineno int, w io.Writer, db datas.Database) (lineCnt int, err error) {
|
||||
genPrefix := func(pw *writers.PrefixWriter) []byte {
|
||||
return []byte(genGraph(node, int(pw.NumLines)+1))
|
||||
}
|
||||
mlw := &writers.MaxLineWriter{Dest: w, MaxLines: uint32(maxLines), NumLines: uint32(lineno)}
|
||||
pw := &writers.PrefixWriter{Dest: mlw, PrefixFunc: genPrefix, NeedsPrefix: true, NumLines: uint32(lineno)}
|
||||
v := path.Resolve(context.Background(), node.commit, db)
|
||||
v := path.Resolve(ctx, node.commit, db)
|
||||
if v == nil {
|
||||
pw.Write([]byte("<nil>\n"))
|
||||
} else {
|
||||
err = types.WriteEncodedValue(context.Background(), pw, v)
|
||||
err = types.WriteEncodedValue(ctx, pw, v)
|
||||
mlw.MaxLines = 0
|
||||
if err != nil {
|
||||
d.PanicIfNotType(writers.MaxLinesErr, err)
|
||||
@@ -314,7 +314,7 @@ func writeCommitLines(node LogNode, path types.Path, maxLines, lineno int, w io.
|
||||
return int(pw.NumLines), err
|
||||
}
|
||||
|
||||
func writeDiffLines(node LogNode, path types.Path, db datas.Database, maxLines, lineno int, w io.Writer) (lineCnt int, err error) {
|
||||
func writeDiffLines(ctx context.Context, node LogNode, path types.Path, db datas.Database, maxLines, lineno int, w io.Writer) (lineCnt int, err error) {
|
||||
genPrefix := func(w *writers.PrefixWriter) []byte {
|
||||
return []byte(genGraph(node, int(w.NumLines)+1))
|
||||
}
|
||||
@@ -323,18 +323,18 @@ func writeDiffLines(node LogNode, path types.Path, db datas.Database, maxLines,
|
||||
parents := node.commit.Get(datas.ParentsField).(types.Set)
|
||||
var parent types.Value
|
||||
if parents.Len() > 0 {
|
||||
parent = parents.First(context.Background())
|
||||
parent = parents.First(ctx)
|
||||
}
|
||||
if parent == nil {
|
||||
_, err = fmt.Fprint(pw, "\n")
|
||||
return 1, err
|
||||
}
|
||||
|
||||
parentCommit := parent.(types.Ref).TargetValue(context.Background(), db).(types.Struct)
|
||||
parentCommit := parent.(types.Ref).TargetValue(ctx, db).(types.Struct)
|
||||
var old, neu types.Value
|
||||
functions.All(
|
||||
func() { old = path.Resolve(context.Background(), parentCommit, db) },
|
||||
func() { neu = path.Resolve(context.Background(), node.commit, db) },
|
||||
func() { old = path.Resolve(ctx, parentCommit, db) },
|
||||
func() { neu = path.Resolve(ctx, node.commit, db) },
|
||||
)
|
||||
|
||||
// TODO: It would be better to treat this as an add or remove, but that requires generalization
|
||||
@@ -347,7 +347,7 @@ func writeDiffLines(node LogNode, path types.Path, db datas.Database, maxLines,
|
||||
}
|
||||
|
||||
if old != nil && neu != nil {
|
||||
err = diff.PrintDiff(context.Background(), pw, old, neu, true)
|
||||
err = diff.PrintDiff(ctx, pw, old, neu, true)
|
||||
mlw.MaxLines = 0
|
||||
if err != nil {
|
||||
d.PanicIfNotType(err, writers.MaxLinesErr)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"os"
|
||||
@@ -54,7 +55,7 @@ func setupManifestFlags() *flag.FlagSet {
|
||||
return flagSet
|
||||
}
|
||||
|
||||
func runManifest(args []string) int {
|
||||
func runManifest(ctx context.Context, args []string) int {
|
||||
if len(args) < 1 {
|
||||
fmt.Fprintln(os.Stderr, "Not enough arguments")
|
||||
return 0
|
||||
|
||||
+16
-16
@@ -17,7 +17,7 @@ import (
|
||||
"github.com/attic-labs/noms/go/types"
|
||||
)
|
||||
|
||||
func nomsMap(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
|
||||
func nomsMap(ctx context.Context, noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
|
||||
maap := noms.Command("map", "interact with maps")
|
||||
|
||||
mapNew := maap.Command("new", "creates a new map")
|
||||
@@ -35,37 +35,37 @@ func nomsMap(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler
|
||||
return maap, func(input string) int {
|
||||
switch input {
|
||||
case mapNew.FullCommand():
|
||||
return nomsMapNew(*newDb, *newEntries)
|
||||
return nomsMapNew(ctx, *newDb, *newEntries)
|
||||
case mapSet.FullCommand():
|
||||
return nomsMapSet(*setSpec, *setEntries)
|
||||
return nomsMapSet(ctx, *setSpec, *setEntries)
|
||||
case mapDel.FullCommand():
|
||||
return nomsMapDel(*delSpec, *delKeys)
|
||||
return nomsMapDel(ctx, *delSpec, *delKeys)
|
||||
}
|
||||
d.Panic("notreached")
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
func nomsMapNew(dbStr string, args []string) int {
|
||||
func nomsMapNew(ctx context.Context, dbStr string, args []string) int {
|
||||
sp, err := spec.ForDatabase(dbStr)
|
||||
d.PanicIfError(err)
|
||||
applyMapEdits(sp, types.NewMap(context.Background(), sp.GetDatabase(context.Background())), nil, args)
|
||||
applyMapEdits(ctx, sp, types.NewMap(ctx, sp.GetDatabase(ctx)), nil, args)
|
||||
return 0
|
||||
}
|
||||
|
||||
func nomsMapSet(specStr string, args []string) int {
|
||||
func nomsMapSet(ctx context.Context, specStr string, args []string) int {
|
||||
sp, err := spec.ForPath(specStr)
|
||||
d.PanicIfError(err)
|
||||
rootVal, basePath := splitPath(sp)
|
||||
applyMapEdits(sp, rootVal, basePath, args)
|
||||
rootVal, basePath := splitPath(ctx, sp)
|
||||
applyMapEdits(ctx, sp, rootVal, basePath, args)
|
||||
return 0
|
||||
}
|
||||
|
||||
func nomsMapDel(specStr string, args []string) int {
|
||||
func nomsMapDel(ctx context.Context, specStr string, args []string) int {
|
||||
sp, err := spec.ForPath(specStr)
|
||||
d.PanicIfError(err)
|
||||
|
||||
rootVal, basePath := splitPath(sp)
|
||||
rootVal, basePath := splitPath(ctx, sp)
|
||||
patch := diff.Patch{}
|
||||
for i := 0; i < len(args); i++ {
|
||||
kp := parseKeyPart(args, i)
|
||||
@@ -75,11 +75,11 @@ func nomsMapDel(specStr string, args []string) int {
|
||||
})
|
||||
}
|
||||
|
||||
appplyPatch(sp, rootVal, basePath, patch)
|
||||
appplyPatch(ctx, sp, rootVal, basePath, patch)
|
||||
return 0
|
||||
}
|
||||
|
||||
func applyMapEdits(sp spec.Spec, rootVal types.Value, basePath types.Path, args []string) {
|
||||
func applyMapEdits(ctx context.Context, sp spec.Spec, rootVal types.Value, basePath types.Path, args []string) {
|
||||
if len(args)%2 != 0 {
|
||||
d.CheckError(fmt.Errorf("Must be an even number of key/value pairs"))
|
||||
}
|
||||
@@ -87,11 +87,11 @@ func applyMapEdits(sp spec.Spec, rootVal types.Value, basePath types.Path, args
|
||||
d.CheckErrorNoUsage(fmt.Errorf("No value at: %s", sp.String()))
|
||||
return
|
||||
}
|
||||
db := sp.GetDatabase(context.Background())
|
||||
db := sp.GetDatabase(ctx)
|
||||
patch := diff.Patch{}
|
||||
for i := 0; i < len(args); i += 2 {
|
||||
kp := parseKeyPart(args, i)
|
||||
vv, err := argumentToValue(args[i+1], db)
|
||||
vv, err := argumentToValue(ctx, args[i+1], db)
|
||||
if err != nil {
|
||||
d.CheckError(fmt.Errorf("Invalid value: %s at position %d: %s", args[i+1], i+1, err))
|
||||
}
|
||||
@@ -101,7 +101,7 @@ func applyMapEdits(sp spec.Spec, rootVal types.Value, basePath types.Path, args
|
||||
NewValue: vv,
|
||||
})
|
||||
}
|
||||
appplyPatch(sp, rootVal, basePath, patch)
|
||||
appplyPatch(ctx, sp, rootVal, basePath, patch)
|
||||
}
|
||||
|
||||
func parseKeyPart(args []string, i int) (res types.PathPart) {
|
||||
|
||||
+14
-14
@@ -49,25 +49,25 @@ func checkIfTrue(b bool, format string, args ...interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func runMerge(args []string) int {
|
||||
func runMerge(ctx context.Context, args []string) int {
|
||||
cfg := config.NewResolver()
|
||||
|
||||
if len(args) != 4 {
|
||||
d.CheckErrorNoUsage(fmt.Errorf("Incorrect number of arguments"))
|
||||
}
|
||||
db, err := cfg.GetDatabase(context.Background(), args[0])
|
||||
db, err := cfg.GetDatabase(ctx, args[0])
|
||||
d.CheckError(err)
|
||||
defer db.Close()
|
||||
|
||||
leftDS, rightDS, outDS := resolveDatasets(db, args[1], args[2], args[3])
|
||||
left, right, ancestor := getMergeCandidates(db, leftDS, rightDS)
|
||||
leftDS, rightDS, outDS := resolveDatasets(ctx, db, args[1], args[2], args[3])
|
||||
left, right, ancestor := getMergeCandidates(ctx, db, leftDS, rightDS)
|
||||
policy := decidePolicy(resolver)
|
||||
pc := newMergeProgressChan()
|
||||
merged, err := policy(context.Background(), left, right, ancestor, db, pc)
|
||||
merged, err := policy(ctx, left, right, ancestor, db, pc)
|
||||
d.CheckErrorNoUsage(err)
|
||||
close(pc)
|
||||
|
||||
_, err = db.SetHead(context.Background(), outDS, db.WriteValue(context.Background(), datas.NewCommit(merged, types.NewSet(context.Background(), db, leftDS.HeadRef(), rightDS.HeadRef()), types.EmptyStruct)))
|
||||
_, err = db.SetHead(ctx, outDS, db.WriteValue(ctx, datas.NewCommit(merged, types.NewSet(ctx, db, leftDS.HeadRef(), rightDS.HeadRef()), types.EmptyStruct)))
|
||||
d.PanicIfError(err)
|
||||
if !verbose.Quiet() {
|
||||
status.Printf("Done")
|
||||
@@ -76,12 +76,12 @@ func runMerge(args []string) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func resolveDatasets(db datas.Database, leftName, rightName, outName string) (leftDS, rightDS, outDS datas.Dataset) {
|
||||
func resolveDatasets(ctx context.Context, db datas.Database, leftName, rightName, outName string) (leftDS, rightDS, outDS datas.Dataset) {
|
||||
makeDS := func(dsName string) datas.Dataset {
|
||||
if !datasetRe.MatchString(dsName) {
|
||||
d.CheckErrorNoUsage(fmt.Errorf("Invalid dataset %s, must match %s", dsName, datas.DatasetRe.String()))
|
||||
}
|
||||
return db.GetDataset(context.Background(), dsName)
|
||||
return db.GetDataset(ctx, dsName)
|
||||
}
|
||||
leftDS = makeDS(leftName)
|
||||
rightDS = makeDS(rightName)
|
||||
@@ -89,28 +89,28 @@ func resolveDatasets(db datas.Database, leftName, rightName, outName string) (le
|
||||
return
|
||||
}
|
||||
|
||||
func getMergeCandidates(db datas.Database, leftDS, rightDS datas.Dataset) (left, right, ancestor types.Value) {
|
||||
func getMergeCandidates(ctx context.Context, db datas.Database, leftDS, rightDS datas.Dataset) (left, right, ancestor types.Value) {
|
||||
leftRef, ok := leftDS.MaybeHeadRef()
|
||||
checkIfTrue(!ok, "Dataset %s has no data", leftDS.ID())
|
||||
rightRef, ok := rightDS.MaybeHeadRef()
|
||||
checkIfTrue(!ok, "Dataset %s has no data", rightDS.ID())
|
||||
ancestorCommit, ok := getCommonAncestor(leftRef, rightRef, db)
|
||||
ancestorCommit, ok := getCommonAncestor(ctx, leftRef, rightRef, db)
|
||||
checkIfTrue(!ok, "Datasets %s and %s have no common ancestor", leftDS.ID(), rightDS.ID())
|
||||
|
||||
return leftDS.HeadValue(), rightDS.HeadValue(), ancestorCommit.Get(datas.ValueField)
|
||||
}
|
||||
|
||||
func getCommonAncestor(r1, r2 types.Ref, vr types.ValueReader) (a types.Struct, found bool) {
|
||||
aRef, found := datas.FindCommonAncestor(context.Background(), r1, r2, vr)
|
||||
func getCommonAncestor(ctx context.Context, r1, r2 types.Ref, vr types.ValueReader) (a types.Struct, found bool) {
|
||||
aRef, found := datas.FindCommonAncestor(ctx, r1, r2, vr)
|
||||
if !found {
|
||||
return
|
||||
}
|
||||
v := vr.ReadValue(context.Background(), aRef.TargetHash())
|
||||
v := vr.ReadValue(ctx, aRef.TargetHash())
|
||||
if v == nil {
|
||||
panic(aRef.TargetHash().String() + " not found")
|
||||
}
|
||||
if !datas.IsCommit(v) {
|
||||
panic("Not a commit: " + types.EncodedValueMaxLines(context.Background(), v, 10) + " ...")
|
||||
panic("Not a commit: " + types.EncodedValueMaxLines(ctx, v, 10) + " ...")
|
||||
}
|
||||
return v.(types.Struct), true
|
||||
}
|
||||
|
||||
@@ -37,17 +37,17 @@ func setupRootFlags() *flag.FlagSet {
|
||||
return flagSet
|
||||
}
|
||||
|
||||
func runRoot(args []string) int {
|
||||
func runRoot(ctx context.Context, args []string) int {
|
||||
if len(args) < 1 {
|
||||
fmt.Fprintln(os.Stderr, "Not enough arguments")
|
||||
return 0
|
||||
}
|
||||
|
||||
cfg := config.NewResolver()
|
||||
cs, err := cfg.GetChunkStore(context.Background(), args[0])
|
||||
cs, err := cfg.GetChunkStore(ctx, args[0])
|
||||
d.CheckErrorNoUsage(err)
|
||||
|
||||
currRoot := cs.Root(context.Background())
|
||||
currRoot := cs.Root(ctx)
|
||||
|
||||
if updateRoot == "" {
|
||||
fmt.Println(currRoot)
|
||||
@@ -64,10 +64,10 @@ func runRoot(args []string) int {
|
||||
}
|
||||
|
||||
// If BUG 3407 is correct, we might be able to just take cs and make a Database directly from that.
|
||||
db, err := cfg.GetDatabase(context.Background(), args[0])
|
||||
db, err := cfg.GetDatabase(ctx, args[0])
|
||||
d.CheckErrorNoUsage(err)
|
||||
defer db.Close()
|
||||
if !validate(db.ReadValue(context.Background(), h)) {
|
||||
if !validate(ctx, db.ReadValue(ctx, h)) {
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ Continue?`)
|
||||
return 0
|
||||
}
|
||||
|
||||
ok = cs.Commit(context.Background(), h, currRoot)
|
||||
ok = cs.Commit(ctx, h, currRoot)
|
||||
if !ok {
|
||||
fmt.Fprintln(os.Stderr, "Optimistic concurrency failure")
|
||||
return 1
|
||||
@@ -97,14 +97,14 @@ Continue?`)
|
||||
return 0
|
||||
}
|
||||
|
||||
func validate(r types.Value) bool {
|
||||
func validate(ctx context.Context, r types.Value) bool {
|
||||
rootType := types.MakeMapType(types.StringType, types.MakeRefType(types.ValueType))
|
||||
if !types.IsValueSubtypeOf(r, rootType) {
|
||||
fmt.Fprintf(os.Stderr, "Root of database must be %s, but you specified: %s\n", rootType.Describe(context.Background()), types.TypeOf(r).Describe(context.Background()))
|
||||
fmt.Fprintf(os.Stderr, "Root of database must be %s, but you specified: %s\n", rootType.Describe(ctx), types.TypeOf(r).Describe(ctx))
|
||||
return false
|
||||
}
|
||||
|
||||
return r.(types.Map).Any(context.Background(), func(k, v types.Value) bool {
|
||||
return r.(types.Map).Any(ctx, func(k, v types.Value) bool {
|
||||
if !datas.IsRefOfCommitType(types.TypeOf(v)) {
|
||||
fmt.Fprintf(os.Stderr, "Invalid root map. Value for key '%s' is not a ref of commit.", string(k.(types.String)))
|
||||
return false
|
||||
|
||||
@@ -40,13 +40,13 @@ func setupServeFlags() *flag.FlagSet {
|
||||
return serveFlagSet
|
||||
}
|
||||
|
||||
func runServe(args []string) int {
|
||||
func runServe(ctx context.Context, args []string) int {
|
||||
cfg := config.NewResolver()
|
||||
db := ""
|
||||
if len(args) > 0 {
|
||||
db = args[0]
|
||||
}
|
||||
cs, err := cfg.GetChunkStore(context.Background(), db)
|
||||
cs, err := cfg.GetChunkStore(ctx, db)
|
||||
d.CheckError(err)
|
||||
server := datas.NewRemoteDatabaseServer(cs, port)
|
||||
|
||||
|
||||
+18
-18
@@ -20,7 +20,7 @@ import (
|
||||
"github.com/attic-labs/noms/go/types"
|
||||
)
|
||||
|
||||
func nomsSet(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
|
||||
func nomsSet(ctx context.Context, noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
|
||||
set := noms.Command("set", "interact with sets")
|
||||
|
||||
setNew := set.Command("new", "creates a new set")
|
||||
@@ -38,49 +38,49 @@ func nomsSet(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler
|
||||
return set, func(input string) int {
|
||||
switch input {
|
||||
case setNew.FullCommand():
|
||||
return nomsSetNew(*newDb, *newEntries)
|
||||
return nomsSetNew(ctx, *newDb, *newEntries)
|
||||
case setInsert.FullCommand():
|
||||
return nomsSetInsert(*insertSpec, *insertEntries)
|
||||
return nomsSetInsert(ctx, *insertSpec, *insertEntries)
|
||||
case setDel.FullCommand():
|
||||
return nomsSetDel(*delSpec, *delEntries)
|
||||
return nomsSetDel(ctx, *delSpec, *delEntries)
|
||||
}
|
||||
d.Panic("notreached")
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
func nomsSetNew(dbStr string, args []string) int {
|
||||
func nomsSetNew(ctx context.Context, dbStr string, args []string) int {
|
||||
sp, err := spec.ForDatabase(dbStr)
|
||||
d.PanicIfError(err)
|
||||
applySetEdits(sp, types.NewSet(context.Background(), sp.GetDatabase(context.Background())), nil, types.DiffChangeAdded, args)
|
||||
applySetEdits(ctx, sp, types.NewSet(ctx, sp.GetDatabase(ctx)), nil, types.DiffChangeAdded, args)
|
||||
return 0
|
||||
}
|
||||
|
||||
func nomsSetInsert(specStr string, args []string) int {
|
||||
func nomsSetInsert(ctx context.Context, specStr string, args []string) int {
|
||||
sp, err := spec.ForPath(specStr)
|
||||
d.PanicIfError(err)
|
||||
rootVal, basePath := splitPath(sp)
|
||||
applySetEdits(sp, rootVal, basePath, types.DiffChangeAdded, args)
|
||||
rootVal, basePath := splitPath(ctx, sp)
|
||||
applySetEdits(ctx, sp, rootVal, basePath, types.DiffChangeAdded, args)
|
||||
return 0
|
||||
}
|
||||
|
||||
func nomsSetDel(specStr string, args []string) int {
|
||||
func nomsSetDel(ctx context.Context, specStr string, args []string) int {
|
||||
sp, err := spec.ForPath(specStr)
|
||||
d.PanicIfError(err)
|
||||
rootVal, basePath := splitPath(sp)
|
||||
applySetEdits(sp, rootVal, basePath, types.DiffChangeRemoved, args)
|
||||
rootVal, basePath := splitPath(ctx, sp)
|
||||
applySetEdits(ctx, sp, rootVal, basePath, types.DiffChangeRemoved, args)
|
||||
return 0
|
||||
}
|
||||
|
||||
func applySetEdits(sp spec.Spec, rootVal types.Value, basePath types.Path, ct types.DiffChangeType, args []string) {
|
||||
func applySetEdits(ctx context.Context, sp spec.Spec, rootVal types.Value, basePath types.Path, ct types.DiffChangeType, args []string) {
|
||||
if rootVal == nil {
|
||||
d.CheckErrorNoUsage(fmt.Errorf("No value at: %s", sp.String()))
|
||||
return
|
||||
}
|
||||
db := sp.GetDatabase(context.Background())
|
||||
db := sp.GetDatabase(ctx)
|
||||
patch := diff.Patch{}
|
||||
for i := 0; i < len(args); i++ {
|
||||
vv, err := argumentToValue(args[i], db)
|
||||
vv, err := argumentToValue(ctx, args[i], db)
|
||||
if err != nil {
|
||||
d.CheckErrorNoUsage(err)
|
||||
}
|
||||
@@ -100,10 +100,10 @@ func applySetEdits(sp spec.Spec, rootVal types.Value, basePath types.Path, ct ty
|
||||
}
|
||||
patch = append(patch, d)
|
||||
}
|
||||
appplyPatch(sp, rootVal, basePath, patch)
|
||||
appplyPatch(ctx, sp, rootVal, basePath, patch)
|
||||
}
|
||||
|
||||
func argumentToValue(arg string, db datas.Database) (types.Value, error) {
|
||||
func argumentToValue(ctx context.Context, arg string, db datas.Database) (types.Value, error) {
|
||||
d.PanicIfTrue(arg == "")
|
||||
|
||||
if arg == "true" {
|
||||
@@ -136,7 +136,7 @@ func argumentToValue(arg string, db datas.Database) (types.Value, error) {
|
||||
if arg[0] == '@' {
|
||||
p, err := spec.NewAbsolutePath(arg[1:])
|
||||
d.PanicIfError(err)
|
||||
return p.Resolve(context.Background(), db), nil
|
||||
return p.Resolve(ctx, db), nil
|
||||
}
|
||||
if n, err := strconv.ParseFloat(arg, 64); err == nil {
|
||||
return types.Float(n), nil
|
||||
|
||||
@@ -48,9 +48,9 @@ func setupShowFlags() *flag.FlagSet {
|
||||
return showFlagSet
|
||||
}
|
||||
|
||||
func runShow(args []string) int {
|
||||
func runShow(ctx context.Context, args []string) int {
|
||||
cfg := config.NewResolver()
|
||||
database, value, err := cfg.GetPath(context.Background(), args[0])
|
||||
database, value, err := cfg.GetPath(ctx, args[0])
|
||||
d.CheckErrorNoUsage(err)
|
||||
defer database.Close()
|
||||
|
||||
@@ -73,7 +73,7 @@ func runShow(args []string) int {
|
||||
}
|
||||
|
||||
if showStats {
|
||||
types.WriteValueStats(context.Background(), os.Stdout, value, database)
|
||||
types.WriteValueStats(ctx, os.Stdout, value, database)
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -84,10 +84,10 @@ func runShow(args []string) int {
|
||||
pgr := outputpager.Start()
|
||||
defer pgr.Stop()
|
||||
|
||||
types.WriteEncodedValue(context.Background(), pgr.Writer, value)
|
||||
types.WriteEncodedValue(ctx, pgr.Writer, value)
|
||||
fmt.Fprintln(pgr.Writer)
|
||||
} else {
|
||||
types.WriteEncodedValue(context.Background(), os.Stdout, value)
|
||||
types.WriteEncodedValue(ctx, os.Stdout, value)
|
||||
}
|
||||
|
||||
return 0
|
||||
|
||||
@@ -15,13 +15,13 @@ import (
|
||||
"github.com/attic-labs/noms/go/d"
|
||||
)
|
||||
|
||||
func nomsStats(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
|
||||
func nomsStats(ctx context.Context, noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
|
||||
stats := noms.Command("stats", "Shows stats summary for a Noms Database")
|
||||
database := stats.Arg("database", "See Spelling Objects at https://github.com/attic-labs/noms/blob/master/doc/spelling.md for details on the database argument.").Required().String()
|
||||
|
||||
return stats, func(input string) int {
|
||||
cfg := config.NewResolver()
|
||||
store, err := cfg.GetDatabase(context.Background(), *database)
|
||||
store, err := cfg.GetDatabase(ctx, *database)
|
||||
d.CheckError(err)
|
||||
defer store.Close()
|
||||
|
||||
|
||||
+25
-25
@@ -17,7 +17,7 @@ import (
|
||||
"github.com/attic-labs/noms/go/types"
|
||||
)
|
||||
|
||||
func nomsStruct(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
|
||||
func nomsStruct(ctx context.Context, noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
|
||||
strukt := noms.Command("struct", "interact with structs")
|
||||
|
||||
struktNew := strukt.Command("new", "creates a new struct")
|
||||
@@ -36,38 +36,38 @@ func nomsStruct(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHand
|
||||
return strukt, func(input string) int {
|
||||
switch input {
|
||||
case struktNew.FullCommand():
|
||||
return nomsStructNew(*newDb, *newName, *newFields)
|
||||
return nomsStructNew(ctx, *newDb, *newName, *newFields)
|
||||
case struktSet.FullCommand():
|
||||
return nomsStructSet(*setSpec, *setFields)
|
||||
return nomsStructSet(ctx, *setSpec, *setFields)
|
||||
case struktDel.FullCommand():
|
||||
return nomsStructDel(*delSpec, *delFields)
|
||||
return nomsStructDel(ctx, *delSpec, *delFields)
|
||||
}
|
||||
d.Panic("notreached")
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
func nomsStructNew(dbStr string, name string, args []string) int {
|
||||
func nomsStructNew(ctx context.Context, dbStr string, name string, args []string) int {
|
||||
sp, err := spec.ForDatabase(dbStr)
|
||||
d.PanicIfError(err)
|
||||
applyStructEdits(sp, types.NewStruct(name, nil), nil, args)
|
||||
applyStructEdits(ctx, sp, types.NewStruct(name, nil), nil, args)
|
||||
return 0
|
||||
}
|
||||
|
||||
func nomsStructSet(specStr string, args []string) int {
|
||||
func nomsStructSet(ctx context.Context, specStr string, args []string) int {
|
||||
sp, err := spec.ForPath(specStr)
|
||||
d.PanicIfError(err)
|
||||
|
||||
rootVal, basePath := splitPath(sp)
|
||||
applyStructEdits(sp, rootVal, basePath, args)
|
||||
rootVal, basePath := splitPath(ctx, sp)
|
||||
applyStructEdits(ctx, sp, rootVal, basePath, args)
|
||||
return 0
|
||||
}
|
||||
|
||||
func nomsStructDel(specStr string, args []string) int {
|
||||
func nomsStructDel(ctx context.Context, specStr string, args []string) int {
|
||||
sp, err := spec.ForPath(specStr)
|
||||
d.PanicIfError(err)
|
||||
|
||||
rootVal, basePath := splitPath(sp)
|
||||
rootVal, basePath := splitPath(ctx, sp)
|
||||
patch := diff.Patch{}
|
||||
for i := 0; i < len(args); i++ {
|
||||
if !types.IsValidStructFieldName(args[i]) {
|
||||
@@ -79,15 +79,15 @@ func nomsStructDel(specStr string, args []string) int {
|
||||
})
|
||||
}
|
||||
|
||||
appplyPatch(sp, rootVal, basePath, patch)
|
||||
appplyPatch(ctx, sp, rootVal, basePath, patch)
|
||||
return 0
|
||||
}
|
||||
|
||||
func splitPath(sp spec.Spec) (rootVal types.Value, basePath types.Path) {
|
||||
db := sp.GetDatabase(context.Background())
|
||||
func splitPath(ctx context.Context, sp spec.Spec) (rootVal types.Value, basePath types.Path) {
|
||||
db := sp.GetDatabase(ctx)
|
||||
rootPath := sp.Path
|
||||
rootPath.Path = types.Path{}
|
||||
rootVal = rootPath.Resolve(context.Background(), db)
|
||||
rootVal = rootPath.Resolve(ctx, db)
|
||||
if rootVal == nil {
|
||||
d.CheckError(fmt.Errorf("Invalid path: %s", sp.String()))
|
||||
return
|
||||
@@ -96,7 +96,7 @@ func splitPath(sp spec.Spec) (rootVal types.Value, basePath types.Path) {
|
||||
return
|
||||
}
|
||||
|
||||
func applyStructEdits(sp spec.Spec, rootVal types.Value, basePath types.Path, args []string) {
|
||||
func applyStructEdits(ctx context.Context, sp spec.Spec, rootVal types.Value, basePath types.Path, args []string) {
|
||||
if len(args)%2 != 0 {
|
||||
d.CheckError(fmt.Errorf("Must be an even number of key/value pairs"))
|
||||
}
|
||||
@@ -104,13 +104,13 @@ func applyStructEdits(sp spec.Spec, rootVal types.Value, basePath types.Path, ar
|
||||
d.CheckErrorNoUsage(fmt.Errorf("No value at: %s", sp.String()))
|
||||
return
|
||||
}
|
||||
db := sp.GetDatabase(context.Background())
|
||||
db := sp.GetDatabase(ctx)
|
||||
patch := diff.Patch{}
|
||||
for i := 0; i < len(args); i += 2 {
|
||||
if !types.IsValidStructFieldName(args[i]) {
|
||||
d.CheckError(fmt.Errorf("Invalid field name: %s at position: %d", args[i], i))
|
||||
}
|
||||
nv, err := argumentToValue(args[i+1], db)
|
||||
nv, err := argumentToValue(ctx, args[i+1], db)
|
||||
if err != nil {
|
||||
d.CheckError(fmt.Errorf("Invalid field value: %s at position %d: %s", args[i+1], i+1, err))
|
||||
}
|
||||
@@ -120,20 +120,20 @@ func applyStructEdits(sp spec.Spec, rootVal types.Value, basePath types.Path, ar
|
||||
NewValue: nv,
|
||||
})
|
||||
}
|
||||
appplyPatch(sp, rootVal, basePath, patch)
|
||||
appplyPatch(ctx, sp, rootVal, basePath, patch)
|
||||
}
|
||||
|
||||
func appplyPatch(sp spec.Spec, rootVal types.Value, basePath types.Path, patch diff.Patch) {
|
||||
db := sp.GetDatabase(context.Background())
|
||||
baseVal := basePath.Resolve(context.Background(), rootVal, db)
|
||||
func appplyPatch(ctx context.Context, sp spec.Spec, rootVal types.Value, basePath types.Path, patch diff.Patch) {
|
||||
db := sp.GetDatabase(ctx)
|
||||
baseVal := basePath.Resolve(ctx, rootVal, db)
|
||||
if baseVal == nil {
|
||||
d.CheckErrorNoUsage(fmt.Errorf("No value at: %s", sp.String()))
|
||||
}
|
||||
|
||||
newRootVal := diff.Apply(context.Background(), rootVal, patch)
|
||||
newRootVal := diff.Apply(ctx, rootVal, patch)
|
||||
d.Chk.NotNil(newRootVal)
|
||||
r := db.WriteValue(context.Background(), newRootVal)
|
||||
db.Flush(context.Background())
|
||||
r := db.WriteValue(ctx, newRootVal)
|
||||
db.Flush(ctx)
|
||||
newAbsPath := spec.AbsolutePath{
|
||||
Hash: r.TargetHash(),
|
||||
Path: basePath,
|
||||
|
||||
@@ -43,9 +43,9 @@ func setupSyncFlags() *flag.FlagSet {
|
||||
return syncFlagSet
|
||||
}
|
||||
|
||||
func runSync(args []string) int {
|
||||
func runSync(ctx context.Context, args []string) int {
|
||||
cfg := config.NewResolver()
|
||||
sourceStore, sourceObj, err := cfg.GetPath(context.Background(), args[0])
|
||||
sourceStore, sourceObj, err := cfg.GetPath(ctx, args[0])
|
||||
d.CheckError(err)
|
||||
defer sourceStore.Close()
|
||||
|
||||
@@ -53,7 +53,7 @@ func runSync(args []string) int {
|
||||
d.CheckErrorNoUsage(fmt.Errorf("Object not found: %s", args[0]))
|
||||
}
|
||||
|
||||
sinkDB, sinkDataset, err := cfg.GetDataset(context.Background(), args[1])
|
||||
sinkDB, sinkDataset, err := cfg.GetDataset(ctx, args[1])
|
||||
d.CheckError(err)
|
||||
defer sinkDB.Close()
|
||||
|
||||
@@ -84,12 +84,12 @@ func runSync(args []string) int {
|
||||
nonFF := false
|
||||
err = d.Try(func() {
|
||||
defer profile.MaybeStartProfile().Stop()
|
||||
datas.Pull(context.Background(), sourceStore, sinkDB, sourceRef, progressCh)
|
||||
datas.Pull(ctx, sourceStore, sinkDB, sourceRef, progressCh)
|
||||
|
||||
var err error
|
||||
sinkDataset, err = sinkDB.FastForward(context.Background(), sinkDataset, sourceRef)
|
||||
sinkDataset, err = sinkDB.FastForward(ctx, sinkDataset, sourceRef)
|
||||
if err == datas.ErrMergeNeeded {
|
||||
sinkDataset, err = sinkDB.SetHead(context.Background(), sinkDataset, sourceRef)
|
||||
sinkDataset, err = sinkDB.SetHead(ctx, sinkDataset, sourceRef)
|
||||
nonFF = true
|
||||
}
|
||||
d.PanicIfError(err)
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
@@ -26,7 +27,7 @@ func setupVersionFlags() *flag.FlagSet {
|
||||
return flag.NewFlagSet("version", flag.ExitOnError)
|
||||
}
|
||||
|
||||
func runVersion(args []string) int {
|
||||
func runVersion(ctx context.Context, args []string) int {
|
||||
fmt.Fprintf(os.Stdout, "format version: %v\n", constants.NomsVersion)
|
||||
fmt.Fprintf(os.Stdout, "built from %v\n", constants.NomsGitSHA)
|
||||
return 0
|
||||
|
||||
@@ -56,7 +56,7 @@ type nodeChild struct {
|
||||
Value nodeInfo `json:"value"`
|
||||
}
|
||||
|
||||
func Cmd(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
|
||||
func Cmd(ctx context.Context, noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
|
||||
splore := noms.Command("splore", `Interactively explore a Noms database using a web browser.
|
||||
See Spelling Objects at https://github.com/attic-labs/noms/blob/master/doc/spelling.md for details on the path argument.
|
||||
`)
|
||||
@@ -64,40 +64,40 @@ See Spelling Objects at https://github.com/attic-labs/noms/blob/master/doc/spell
|
||||
browser := splore.Flag("browser", "Immediately open a web browser.").Short('b').Bool()
|
||||
spec := splore.Arg("database or path", "The noms database or path to splore.").Required().String()
|
||||
return splore, func(input string) (exitCode int) {
|
||||
return run(&http.ServeMux{}, *port, *browser, *spec)
|
||||
return run(ctx, &http.ServeMux{}, *port, *browser, *spec)
|
||||
}
|
||||
}
|
||||
|
||||
func run(mux *http.ServeMux, port int, browser bool, spStr string) int {
|
||||
func run(ctx context.Context, mux *http.ServeMux, port int, browser bool, spStr string) int {
|
||||
var sp spec.Spec
|
||||
var getValue func() types.Value
|
||||
|
||||
cfg := config.NewResolver()
|
||||
if pathSp, err := spec.ForPath(cfg.ResolvePathSpec(spStr)); err == nil {
|
||||
sp = pathSp
|
||||
getValue = func() types.Value { return sp.GetValue(context.Background()) }
|
||||
getValue = func() types.Value { return sp.GetValue(ctx) }
|
||||
} else if dbSp, err := spec.ForDatabase(cfg.ResolveDbSpec(spStr)); err == nil {
|
||||
sp = dbSp
|
||||
getValue = func() types.Value { return sp.GetDatabase(context.Background()).Datasets(context.Background()) }
|
||||
getValue = func() types.Value { return sp.GetDatabase(ctx).Datasets(ctx) }
|
||||
} else {
|
||||
d.CheckError(fmt.Errorf("Not a path or database: %s", spStr))
|
||||
}
|
||||
|
||||
defer sp.Close()
|
||||
|
||||
req := func(w http.ResponseWriter, contentType string) {
|
||||
sp.GetDatabase(context.Background()).Rebase(context.Background())
|
||||
req := func(ctx context.Context, w http.ResponseWriter, contentType string) {
|
||||
sp.GetDatabase(ctx).Rebase(ctx)
|
||||
w.Header().Add("Content-Type", contentType)
|
||||
w.Header().Add("Cache-Control", "max-age=0,no-cache")
|
||||
}
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
req(w, "text/html")
|
||||
req(r.Context(), w, "text/html")
|
||||
fmt.Fprintf(w, indexHtml, sp.String())
|
||||
})
|
||||
|
||||
mux.HandleFunc("/out.js", func(w http.ResponseWriter, r *http.Request) {
|
||||
req(w, "application/javascript")
|
||||
req(r.Context(), w, "application/javascript")
|
||||
// To develop JS, uncomment this line and run `yarn start`:
|
||||
//http.ServeFile(w, r, "splore/out.js")
|
||||
// To build noms-splore. uncomment this line and run `yarn buildgo`:
|
||||
@@ -105,7 +105,7 @@ func run(mux *http.ServeMux, port int, browser bool, spStr string) int {
|
||||
})
|
||||
|
||||
mux.HandleFunc("/getNode", func(w http.ResponseWriter, r *http.Request) {
|
||||
req(w, "application/json")
|
||||
req(r.Context(), w, "application/json")
|
||||
r.ParseForm()
|
||||
id := r.Form.Get("id")
|
||||
|
||||
@@ -116,10 +116,10 @@ func run(mux *http.ServeMux, port int, browser bool, spStr string) int {
|
||||
case id[0] == '#':
|
||||
abspath, err := spec.NewAbsolutePath(id)
|
||||
d.PanicIfError(err)
|
||||
v = abspath.Resolve(r.Context(), sp.GetDatabase(context.Background()))
|
||||
v = abspath.Resolve(r.Context(), sp.GetDatabase(r.Context()))
|
||||
default:
|
||||
path := types.MustParsePath(id)
|
||||
v = path.Resolve(r.Context(), getValue(), sp.GetDatabase(context.Background()))
|
||||
v = path.Resolve(r.Context(), getValue(), sp.GetDatabase(r.Context()))
|
||||
}
|
||||
|
||||
if v == nil {
|
||||
|
||||
@@ -42,7 +42,7 @@ func TestNomsSplore(t *testing.T) {
|
||||
return nil
|
||||
}
|
||||
|
||||
go func() { run(&http.ServeMux{}, 0, false, "nbs:"+dir) }()
|
||||
go func() { run(context.Background(), &http.ServeMux{}, 0, false, "nbs:"+dir) }()
|
||||
l := <-lchan
|
||||
defer l.Close()
|
||||
|
||||
|
||||
+2
-1
@@ -6,6 +6,7 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
@@ -16,7 +17,7 @@ import (
|
||||
type Command struct {
|
||||
// Run runs the command.
|
||||
// The args are the arguments after the command name.
|
||||
Run func(args []string) int
|
||||
Run func(ctx context.Context, args []string) int
|
||||
|
||||
// UsageLine is the one-line usage message.
|
||||
// The first word in the line is taken to be the command name.
|
||||
|
||||
@@ -4,7 +4,10 @@
|
||||
|
||||
package util
|
||||
|
||||
import "github.com/attic-labs/kingpin"
|
||||
import (
|
||||
"context"
|
||||
"github.com/attic-labs/kingpin"
|
||||
)
|
||||
|
||||
type KingpinHandler func(input string) (exitCode int)
|
||||
type KingpinCommand func(*kingpin.Application) (*kingpin.CmdClause, KingpinHandler)
|
||||
type KingpinCommand func(context.Context, *kingpin.Application) (*kingpin.CmdClause, KingpinHandler)
|
||||
|
||||
Reference in New Issue
Block a user