-
Notifications
You must be signed in to change notification settings - Fork 14
/
cem.go
75 lines (61 loc) · 1.9 KB
/
cem.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
package cem
import (
"encoding/gob"
"fmt"
"os"
"github.com/infamousjoeg/cybr-cli/pkg/cybr/helpers/util"
)
// SaveToken saving token as file on the local filesystem
func SaveToken(token string, tokenPath string) error {
// Get user home directory
userHome, err := util.GetUserHomeDir()
if err != nil {
return fmt.Errorf("ACL error. %s", err)
}
// Check if .cybr directory already exists, create if not
if _, err = os.Stat(userHome + "/.cybr"); os.IsNotExist(err) {
// Create .cybr folder in user home directory
err = os.Mkdir(userHome+"/.cybr", 0766)
if err != nil {
return fmt.Errorf("could not create folder %s/.cybr on local file system. %s", userHome, err)
}
}
// Check for config file and remove if existing
if _, err = os.Stat(userHome + tokenPath); !os.IsNotExist(err) {
err = os.Remove(userHome + tokenPath)
if err != nil {
return fmt.Errorf("could not remove existing %s%s file. %s", userHome, tokenPath, err)
}
}
// Create config file in user home directory
dataFile, err := os.Create(userHome + tokenPath)
if err != nil {
return fmt.Errorf("could not create configuration file at %s%s. %s", userHome, tokenPath, err)
}
// serialize the data
dataEncoder := gob.NewEncoder(dataFile)
dataEncoder.Encode(token)
dataFile.Close()
return nil
}
// GetToken file from local filesystem and read
func GetToken(tokenPath string) (string, error) {
// Get user home directory
userHome, err := util.GetUserHomeDir()
if err != nil {
return "", fmt.Errorf("ACL error. %s", err)
}
// open data file
dataFile, err := os.Open(userHome + tokenPath)
if err != nil {
return "", fmt.Errorf("failed to retrieve token file at %s. %s", tokenPath, err)
}
dataDecoder := gob.NewDecoder(dataFile)
result := ""
err = dataDecoder.Decode(&result)
if err != nil {
return result, fmt.Errorf("failed to decode token file at .cybr/config. %s", err)
}
dataFile.Close()
return result, nil
}