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

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