/
get.go
43 lines (37 loc) · 992 Bytes
/
get.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
package cmd
import (
"fmt"
"os"
"github.com/MattRighetti/passwdvault/configuration"
db "github.com/MattRighetti/passwdvault/database"
"github.com/spf13/cobra"
)
var getCmd = &cobra.Command{
Use: "get [PASSWORD_ID]",
Short: "Gets the password of given identifier if present",
Long: "Get will search for the password of the passed pasword identifier and, if present, it will print it",
PreRunE: func(cmd *cobra.Command, args []string) error {
return configuration.InitCriticalData()
},
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Help()
os.Exit(0)
}
if db.IsPresent(args[0]) {
passwd, err := db.Get(args[0])
if err != nil {
fmt.Printf("Could not get value with key %s\n", args[0])
}
fmt.Println(string(passwd))
} else {
fmt.Printf("No password stored with key %s\n", args[0])
}
},
PostRun: func(cmd *cobra.Command, args []string) {
configuration.CloseDb()
},
}
func init() {
rootCmd.AddCommand(getCmd)
}