github.com/adowair/kvdb@v0.0.0-20231101174258-ceca93b03596/cmd/ts.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  const timeFormat = "2006-01-02 15:04:05"
    11  
    12  // tsCmd represents the ts command
    13  var tsCmd = &cobra.Command{
    14  	Use:   "ts",
    15  	Short: "Get the created and last-modified timestamps for a key",
    16  	Long: `Get the times this key was first and last set
    17  These timestamps are durable--moving a database folder will not affect them.`,
    18  	Args: cobra.ExactArgs(1),
    19  	RunE: func(cmd *cobra.Command, args []string) error {
    20  		key := args[0]
    21  		first, last, err := kv.Timestamps(key)
    22  		if err != nil {
    23  			return err
    24  		}
    25  
    26  		fmt.Printf("Key %s:\n", key)
    27  		fmt.Printf("  First set on %s\n", first.Format(timeFormat))
    28  		fmt.Printf("  Last set on  %s\n", last.Format(timeFormat))
    29  		return nil
    30  	},
    31  }
    32  
    33  func init() {
    34  	rootCmd.AddCommand(tsCmd)
    35  
    36  	// Here you will define your flags and configuration settings.
    37  
    38  	// Cobra supports Persistent Flags which will work for this command
    39  	// and all subcommands, e.g.:
    40  	// tsCmd.PersistentFlags().String("foo", "", "A help for foo")
    41  
    42  	// Cobra supports local flags which will only run when this command
    43  	// is called directly, e.g.:
    44  	// tsCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
    45  }