github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/cmd/noms/noms_ds.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  	"context"
    26  	"fmt"
    27  	"os"
    28  
    29  	flag "github.com/juju/gnuflag"
    30  
    31  	"github.com/dolthub/dolt/go/store/cmd/noms/util"
    32  	"github.com/dolthub/dolt/go/store/config"
    33  	"github.com/dolthub/dolt/go/store/d"
    34  	"github.com/dolthub/dolt/go/store/types"
    35  	"github.com/dolthub/dolt/go/store/util/verbose"
    36  )
    37  
    38  var toDelete string
    39  
    40  var nomsDs = &util.Command{
    41  	Run:       runDs,
    42  	UsageLine: "ds [<database> | -d <dataset>]",
    43  	Short:     "Noms dataset management",
    44  	Long:      "See Spelling Objects at https://github.com/attic-labs/noms/blob/master/doc/spelling.md for details on the database and dataset arguments.",
    45  	Flags:     setupDsFlags,
    46  	Nargs:     0,
    47  }
    48  
    49  func setupDsFlags() *flag.FlagSet {
    50  	dsFlagSet := flag.NewFlagSet("ds", flag.ExitOnError)
    51  	dsFlagSet.StringVar(&toDelete, "d", "", "dataset to delete")
    52  	verbose.RegisterVerboseFlags(dsFlagSet)
    53  	return dsFlagSet
    54  }
    55  
    56  func runDs(ctx context.Context, args []string) int {
    57  	cfg := config.NewResolver()
    58  	if toDelete != "" {
    59  		db, set, err := cfg.GetDataset(ctx, toDelete)
    60  		util.CheckError(err)
    61  		defer db.Close()
    62  
    63  		oldCommitRef, errBool, err := set.MaybeHeadRef()
    64  		d.PanicIfError(err)
    65  
    66  		if !errBool {
    67  			util.CheckError(fmt.Errorf("Dataset %v not found", set.ID()))
    68  		}
    69  
    70  		_, err = set.Database().Delete(ctx, set)
    71  		util.CheckError(err)
    72  
    73  		fmt.Printf("Deleted %v (was #%v)\n", toDelete, oldCommitRef.TargetHash().String())
    74  	} else {
    75  		dbSpec := ""
    76  		if len(args) >= 1 {
    77  			dbSpec = args[0]
    78  		}
    79  		store, err := cfg.GetDatabase(ctx, dbSpec)
    80  		util.CheckError(err)
    81  		defer store.Close()
    82  
    83  		dss, err := store.Datasets(ctx)
    84  
    85  		if err != nil {
    86  			fmt.Fprintln(os.Stderr, "failed to get datasets")
    87  			return 1
    88  		}
    89  
    90  		_ = dss.IterAll(ctx, func(k, v types.Value) error {
    91  			fmt.Println(k)
    92  			return nil
    93  		})
    94  	}
    95  	return 0
    96  }