-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.go
77 lines (62 loc) · 1.9 KB
/
generate.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package cmd
import (
"fmt"
"path/filepath"
"github.com/pvwnthem/gopwd/util"
"github.com/spf13/cobra"
)
var generateCmd = &cobra.Command{
Use: "generate [site] [length] [flags]",
Short: "Generates and inserts a new password into the vault",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
site := args[0]
length := args[1]
vaultPath := filepath.Join(Path, Name)
// Check if the vault exists
vaultExists, err := util.Exists(vaultPath)
if err != nil {
return fmt.Errorf("failed to check vault existence: %w", err)
}
if !vaultExists {
return fmt.Errorf("vault does not exist at %s", vaultPath)
}
// Check if the password already exists
passwordExists, err := util.Exists(filepath.Join(vaultPath, site))
if err != nil {
return fmt.Errorf("failed to check password existence: %w", err)
}
if passwordExists {
return fmt.Errorf("password already exists")
}
// Create the directory
dirPath := filepath.Join(vaultPath, site)
err = util.CreateDirectory(dirPath)
if err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
// Generate the password
password := util.GeneratePassword(length)
GPGID, err := util.GetGPGID(vaultPath)
if err != nil {
return fmt.Errorf("failed to get gpg-id: %w", err)
}
GPGModule := util.NewGPGModule(GPGID, "/usr/bin/gpg")
encryptedPassword, err := GPGModule.Encrypt([]byte(password))
if err != nil {
return fmt.Errorf("failed to encrypt password: %w", err)
}
// Create the password file
passwordPath := filepath.Join(dirPath, "password")
err = util.WriteBytesToFile(passwordPath, encryptedPassword)
if err != nil {
return fmt.Errorf("failed to write password to file: %w", err)
}
fmt.Printf("Inserted password for %s at %s\n", site, dirPath)
fmt.Printf("Generated password: %s\n", password)
return nil
},
}
func init() {
rootCmd.AddCommand(generateCmd)
}