github.com/jbendotnet/noms@v0.0.0-20190904222105-c43e4293ea92/cmd/noms/noms_ds.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/attic-labs/noms/cmd/util"
    12  	"github.com/attic-labs/noms/go/config"
    13  	"github.com/attic-labs/noms/go/d"
    14  	"github.com/attic-labs/noms/go/types"
    15  )
    16  
    17  func nomsDs(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
    18  	cmd := noms.Command("ds", "Dataset management.")
    19  	del := cmd.Flag("delete", "delete a dataset").Short('d').Bool()
    20  	name := cmd.Arg("name", "name of the database to list or dataset to delete - see Spelling Objects at https://github.com/attic-labs/noms/blob/master/doc/spelling.md").String()
    21  
    22  	return cmd, func(input string) int {
    23  		cfg := config.NewResolver()
    24  		if *del {
    25  			db, set, err := cfg.GetDataset(*name)
    26  			d.CheckError(err)
    27  			defer db.Close()
    28  
    29  			oldCommitRef, errBool := set.MaybeHeadRef()
    30  			if !errBool {
    31  				d.CheckError(fmt.Errorf("Dataset %v not found", set.ID()))
    32  			}
    33  
    34  			_, err = set.Database().Delete(set)
    35  			d.CheckError(err)
    36  
    37  			fmt.Printf("Deleted %v (was #%v)\n", *name, oldCommitRef.TargetHash().String())
    38  		} else {
    39  			store, err := cfg.GetDatabase(*name)
    40  			d.CheckError(err)
    41  			defer store.Close()
    42  
    43  			store.Datasets().IterAll(func(k, v types.Value) {
    44  				fmt.Println(k)
    45  			})
    46  		}
    47  		return 0
    48  	}
    49  }