-
Notifications
You must be signed in to change notification settings - Fork 0
/
listfolder.go
85 lines (75 loc) · 2.19 KB
/
listfolder.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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/fatih/color"
)
type listfolderResult struct {
Result int `json:"result"`
Metadata listfolderMetadata `json:"metadata"`
}
type listfolderMetadata struct {
Path string `json:"path"`
Name string `json:"name"`
Created string `json:"created"`
Ismine bool `json:"ismine"`
Thumb bool `json:"thumb"`
Modified string `json:"Modified"`
Id string `json:"id"`
IsShared bool `json:"isshared"`
Icon string `json:"icon"`
IsFolder bool `json:"isfolder"`
FolderId int `json:"folderid"`
Contents []listfolderMetadata `json:"contents"`
}
var printFolder = color.New(color.FgBlue).PrintlnFunc()
func listfolder(auth, path string, recursive, showdeleted, nofiles, noshares bool) error {
// must start with `/`, must remove end of `/`
path = strings.Trim(path, "/")
path = "/" + path
params := url.Values{
"auth": {auth},
"path": {path},
"recursive": {strconv.Itoa(btoi(recursive))},
"showdeleted": {strconv.Itoa(btoi(showdeleted))},
"nofiles": {strconv.Itoa(btoi(nofiles))},
"noshares": {strconv.Itoa(btoi(noshares))},
}
url := fmt.Sprintf("https://api.pcloud.com/listfolder?%s", params.Encode())
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("get listfolder: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("read listfolder body: %v", err)
}
// debug
// fmt.Println(string(body))
var result listfolderResult
if err := json.Unmarshal(body, &result); err != nil {
return fmt.Errorf("unmarshal listfolder result %s: %v", string(body), err)
}
printListfolder("/", result.Metadata)
return nil
}
func printListfolder(path string, ls listfolderMetadata) {
if ls.Path == "/" {
printFolder("/")
} else if ls.IsFolder {
path += ls.Name + "/"
printFolder(path)
} else {
path += ls.Name
fmt.Println(path)
}
for _, content := range ls.Contents {
printListfolder(path, content)
}
}