@@ -9,22 +9,25 @@ import (
9
9
"strmprivacy/strm/pkg/common"
10
10
)
11
11
12
- const ActiveProjectFilename = "active_projects.json"
12
+ const activeProjectFilename = "active_projects.json"
13
13
14
- var Projects UsersProjects
14
+ var ActiveProjectFilepath = path . Join ( common . ConfigPath (), activeProjectFilename )
15
15
16
- // UsersProjects is the printed json format of the different active projects
16
+ var Projects * UsersProjectsContext
17
+
18
+ // UsersProjectsContext is the printed json format of the different active projects
17
19
// per past or currently logged-in user
18
- type UsersProjects struct {
19
- Users []UserProject `json:"users"`
20
+ type UsersProjectsContext struct {
21
+ Users []UserProjectContext `json:"users"`
20
22
}
21
23
22
- type UserProject struct {
24
+ type UserProjectContext struct {
23
25
Email string `json:"email"`
24
26
ActiveProject string `json:"active_project"`
27
+ ZedToken string `json:"zed_token"`
25
28
}
26
29
27
- func (projects * UsersProjects ) GetCurrentProjectByEmail () string {
30
+ func (projects * UsersProjectsContext ) GetCurrentProjectByEmail () string {
28
31
activeProject := ""
29
32
email := GetUserEmail ()
30
33
for _ , user := range projects .Users {
@@ -35,7 +38,7 @@ func (projects *UsersProjects) GetCurrentProjectByEmail() string {
35
38
return activeProject
36
39
}
37
40
38
- func (projects * UsersProjects ) SetActiveProject (project string ) {
41
+ func (projects * UsersProjectsContext ) SetActiveProject (project string ) {
39
42
email := GetUserEmail ()
40
43
added := false
41
44
for index , user := range projects .Users {
@@ -46,11 +49,13 @@ func (projects *UsersProjects) SetActiveProject(project string) {
46
49
}
47
50
48
51
if ! added {
49
- projects .Users = append (projects .Users , UserProject {
52
+ projects .Users = append (projects .Users , UserProjectContext {
50
53
Email : email ,
51
54
ActiveProject : project ,
52
55
})
53
56
}
57
+
58
+ storeUserProjectContext ()
54
59
}
55
60
56
61
func GetUserEmail () string {
@@ -63,19 +68,53 @@ func GetUserEmail() string {
63
68
return auth .Auth .Email
64
69
}
65
70
66
- func LoadActiveProject () {
67
- activeProjectFilePath := path .Join (common .ConfigPath (), ActiveProjectFilename )
71
+ func initializeUsersProjectsContext () {
72
+ if Projects == nil {
73
+ activeProjectFilePath := path .Join (common .ConfigPath (), activeProjectFilename )
68
74
69
- bytes , err := os .ReadFile (activeProjectFilePath )
70
- common .CliExit (err )
71
- activeProjects := UsersProjects {}
72
- _ = json .Unmarshal (bytes , & activeProjects )
73
- Projects = activeProjects
75
+ bytes , err := os .ReadFile (activeProjectFilePath )
76
+ common .CliExit (err )
77
+ activeProjects := UsersProjectsContext {}
78
+ _ = json .Unmarshal (bytes , & activeProjects )
79
+ Projects = & activeProjects
80
+ }
74
81
}
75
82
76
83
func GetActiveProject () string {
77
- LoadActiveProject ()
84
+ initializeUsersProjectsContext ()
78
85
activeProject := Projects .GetCurrentProjectByEmail ()
79
86
log .Infoln ("Current active project is: " + activeProject )
80
87
return activeProject
81
88
}
89
+
90
+ func SetZedToken (zedToken string ) {
91
+ initializeUsersProjectsContext ()
92
+ email := GetUserEmail ()
93
+ for index , user := range Projects .Users {
94
+ // If there is no entry for the user, a zed token will be added the next time, when it is present
95
+ if user .Email == email {
96
+ (* Projects ).Users [index ].ZedToken = zedToken
97
+ }
98
+ }
99
+
100
+ storeUserProjectContext ()
101
+ }
102
+
103
+ func GetZedToken () * string {
104
+ initializeUsersProjectsContext ()
105
+ email := GetUserEmail ()
106
+ for _ , user := range Projects .Users {
107
+ if user .Email == email && user .ZedToken != "" {
108
+ return & user .ZedToken
109
+ }
110
+ }
111
+ return nil
112
+ }
113
+
114
+ func storeUserProjectContext () {
115
+ projects , err := json .Marshal (Projects )
116
+ common .CliExit (err )
117
+
118
+ err = os .WriteFile (ActiveProjectFilepath , projects , 0644 )
119
+ common .CliExit (err )
120
+ }
0 commit comments