get.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package operation
  2. import (
  3. "notabug.org/apiote/next-eeze/fs"
  4. "log"
  5. "strings"
  6. "errors"
  7. )
  8. // todo memguard masterPassword
  9. func Get(username, label, url string, full, short bool, masterPassword string) (string, error) {
  10. if full && short {
  11. err := errors.New("Full and short specified at the same time")
  12. log.Fatal("Error. ", err)
  13. return "", err
  14. }
  15. // todo memguard
  16. passwords, err := fs.Read(masterPassword)
  17. if err != nil {
  18. return "", err
  19. }
  20. // todo memguard
  21. resultPasswords := []string{}
  22. // todo memguard
  23. for _, p := range passwords {
  24. if (username == "" || username == p.Username) &&
  25. (label == "" || label == p.Label) &&
  26. (url == "" || url == p.Url) {
  27. if short {
  28. resultPasswords = append(resultPasswords, p.Password)
  29. } else if full {
  30. resultPassword := p.Label
  31. if p.Url != "" {
  32. resultPassword = resultPassword + " ("+p.Url+")"
  33. }
  34. resultPassword += "\nUsername: "
  35. if p.Username != "" {
  36. resultPassword = resultPassword + p.Username
  37. } else {
  38. resultPassword = resultPassword + "-"
  39. }
  40. resultPassword += "\nPassword: " + p.Password
  41. if p.Notes != "" {
  42. resultPassword += "\n" + p.Notes
  43. }
  44. resultPasswords = append(resultPasswords, resultPassword)
  45. } else {
  46. resultPasswords = append(resultPasswords, p.Username+" / "+p.Password)
  47. }
  48. }
  49. }
  50. return strings.Join(resultPasswords, "\n\n"), nil
  51. }
  52. // todo memguard masterPassword
  53. func List(masterPassword string) (string, error) {
  54. // todo memguard
  55. passwords, err := fs.Read(masterPassword)
  56. if err != nil {
  57. return "", err
  58. }
  59. // todo memguard
  60. resultPasswords := []string{}
  61. // todo memguard
  62. for _, p := range passwords {
  63. if p.Username != "" {
  64. resultPasswords = append(resultPasswords, p.Label+": "+p.Username)
  65. } else {
  66. resultPasswords = append(resultPasswords, p.Label)
  67. }
  68. }
  69. return strings.Join(resultPasswords, "\n"), nil
  70. }