-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
56 lines (48 loc) · 1.22 KB
/
utils.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
/*
Different utility functions used throughout the project.
*/
package main
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// Utility functions for ConstructTreeFromDrpPaths:
// Returns the string after the last '/'.
// E.g: /preda/raluca/antonio -> antonio
// /preda/raluca -> raluca
func lastFolderFromPath(path string) string {
slices := strings.Split(path, "/")
return slices[len(slices)-1]
}
// Returns the string before the last '/'.
// E.g: /preda/raluca/antonio -> /preda/raluca
// /preda/raluca -> /preda
func firstPartFromPath(path string) string {
return path[:strings.LastIndex(path, "/")]
}
// Utility functions for tokenManager.go :
// Retrieves the AccessToken from its file.
func readToken(filePath string) (string, error) {
b, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
return string(b), nil
}
// Stores the AccessToken in its file.
func writeToken(filePath string, token string) {
// Check if file exists
if _, err := os.Stat(filePath); os.IsNotExist(err) {
// Doesn't exist; lets create it
err = os.MkdirAll(filepath.Dir(filePath), 0700)
if err != nil {
return
}
}
b := []byte(token)
if err := ioutil.WriteFile(filePath, b, 0600); err != nil {
return
}
}