Öffentliche Dateiansicht: Raw-Dateien, Tree, Releases und Issues sind ohne Login verfügbar.
cmd/delete.go Raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package cmd

import (
	"bufio"
	"fmt"
	"os"
	"strings"

	"envault/vault"

	"github.com/spf13/cobra"
)

func init() {
	rootCmd.AddCommand(newDeleteCmd())
}

func newDeleteCmd() *cobra.Command {
	var yes bool

	cmd := &cobra.Command{
		Use:   "delete KEY",
		Short: "Remove a secret",
		Args:  cobra.ExactArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			key := normalizeKeyInput(args[0])
			project := resolveProject()

			if !yes {
				fmt.Fprintf(os.Stderr, "Delete %s from project %q? [y/N] ", key, project)
				scanner := bufio.NewScanner(os.Stdin)
				scanner.Scan()
				if !strings.EqualFold(strings.TrimSpace(scanner.Text()), "y") {
					fmt.Fprintln(os.Stderr, "Aborted.")
					return nil
				}
			}

			v, path, password, err := openVault()
			if err != nil {
				return err
			}

			if err := v.Delete(project, key); err != nil {
				return err
			}

			if err := v.Save(path, password); err != nil {
				return err
			}

			_ = vault.RefreshSession(project, v.GetAll(project))
			vault.Audit("delete-secret", fmt.Sprintf("project=%s key=%s", vault.HashName(project), vault.HashName(key)))
			fmt.Fprintf(os.Stderr, "Deleted %s from project %q\n", key, project)
			return nil
		},
	}

	cmd.Flags().BoolVarP(&yes, "yes", "y", false, "skip confirmation")
	return cmd
}