cmd

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const DEFAULT_FIELD = "password"
View Source
const ENV_DATABASE = "KEYDEX_DATABASE"
View Source
const ENV_KEY = "KEYDEX_KEY"
View Source
const ENV_PASSPHRASE = "KEYDEX_PASSPHRASE"

Variables

View Source
var Copy = &cobra.Command{
	Short: "Copies a field of a reference to the clipboard.",
	Long: `Copies a field of a reference to the clipboard.

Reads a 'reference' from the database at 'file' and copies the value of 'field' to the clipboard.

The 'file' is the the path to the *.kdbx database. It can be passed either as an argument or via the ` + ENV_DATABASE + ` environment variable.
The 'reference' can be passed either as the last argument, or can be read from stdin - to allow piping.
Use the 'list' command to get a list of all the references in the database.

See "Examples" for more details.`,
	Example: `  # Copy the password of the "github" entry in the "coding" group in the "test" database at test.kdbx
  ` + info.NAME + ` copy test.kdbx /test/coding/github
  
  # Or copy the username instead
  ` + info.NAME + ` copy -f username test.kdbx /test/coding/github

  # Or with stdin
  export ` + ENV_PASSPHRASE + `=${MY_SECRET_PHRASE}
  echo "/test/coding/github" | ` + info.NAME + ` copy test.kdbx

  # Or with stdin and environment variables
  export ` + ENV_PASSPHRASE + `=${MY_SECRET_PHRASE}
  export ` + ENV_DATABASE + `=test.kdbx
  echo "/test/coding/github" | ` + info.NAME + ` copy

  # List entries, browse them with fzf and copy the username to the clipboard
  export ` + ENV_PASSPHRASE + `=${MY_SECRET_PHRASE}
  export ` + ENV_DATABASE + `=test.kdbx

  ` + info.NAME + ` list | fzf | ` + info.NAME + ` copy -f username`,
	Use: "copy [file] [reference]",

	Aliases: []string{"cp", "password", "pwd", "copy-password"},
	Args: cobra.MatchAll(
		cobra.MaximumNArgs(2),
		DatabaseMustBeDefined(),
	),
	RunE: func(cmd *cobra.Command, args []string) error {
		database, reference, key := ReadDatabaseArguments(cmd, args)
		field := cmd.Flag("field").Value.String()
		log.Debugf("Using: database %s, reference %s, key %s", database, reference, key)

		passphrase := credentials.GetPassphrase(database, os.Getenv(ENV_PASSPHRASE))

		return copy(database, key, passphrase, reference, field)
	},
	DisableAutoGenTag: true,
}
View Source
var List = &cobra.Command{
	Short: "Lists all the entries in the database",
	Long: `Lists all the entries in the database. 

The list of references - in the form of - /database/group/.../entry will be printed on stadout, allowing for piping.
The 'file' is the the path to the *.kdbx database. It can be passed either as an argument or via the ` + ENV_DATABASE + ` environment variable.
This command can be used in conjuction with tools such like 'fzf' or 'dmenu' to browse the databse and pipe the result to other commands.

See "Examples" for more details.`,
	Use:     "list [file]",
	Aliases: []string{"ls"},
	Args: cobra.MatchAll(
		cobra.MaximumNArgs(1),
		DatabaseMustBeDefined(),
	),
	Example: `  # List all entries of vault.kdbx database
  ` + info.NAME + ` list vault.kdbx

  # List entries, browse them with fzf and copy the result to the clipboard
  export ` + ENV_PASSPHRASE + `=${MY_SECRET_PHRASE}
  export ` + ENV_DATABASE + `=~/vault.kdbx

  ` + info.NAME + ` list | fzf | ` + info.NAME + ` copy`,
	RunE: func(cmd *cobra.Command, args []string) error {
		database, _, key := ReadDatabaseArguments(cmd, args)
		log.Debugf("Using: database %s, key %s", database, key)
		passphrase := credentials.GetPassphrase(database, os.Getenv(ENV_PASSPHRASE))

		return list(database, key, passphrase)
	},
	DisableAutoGenTag: true,
}
View Source
var Open = &cobra.Command{
	Use:     "open [file] [reference]",
	Short:   "Open the entry editor for a reference.",
	Aliases: []string{"edit"},
	Long: `Open the entry editor for a reference.

Reads a 'reference' from the database at 'file' and opens the editor there. If no reference is passed, it opens a fuzzy search within the editor.

The 'file' is the the path to the *.kdbx database. It can be passed either as an argument or via the ` + ENV_DATABASE + ` environment variable.
The 'reference' can be passed as last argument; if the reference is missing, it opens a fuzzy search.
Use the 'list' command to get a list of all the references in the database.

See "Examples" for more details.`,
	Example: `  # Opens the "github" entry in the "coding" group in the "test" database at test.kdbx
  ` + info.NAME + ` open test.kdbx /test/coding/github
  
  # Open fuzzy search within the test.kdbx database
  ` + info.NAME + ` open test.kdbx

  # Or with environment variables
  export ` + ENV_PASSPHRASE + `=${MY_SECRET_PHRASE}
  export ` + ENV_DATABASE + `=test.kdbx
  ` + info.NAME + ` open

  # List entries, browse them with fzf and edit the result
  export ` + ENV_PASSPHRASE + `=${MY_SECRET_PHRASE}
  export ` + ENV_DATABASE + `=test.kdbx

  ` + info.NAME + ` list | fzf | ` + info.NAME + ` open`,
	Args: cobra.MatchAll(
		cobra.MaximumNArgs(2),
		DatabaseMustBeDefined(),
	),
	RunE: func(cmd *cobra.Command, args []string) error {
		database, reference, key := ReadDatabaseArguments(cmd, args)
		log.Debugf("Using: database %s, reference %s, key %s", database, reference, key)

		passphrase := credentials.GetPassphrase(database, os.Getenv(ENV_PASSPHRASE))

		return open(database, key, passphrase, reference)
	},
	DisableAutoGenTag: true,
}
View Source
var Root = &cobra.Command{
	Use:   info.NAME,
	Short: "Manage KeePass databases from your terminal.",
	Long: info.NAME + ` is a command line utility to manage KeePass databases. It comes with subcommands for managing the entries and a simple, display-oriented editor inspired by the minimalism of GNU nano.

` + info.NAME + ` can read the following environment variables:

  - ` + ENV_PASSPHRASE + ` 
    When this variable is set, ` + info.NAME + ` will skip the password prompt. It can be replaced by utils such as 'autoexpect'.

  - ` + ENV_DATABASE + `
    Is the path to the *.kbdx database to unlock. Providing 'file' inline overrides this value.

  - ` + ENV_KEY + `
    Is the path to the optional *.key file used to unlock the database. Providing the '--key' flag inline overrides this value.

All the entries are referenced with a path-like reference string shaped like /database/group1/../groupN/entry where 'database' is the database name, 'groupN' is the group name, and 'entry' is the entry title. 

Internally all the entries are referenced by a UUID, however ` + info.NAME + ` will read the first occurrence of a reference in cases of conflicts. Writes are always done via UUID and they are threfore conflict-safe.
    
Some commands make use of the system clipboard, in absence of which ` + info.NAME + ` will fail.`,
	Run: func(cmd *cobra.Command, args []string) {
		cmd.Help()
	},
	DisableAutoGenTag: true,
	Version:           info.VERSION,
}

Functions

func DatabaseMustBeDefined

func DatabaseMustBeDefined() cobra.PositionalArgs

func ReadDatabaseArguments

func ReadDatabaseArguments(cmd *cobra.Command, args []string) (database string, reference string, key string)

DATABASE=lol keydex open -> opens list (blank ref) keydex copy database -> gets ref from stdin (blank ref) DATABASE=lol keydex open /ref -> OK (db: lol, ref: /ref) keydex open database /ref -> OK (db: database, ref: /ref) DATABASE=lol keydex open database /ref -> (db database, /ref) keydex open /ref -> uses /ref as db and then fails

func ReadReferenceFromStdin

func ReadReferenceFromStdin(maybeReference string) (string, error)

If zero value reference is passed, reads from stdin to get the value

Types

This section is empty.

Jump to

Keyboard shortcuts

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