Skip to main content

root.go

root.go - Overview

This file defines the root command for the badger CLI tool, which provides tools to manage Badger databases. It handles command execution, flag initialization, and validation of root command arguments.

Detailed Documentation

var sstDir string

This variable stores the directory where the LSM tree files are located. It is populated via command-line flag.

var vlogDir string

This variable stores the directory where the value log files are located. It is populated via command-line flag.

var RootCmd *cobra.Command

This variable represents the base command when called without any subcommands.

  • Purpose: Defines the root command for the Badger CLI tool.
  • Use: "badger"
  • Short: "Tools to manage Badger database."
  • PersistentPreRunE: validateRootCmdArgs

func Execute()

  • Purpose: Adds all child commands to the root command and executes the root command. Handles errors during execution.
  • Parameters: None
  • Returns: None

func init()

  • Purpose: Initializes the command-line flags for the root command.

  • Parameters: None

  • Returns: None

    Initializes the --dir flag for specifying the directory of LSM tree files and the --vlog-dir flag for specifying the directory of value log files.

func validateRootCmdArgs(cmd *cobra.Command, args []string) error

  • Purpose: Validates the arguments passed to the root command.

  • Parameters:

    • cmd *cobra.Command: The Cobra command being executed.
    • args []string: The command-line arguments passed to the command.
  • Returns: error: An error if validation fails, nil otherwise.

    Checks if the --dir flag is specified. If not, returns an error. If --vlog-dir is not specified, it defaults to the value of --dir. Skips validation if the command is a help command.

Getting Started Relevance