/
init.go
80 lines (67 loc) · 2.03 KB
/
init.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
package cmd
import (
"bufio"
"fmt"
"log"
"os"
"path"
"github.com/MattRighetti/passwdvault/configuration"
"github.com/MattRighetti/passwdvault/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
stdinReader = bufio.NewReader(os.Stdin)
initCmd = &cobra.Command{
Use: "init",
Short: "Initializes configuration files and database",
PreRun: func(cmd *cobra.Command, args []string) {
exists := utils.FileExists(configuration.ConfigFilePath)
if exists {
var res string
fmt.Print("A configuration file already exist, would you like to overwrite it? [y/n]: ")
fmt.Scanf("%s", &res)
if res != "y" {
os.Exit(0)
}
}
},
Run: func(cmd *cobra.Command, args []string) {
var config configuration.Configuration
var masterkey []byte = nil
utils.ReadInputString("Your name: ", &config.User.Name)
utils.ReadInputString("Your email: ", &config.User.Email)
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
config.Database.Path = dir
config.Database.Name = ".passwddatabase"
confirm := utils.ReadBool("Would you like to encrypt the database?")
if confirm {
config.Database.Encrypted = true
for {
masterkey, _ = utils.ReadInputStringHideInput("MasterKey (must be either 16 or 24 or 64 chars): ")
if len(masterkey) == 16 || len(masterkey) == 24 || len(masterkey) == 64 {
break
}
}
config.Database.MasterKey.Length = int8(len(masterkey))
viper.Set("masterkey", string(masterkey))
confirm := utils.ReadBool("Would you like to store the masterkey somewhere in your system in order to avoid writing everytime?")
if confirm {
fmt.Print("Insert path to file: ")
fmt.Scanf("%s", &config.Database.MasterKey.FromFilePath)
}
} else {
config.Database.Encrypted = false
}
dbPath := path.Join(config.Database.Path, config.Database.Name)
configuration.CreateDb(dbPath, masterkey)
configuration.CreateConfigurationFile(&config.User, &config.Database)
},
}
)
func init() {
rootCmd.AddCommand(initCmd)
}