commands

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AddCmd = &cobra.Command{
	Use:     "{a}ddkv [KEY] [VALUE]",
	Short:   "Add or Update a value for a key.",
	Long:    ``,
	Aliases: []string{"a"},
	Args: func(cmd *cobra.Command, args []string) error {
		if len(args) < 2 {
			return errors.New("addkv requires two parameters [key] and [value]. Please try it again")
		}
		return nil
	},
	Run: func(cmd *cobra.Command, args []string) {
		err := database.DB.Update(
			func(tx *nutsdb.Tx) error {
				return tx.Put(database.Bucket, []byte(args[0]), []byte(args[1]), 0)
			})

		must.Must(err, "AddCmd() - oops! Huston, we have a problem adding/updating keys.")
	},
}

AddCmd represents the addkv command

View Source
var DelCmd = &cobra.Command{
	Use:     "{d}elkv [KEY]",
	Short:   "Remove a stored key.",
	Long:    ``,
	Aliases: []string{"d"},
	Args:    cobra.MinimumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		if err := database.DB.Update(
			func(tx *nutsdb.Tx) error {
				key := []byte(args[0])
				return tx.Delete(database.Bucket, key)
			}); err != nil {
			must.Must(err, "DelCmd() - oops! Huston, we have a problem deleting keys. The key does not exist or dataase must be empty.")
		}
	},
}

DelCmd represents the delkv command

View Source
var ExpCmd = &cobra.Command{
	Use:     "{e}xportkv",
	Short:   "Export all keys to a file.",
	Long:    ``,
	Aliases: []string{"e"},
	Run: func(cmd *cobra.Command, args []string) {
		content := make(map[string]string)
		err := database.DB.View(
			func(tx *nutsdb.Tx) error {
				if keys, values, err := tx.GetAll(database.Bucket); err != nil {
					return err
				} else {
					n := len(keys)
					for i := 0; i < n; i++ {
						fmt.Println(string(keys[i]), " ", string(values[i]))
					}
				}

				configFile := kvpath.GetKVHomeDir() + "/.config/kvstok/kvstok.json"
				configHash := kvpath.GetKVHomeDir() + "/.config/kvstok/kvstok.hash"

				fileContent, _ := json.MarshalIndent(content, "", " ")
				_ = os.WriteFile(configFile, fileContent, 0600)

				hash := kvpath.GenHash(configFile)

				_ = os.WriteFile(configHash, []byte(hash), 0600)

				return nil
			})

		must.Must(err, "ExpCmd() - oops! Huston, we have a problem exporting keys.")

		fmt.Printf("Keys exported to ~/.config/kvstok \n Please keep [.json and .hash] files it into safety place.")
	},
}

LstCmd represents the lstkv command

View Source
var GetCmd = &cobra.Command{
	Use:     "{g}etkv [KEY]",
	Short:   "Get a value for a key.",
	Long:    ``,
	Aliases: []string{"g"},
	Args:    cobra.MinimumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {

		if err := database.DB.Update(
			func(tx *nutsdb.Tx) error {
				key := []byte(args[0])
				content, err := tx.Get(database.Bucket, key)
				must.Must(err, "GetCmd() - key not found or datababse must be empty.")
				fmt.Printf("%s\n", content)
				return nil
			}); err != nil {
		}
	},
}

GetCmd represents the getkv command

View Source
var ImpCmd = &cobra.Command{
	Use:     "{i}mportkv",
	Short:   "Rostore all keys from kvstok.json.",
	Long:    ``,
	Aliases: []string{"i"},
	Run: func(cmd *cobra.Command, args []string) {
		var dataResult map[string]string

		configFile := kvpath.GetKVHomeDir() + "/.config/kvstok/kvstok.json"
		configHash := kvpath.GetKVHomeDir() + "/.config/kvstok/kvstok.hash"

		file, err := os.ReadFile(configHash)
		must.Must(err, "ImpCmd() - oops! Huston, we have a problem importing keys.")

		currentHash := kvpath.GenHash(configFile)
		storedHash := []byte(file)

		areEquals := isEquals(currentHash, string(storedHash))

		if !areEquals {

			fmt.Fprintf(os.Stderr, "JSON export key corrupted. Hashcode are not the same.")
			os.Exit(1)
		}

		if areEquals {

			file, err = os.ReadFile(configFile)
			must.Must(err, "ImpCmd() - oops! Huston, we have a problem integrity broken.")

			json.Unmarshal([]byte(file), &dataResult)

			for key, value := range dataResult {
				err := database.DB.Update(
					func(tx *nutsdb.Tx) error {
						key := []byte(key)
						val := []byte(value)
						return tx.Put(database.Bucket, key, val, 0)
					})

				must.Must(err, "ImpCmd() - oops! Huston, we have a problem integrity broken.")
			}
		}

		fmt.Printf("Keys imported successfully.")
	},
}

AddCmd represents the addkv command

View Source
var LstCmd = &cobra.Command{
	Use:     "{l}istkv",
	Short:   "List all keys values pairs.",
	Long:    ``,
	Aliases: []string{"l"},
	Run: func(cmd *cobra.Command, args []string) {
		err := database.DB.View(
			func(tx *nutsdb.Tx) error {
				if keys, values, err := tx.GetAll(database.Bucket); err != nil {
					return err
				} else {
					n := len(keys)
					for i := 0; i < n; i++ {
						fmt.Println(string(keys[i]), " ", string(values[i]))
					}
				}

				return nil
			})

		must.Must(err, "LstCmd() - key not found or datababse must be empty.")
	},
}

LstCmd represents the lstkv command

View Source
var TtlCmd = &cobra.Command{
	Use:     "{t}tladdkv [KEY] [VALUE] [TIME_TO_LIVE_IN_MINUTES]",
	Short:   "Add a key with time to be live. Default 1 minute.",
	Long:    ``,
	Aliases: []string{"t"},
	Args: func(cmd *cobra.Command, args []string) error {
		if len(args) < 2 {
			return errors.New("addkv requires at least two parameters [key] and [value] the param [ttl] it is optional, the default value it is 1 minute. Please try it again")
		}
		return nil
	},
	Run: func(cmd *cobra.Command, args []string) {
		if err := database.DB.Update(
			func(tx *nutsdb.Tx) error {
				key := []byte(args[0])
				val := []byte(args[1])
				ttl := uint32(60)

				if len(args) == 3 {
					temp_ttl, err := strconv.ParseUint(string([]byte(args[2])), 10, 32)
					must.Must(err, "Third param must be a number.")
					ttl = uint32(temp_ttl) * 60
				}

				return tx.Put(database.Bucket, key, val, ttl)
			}); err != nil {
			must.Must(err, "TTLCmd() - oops! Huston, we have a problem adding/updating keys.")
		}
	},
}

AddCmd represents the addkv command

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL