-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_cmd.go
83 lines (70 loc) · 2.01 KB
/
insert_cmd.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
78
79
80
81
82
83
// Copyright (c) 2020 BVK Chaitanya
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"syscall"
"github.com/bvk/past/store"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/xerrors"
)
var insertCmd = &cobra.Command{
Use: "insert [flags] <password-file>",
Short: "Inserts a password to the in a new password-file.",
RunE: cmdInsert,
}
func init() {
flags := insertCmd.Flags()
flags.String("user", "", "Username to save along with the password.")
}
func cmdInsert(cmd *cobra.Command, args []string) (status error) {
flags := cmd.Flags()
ps, err := newPasswordStore(flags)
if err != nil {
return xerrors.Errorf("could not create password-store instance: %w", err)
}
if len(args) == 0 {
return xerrors.Errorf("search string argument is required: %w", os.ErrInvalid)
}
if len(args) > 1 {
return xerrors.Errorf("too many search string arguments: %w", os.ErrInvalid)
}
file := filepath.Join("./", args[0])
user, err := flags.GetString("user")
if err != nil {
return xerrors.Errorf("could not get --user value: %w", err)
}
// Read the password without echo.
passwd1, err := getPassword("Password:")
if err != nil {
return xerrors.Errorf("could not read password: %w", err)
}
passwd2, err := getPassword("Retype password:")
if err != nil {
return xerrors.Errorf("could not read retyped password: %w", err)
}
if passwd1 != passwd2 {
return xerrors.Errorf("passwords do not match: %w", os.ErrInvalid)
}
vs := store.NewValues(nil)
if len(user) > 0 {
vs.Set("username", user)
}
data := store.Format(passwd1, vs.Bytes())
if err := ps.CreateFile(file, data, os.FileMode(0644)); err != nil {
return xerrors.Errorf("could not insert new file %q: %w", file, err)
}
return nil
}
func getPassword(prompt string) (string, error) {
fmt.Print(prompt)
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
fmt.Println()
if err != nil {
return "", xerrors.Errorf("could not read password: %w", err)
}
return strings.TrimSpace(string(bytePassword)), nil
}