This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (89 loc) · 2.09 KB
/
main.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"crypto/sha256"
"flag"
"fmt"
"github.com/andriikushch/password-storage/menu"
"log"
"os"
"syscall"
"github.com/andriikushch/password-storage/repository"
"golang.org/x/crypto/ssh/terminal"
)
const version = "0.0.6"
func main() {
var l, ac, a, g, d, v, i bool
flag.BoolVar(&l, "l", false, "list of stored accounts")
flag.BoolVar(&a, "a", false, "add new account with random password")
flag.BoolVar(&ac, "ac", false, "add new username:password")
flag.BoolVar(&g, "g", false, "copy to clip board password for account")
flag.BoolVar(&d, "d", false, "delete password for account")
flag.BoolVar(&v, "v", false, "version")
flag.BoolVar(&i, "i", false, "interactive mode")
flag.Parse()
if v {
fmt.Println(version)
return
}
m := menu.NewMenu(repository.NewPasswordRepository(userHomeDir() + "/.dat2"))
fmt.Print("Enter master password: ")
masterPassword, err := terminal.ReadPassword(syscall.Stdin)
tmpKey := sha256.Sum256(masterPassword)
key := tmpKey[:]
if err != nil {
panic("Can't read password input")
}
fmt.Println("")
if !i {
var err error
switch true {
case a:
err = m.AddAccountWithRandomPasswordMenuItem(key)
case ac:
err = m.AddCredentialsMenuItem(key)
case g:
err = m.GetPasswordForAccountMenuItem(key)
case l:
m.PrintAccountMenuItem(key)
case d:
err = m.DeleteAccountMenuItem(key)
}
if err != nil {
log.Fatalln(err)
}
} else {
var quit bool
for !quit {
var err error
fmt.Println("Print command: ")
var command string
_, err = fmt.Scanln(&command)
if err != nil {
panic("Can't read command input")
}
switch command {
case "a":
err = m.AddAccountWithRandomPasswordMenuItem(key)
case "ac":
err = m.AddCredentialsMenuItem(key)
case "g":
err = m.GetPasswordForAccountMenuItem(key)
case "l":
m.PrintAccountMenuItem(key)
case "d":
err = m.DeleteAccountMenuItem(key)
case "q":
quit = true
}
if err != nil {
log.Fatalln(err)
}
}
}
}
func userHomeDir() string {
if home := os.Getenv("HOME"); home != "" {
return home
}
panic("$HOME is not set")
}