-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow_cmd.go
60 lines (50 loc) · 1.33 KB
/
show_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
// Copyright (c) 2020 BVK Chaitanya
package main
import (
"bytes"
"fmt"
"os"
"github.com/spf13/cobra"
"golang.org/x/xerrors"
)
var showCmd = &cobra.Command{
Use: "show [flags] <password-file>",
Short: "Decrypts a password-file and prints it's content.",
RunE: cmdShow,
}
func init() {
flags := showCmd.Flags()
flags.Uint32("line", 0, "When non-zero, only prints the data at given line.")
}
func cmdShow(cmd *cobra.Command, args []string) 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("password file argument is required: %w", os.ErrInvalid)
}
if len(args) > 1 {
return xerrors.Errorf("too many arguments: %w", os.ErrInvalid)
}
file := args[0]
line, err := flags.GetUint32("line")
if err != nil {
return xerrors.Errorf("could not get --line value: %w", err)
}
decrypted, err := ps.ReadFile(file)
if err != nil {
return xerrors.Errorf("could not read file %q: %w", file, err)
}
if line == 0 {
fmt.Printf("%s", decrypted)
return nil
}
lines := bytes.Split(decrypted, []byte("\n"))
if l := len(lines); line >= uint32(l) {
return xerrors.Errorf("file %q doesn't have line %d: %w", line, os.ErrInvalid)
}
fmt.Printf("%s\n", lines[int(line)])
return nil
}