next-eeze.git

ref: b8662330e5a2aed7af1568e6cddb044597857f5c

operation/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
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
package operation

import (
	"notabug.org/apiote/next-eeze/fs"
	
	"log"
	"strings"
	"errors"
)

// todo memguard masterPassword
func Get(username, label, url string, full, short bool, masterPassword string) (string, error) {
	if full && short {
		err := errors.New("Full and short specified at the same time")
		log.Fatal("Error. ", err)
		return "", err
	}
	// todo memguard
	passwords, err := fs.Read(masterPassword)
	if err != nil {
		return "", err
	}
	// todo memguard
	resultPasswords := []string{}
	// todo memguard
	for _, p := range passwords {
		if (username == "" || username == p.Username) &&
			(label == "" || label == p.Label) &&
			(url == "" || url == p.Url) {
			if short {
				resultPasswords = append(resultPasswords, p.Password)
			} else if full {
				resultPassword := p.Label
				if p.Url != "" {
					resultPassword = resultPassword + " ("+p.Url+")"
				}
				resultPassword += "\nUsername: "
				if p.Username != "" {
					resultPassword = resultPassword + p.Username
				} else {
					resultPassword = resultPassword + "-"
				}
				resultPassword += "\nPassword: " + p.Password
				if p.Notes != "" {
				resultPassword += "\n" + p.Notes
				}
				resultPasswords = append(resultPasswords, resultPassword)
			} else {
				resultPasswords = append(resultPasswords, p.Username+" / "+p.Password)
			}
		}
	}
	return strings.Join(resultPasswords, "\n\n"), nil
}

// todo memguard masterPassword
func List(masterPassword string) (string, error) {
	// todo memguard
	passwords, err := fs.Read(masterPassword)
	if err != nil {
		return "", err
	}
	// todo memguard
	resultPasswords := []string{}
	// todo memguard
	for _, p := range passwords {
		if p.Username != "" {
			resultPasswords = append(resultPasswords, p.Label+": "+p.Username)
		} else {
			resultPasswords = append(resultPasswords, p.Label)
		}
	}
	return strings.Join(resultPasswords, "\n"), nil
}