github.com/adowair/kvdb@v0.0.0-20231101174258-ceca93b03596/cmd/get.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/adowair/kvdb/kv"
     7  	"github.com/spf13/cobra"
     8  )
     9  
    10  // getCmd represents the get command
    11  var getCmd = &cobra.Command{
    12  	Use:   "get",
    13  	Short: "Get the value for a key",
    14  	Long: `Get the value of a key in the database.
    15  If the key does not exist, an error is returned.`,
    16  	Args: cobra.ExactArgs(1),
    17  	RunE: func(cmd *cobra.Command, args []string) error {
    18  		key := args[0]
    19  		val, err := kv.Get(key)
    20  		if err != nil {
    21  			return err
    22  		}
    23  
    24  		fmt.Printf("Key %s: %s\n", key, val)
    25  		return nil
    26  	},
    27  }
    28  
    29  func init() {
    30  	rootCmd.AddCommand(getCmd)
    31  
    32  	// Here you will define your flags and configuration settings.
    33  
    34  	// Cobra supports Persistent Flags which will work for this command
    35  	// and all subcommands, e.g.:
    36  	// getCmd.PersistentFlags().String("foo", "", "A help for foo")
    37  
    38  	// Cobra supports local flags which will only run when this command
    39  	// is called directly, e.g.:
    40  	// getCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
    41  }