forked from XeroAPI/xoauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scopes.go
84 lines (61 loc) · 1.69 KB
/
scopes.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
package config
import (
"errors"
"log"
"strings"
"github.com/NathanBeddoeWebDev/xoauth/pkg/db"
"github.com/spf13/cobra"
)
func ValidateScopeCmdArgs(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("please supply a client name, e.g, `xero`")
}
if len(args) < 2 {
return errors.New("please supply at least one scope, e.g, `accounting.read` ")
}
return nil
}
func AddScope(database *db.CredentialStore, clientName string, scopeNames ...string) {
allClients, clientsErr := database.GetClients()
if clientsErr != nil {
log.Fatal(clientsErr)
}
client, clientErr := database.GetClientWithoutSecret(allClients, clientName)
if clientErr != nil {
log.Fatal(clientErr)
}
for _, scope := range scopeNames {
if Contains(client.Scopes, scope) {
continue
}
client.Scopes = append(client.Scopes, scope)
}
_, saveErr := database.SaveClientMetadata(client)
if saveErr != nil {
log.Fatal(saveErr)
}
log.Printf("Scopes are: \n • %s", strings.Join(client.Scopes, "\n • "))
}
func RemoveScope(database *db.CredentialStore, clientName string, scopeNames ...string) {
allClients, clientsErr := database.GetClients()
if clientsErr != nil {
log.Fatal(clientsErr)
}
client, clientErr := database.GetClientWithoutSecret(allClients, clientName)
if clientErr != nil {
log.Fatal(clientErr)
}
var newScopes []string
for _, scope := range client.Scopes {
if Contains(scopeNames, scope) {
continue
}
newScopes = append(newScopes, scope)
}
client.Scopes = newScopes
_, saveErr := database.SaveClientMetadata(client)
if saveErr != nil {
log.Fatal(saveErr)
}
log.Printf("Scopes are: \n • %s", strings.Join(client.Scopes, "\n • "))
}