secret

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Decrypt = app.Register(&app.Command[FileFlags, app.Empty]{
	Use:  "decrypt [-w] [-o output] {file}",
	Desc: "Decrypt a file use given password",

	Prepare: prepareFileCmd,

	Run: func(ctx *app.Context[FileFlags, app.Empty]) error {
		inPath := ctx.Arg(0)

		data, err := os.ReadFile(inPath)
		if err != nil {
			return err
		}

		password, err := term.InputPassword("Please input password")
		if err != nil {
			return err
		}

		value := strings.TrimSpace(string(data))
		raw, err := crypto.Decrypt(password, "", value)
		if err != nil {
			return err
		}

		var outPath string
		if ctx.Flags.Write {
			outPath = inPath
		} else {
			outPath = ctx.Flags.Output
		}
		if outPath != "" {
			return osutil.WriteFile(outPath, raw)
		}

		fmt.Print(string(raw))
		return nil
	},
})
View Source
var Describe = app.Register(&app.Command[app.Empty, app.Empty]{
	Use:    "secret {key}",
	Desc:   "Show secret value",
	Action: "Describe",

	PrepareNoFlag: func(cmd *cobra.Command) {
		cmd.Args = cobra.ExactArgs(1)
	},

	Run: func(ctx *app.Context[app.Empty, app.Empty]) error {
		key := ctx.Arg(0)
		value, err := core.GetSecret(key, false)
		if err != nil {
			return err
		}
		fmt.Print(value)
		return nil
	},
})
View Source
var Encrypt = app.Register(&app.Command[FileFlags, app.Empty]{
	Use:  "encrypt [-w] [-o output] {file}",
	Desc: "Encrypt a file use given password",

	Prepare: prepareFileCmd,

	Run: func(ctx *app.Context[FileFlags, app.Empty]) error {
		inPath := ctx.Arg(0)

		data, err := os.ReadFile(inPath)
		if err != nil {
			return err
		}

		password, err := term.InputNewPassword("Please input new password")
		if err != nil {
			return err
		}

		encrypted, _, err := crypto.Encrypt(password, data, true)
		if err != nil {
			return err
		}

		var outPath string
		if ctx.Flags.Write {
			outPath = inPath
		} else {
			outPath = ctx.Flags.Output
		}
		if outPath != "" {
			return osutil.WriteFile(outPath, []byte(encrypted))
		}

		fmt.Print(encrypted)
		return nil
	},
})
View Source
var Secret = app.Register(&app.Command[app.Empty, app.Empty]{
	Use:    "secret {key}",
	Desc:   "Delete a secret",
	Action: "Delete",

	PrepareNoFlag: func(cmd *cobra.Command) {
		cmd.Args = cobra.ExactArgs(1)
	},

	Run: func(ctx *app.Context[app.Empty, app.Empty]) error {
		key := ctx.Arg(0)
		return core.DeleteSecret(key)
	},
})

Functions

This section is empty.

Types

type FileFlags

type FileFlags struct {
	Write  bool
	Output string
}

Jump to

Keyboard shortcuts

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