-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
44 lines (37 loc) · 1.13 KB
/
helpers.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
package pkg
import "fmt"
// GetUserDisk returns the user's default disk or the disk with the desired id.
// It also returns the crypto info for the disk
func GetUserDisk(token string, disk string) (*Disk, *CryptoInfo, error) {
resp, err := ApiRequest(token, "disks.get", nil)
if err != nil {
return nil, nil, err
}
// At the moment results are not a structure, so we need to cast it to a map
disks, err := MapToStruct[DisksInfo](resp.Result)
if err != nil {
return nil, nil, err
}
if len(disks.List) == 0 {
return nil, nil, fmt.Errorf("users default disk not found")
}
var diskInfo *Disk
for _, nextDisk := range disks.List {
// If the disk is set, we need to find the disk with the desired id
if disk != "" && nextDisk.ID == disk {
diskInfo = nextDisk
break
}
// If the disk is not set, we need to return the first disk as default
if disk == "" {
diskInfo = nextDisk
break
}
}
if diskInfo == nil {
return nil, nil, fmt.Errorf("disk not found")
}
cryptoKey := diskInfo.CryptoKey
publicKey := diskInfo.PublicKey
return diskInfo, &CryptoInfo{EncryptedCryptoKey: cryptoKey, PublicKey: publicKey}, nil
}