/
example.go
72 lines (61 loc) · 1.56 KB
/
example.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
// Example showing how to create, read, update, delete accounts.
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"strings"
"github.com/ansd/lastpass-go"
)
func main() {
// Read LastPass username and master password from file.
b, err := ioutil.ReadFile("../credentials.txt")
if err != nil {
log.Fatalln(err)
}
lines := strings.Split(string(b), "\n")
username := lines[0]
masterPassword := lines[1]
// NewClient() authenticates with LastPass servers.
// Read examples at https://pkg.go.dev/github.com/ansd/lastpass-go#NewClient for two-factor authentication.
client, err := lastpass.NewClient(context.Background(), username, masterPassword)
if err != nil {
log.Fatalln(err)
}
account := &lastpass.Account{
Name: "my site",
Username: "my user",
Password: "my pwd",
URL: "https://myURL",
Group: "my group",
Notes: "my notes",
}
// Add() account
if err = client.Add(context.Background(), account); err != nil {
log.Fatalln(err)
}
// read all Accounts()
accounts, err := client.Accounts(context.Background())
if err != nil {
log.Fatalln(err)
}
// print all Accounts
for _, a := range accounts {
fmt.Printf("%+v\n", a)
}
// Update() account
account.Username = "updated user"
account.Password = "updated password"
if err = client.Update(context.Background(), account); err != nil {
log.Fatalln(err)
}
// Delete() account
if err = client.Delete(context.Background(), account); err != nil {
log.Fatalln(err)
}
// Logout()
if err = client.Logout(context.Background()); err != nil {
log.Fatalln(err)
}
}