-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
66 lines (54 loc) · 1.54 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
/*
Copyright © 2023 sum28it prasad28sumit@gmail.com
*/
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/sum28it/pass-manager/pkg/user"
"golang.org/x/term"
)
// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "Initializes the application",
Long: `init command is the first command to be run after installing the application.
It takes an argument which is the secret for accessing the appication.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Print("Your secret: ")
secret, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
fmt.Println("Error reading secret")
return
}
fmt.Println()
fmt.Print("Confirm secret: ")
retypedSecret, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
fmt.Println("Error reading secret")
return
}
fmt.Println()
if string(secret) != string(retypedSecret) {
fmt.Println("Secrets doesn't match")
return
}
dir, err := user.Init(string(secret))
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Your data is stored at:", dir)
},
}
func init() {
rootCmd.AddCommand(initCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// initCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// initCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}