github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/store/cmd/noms/noms_root.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/datas"
    35  	"github.com/dolthub/dolt/go/store/types"
    36  )
    37  
    38  var nomsRoot = &util.Command{
    39  	Run:       runRoot,
    40  	UsageLine: "root <db-spec>",
    41  	Short:     "Get the current root hash of the entire database",
    42  	Long:      "See Spelling Objects at https://github.com/attic-labs/noms/blob/master/doc/spelling.md for details on the database argument.",
    43  	Flags:     setupRootFlags,
    44  	Nargs:     1,
    45  }
    46  
    47  var updateRoot = ""
    48  
    49  func setupRootFlags() *flag.FlagSet {
    50  	flagSet := flag.NewFlagSet("root", flag.ExitOnError)
    51  	return flagSet
    52  }
    53  
    54  func runRoot(ctx context.Context, args []string) int {
    55  	if len(args) < 1 {
    56  		fmt.Fprintln(os.Stderr, "Not enough arguments")
    57  		return 0
    58  	}
    59  
    60  	cfg := config.NewResolver()
    61  	db, _, _, err := cfg.GetDatabase(ctx, args[0])
    62  	util.CheckErrorNoUsage(err)
    63  	defer db.Close()
    64  	currRoot, err := datas.ChunkStoreFromDatabase(db).Root(ctx)
    65  	if err != nil {
    66  		fmt.Fprintln(os.Stderr, "error getting root.", err)
    67  		return 1
    68  	}
    69  
    70  	fmt.Println(currRoot)
    71  	return 0
    72  }
    73  
    74  func mustType(t *types.Type, err error) *types.Type {
    75  	d.PanicIfError(err)
    76  	return t
    77  }
    78  
    79  func mustString(str string, err error) string {
    80  	d.PanicIfError(err)
    81  	return str
    82  }
    83  
    84  func mustValue(v types.Value, err error) types.Value {
    85  	d.PanicIfError(err)
    86  	return v
    87  }