-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
108 lines (92 loc) · 2.16 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/99designs/keyring"
)
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
func run() error {
client := NewClient()
if client.clientID == "" || client.clientSecret == "" {
return fmt.Errorf("the Client ID and Client secret were not found in env vars")
}
ring, err := keyring.Open(keyring.Config{
ServiceName: "monzo-access-token",
})
if err != nil {
return err
}
i, err := ring.Get("tokens")
if err != nil {
if errors.Is(err, keyring.ErrKeyNotFound) {
fmt.Println("tokens not found in keychain, starting auth flow")
if err := oauth(client); err != nil {
return fmt.Errorf("failed to authenticate: %w", err)
}
// join the tokens and save them to the keychain
tokens := client.accessToken + "::" + client.refreshToken
if err := ring.Set(keyring.Item{
Key: "tokens",
Data: []byte(tokens),
Label: "Monzo Access Token",
Description: "Access and refresh tokens for the Monzo API",
}); err != nil {
return fmt.Errorf("failed to set tokens in keychain: %w", err)
}
} else {
return err
}
} else {
// split the tokens from i
tokens := string(i.Data)
tokenSlice := strings.Split(tokens, "::")
if len(tokenSlice) != 2 {
return fmt.Errorf("unexpected token format: %s", tokens)
}
client.accessToken = tokenSlice[0]
client.refreshToken = tokenSlice[1]
}
err = pingTest(client)
if err != nil {
return err
}
accounts, err := monzoAccounts(client)
if err != nil {
return err
}
var ukRetailAccountID string
for _, account := range *accounts {
if account.Type == "uk_retail" {
ukRetailAccountID = account.ID
break
}
}
b, err := balance(client, ukRetailAccountID)
if err != nil {
return err
}
if b.Balance%100 == 0 {
return nil
}
rounded := (b.Balance / 100) * 100
pennies := b.Balance - rounded
if pennies == 0 {
return nil
}
SavingsPotID, err := potIDByName(client, ukRetailAccountID, "Savings")
if err != nil {
return err
}
err = depositToPot(client, ukRetailAccountID, *SavingsPotID, pennies)
if err != nil {
return err
}
return nil
}