@@ -27,6 +27,14 @@ import (
27
27
28
28
var exportEnvs = false
29
29
var unsetEnvs = false
30
+ var scope = string (envScopeRepo )
31
+
32
+ type envScope string
33
+
34
+ var (
35
+ envScopeRepo envScope = "repo"
36
+ envScopeUser envScope = "user"
37
+ )
30
38
31
39
// envCmd represents the env command
32
40
var envCmd = & cobra.Command {
@@ -67,7 +75,8 @@ delete environment variables with a repository pattern of */foo, foo/* or */*.
67
75
if unsetEnvs {
68
76
err = deleteEnvs (ctx , args )
69
77
} else {
70
- err = setEnvs (ctx , args )
78
+ scopeUser := scope == string (envScopeUser )
79
+ err = setEnvs (ctx , scopeUser , args )
71
80
}
72
81
} else {
73
82
err = getEnvs (ctx )
@@ -80,14 +89,14 @@ type connectToServerResult struct {
80
89
repositoryPattern string
81
90
wsInfo * supervisorapi.WorkspaceInfoResponse
82
91
client * serverapi.APIoverJSONRPC
83
-
84
- useDeprecatedGetEnvVar bool
85
92
}
86
93
87
94
type connectToServerOptions struct {
88
95
supervisorClient * supervisor.SupervisorClient
89
96
wsInfo * api.WorkspaceInfoResponse
90
97
log * log.Entry
98
+
99
+ setEnvScopeUser bool
91
100
}
92
101
93
102
func connectToServer (ctx context.Context , options * connectToServerOptions ) (* connectToServerResult , error ) {
@@ -123,31 +132,23 @@ func connectToServer(ctx context.Context, options *connectToServerOptions) (*con
123
132
}
124
133
repositoryPattern := wsinfo .Repository .Owner + "/" + wsinfo .Repository .Name
125
134
126
- var useDeprecatedGetEnvVar bool
135
+ operations := "create/get/update/delete"
136
+ if options != nil && options .setEnvScopeUser {
137
+ // Updating user env vars requires a different token with a special scope
138
+ repositoryPattern = "*/*"
139
+ operations = "update"
140
+ }
141
+
127
142
clientToken , err := supervisorClient .Token .GetToken (ctx , & supervisorapi.GetTokenRequest {
128
143
Host : wsinfo .GitpodApi .Host ,
129
144
Kind : "gitpod" ,
130
145
Scope : []string {
131
146
"function:getWorkspaceEnvVars" ,
132
147
"function:setEnvVar" ,
133
148
"function:deleteEnvVar" ,
134
- "resource:envVar::" + repositoryPattern + "::create/get/update/delete" ,
149
+ "resource:envVar::" + repositoryPattern + "::" + operations ,
135
150
},
136
151
})
137
- if err != nil {
138
- // TODO remove then GetWorkspaceEnvVars is deployed
139
- clientToken , err = supervisorClient .Token .GetToken (ctx , & supervisorapi.GetTokenRequest {
140
- Host : wsinfo .GitpodApi .Host ,
141
- Kind : "gitpod" ,
142
- Scope : []string {
143
- "function:getEnvVars" , // TODO remove then getWorkspaceEnvVars is deployed
144
- "function:setEnvVar" ,
145
- "function:deleteEnvVar" ,
146
- "resource:envVar::" + repositoryPattern + "::create/get/update/delete" ,
147
- },
148
- })
149
- useDeprecatedGetEnvVar = true
150
- }
151
152
if err != nil {
152
153
return nil , xerrors .Errorf ("failed getting token from supervisor: %w" , err )
153
154
}
@@ -165,7 +166,7 @@ func connectToServer(ctx context.Context, options *connectToServerOptions) (*con
165
166
if err != nil {
166
167
return nil , xerrors .Errorf ("failed connecting to server: %w" , err )
167
168
}
168
- return & connectToServerResult {repositoryPattern , wsinfo , client , useDeprecatedGetEnvVar }, nil
169
+ return & connectToServerResult {repositoryPattern , wsinfo , client }, nil
169
170
}
170
171
171
172
func getWorkspaceEnvs (ctx context.Context , options * connectToServerOptions ) ([]* serverapi.EnvVar , error ) {
@@ -175,10 +176,7 @@ func getWorkspaceEnvs(ctx context.Context, options *connectToServerOptions) ([]*
175
176
}
176
177
defer result .client .Close ()
177
178
178
- if ! result .useDeprecatedGetEnvVar {
179
- return result .client .GetWorkspaceEnvVars (ctx , result .wsInfo .WorkspaceId )
180
- }
181
- return result .client .GetEnvVars (ctx )
179
+ return result .client .GetWorkspaceEnvVars (ctx , result .wsInfo .WorkspaceId )
182
180
}
183
181
184
182
func getEnvs (ctx context.Context ) error {
@@ -194,8 +192,11 @@ func getEnvs(ctx context.Context) error {
194
192
return nil
195
193
}
196
194
197
- func setEnvs (ctx context.Context , args []string ) error {
198
- result , err := connectToServer (ctx , nil )
195
+ func setEnvs (ctx context.Context , scopeUser bool , args []string ) error {
196
+ options := connectToServerOptions {
197
+ setEnvScopeUser : scopeUser ,
198
+ }
199
+ result , err := connectToServer (ctx , & options )
199
200
if err != nil {
200
201
return err
201
202
}
@@ -291,4 +292,5 @@ func init() {
291
292
292
293
envCmd .Flags ().BoolVarP (& exportEnvs , "export" , "e" , false , "produce a script that can be eval'ed in Bash" )
293
294
envCmd .Flags ().BoolVarP (& unsetEnvs , "unset" , "u" , false , "deletes/unsets persisted environment variables" )
295
+ envCmd .Flags ().StringVarP (& scope , "scope" , "s" , "repo" , "deletes/unsets persisted environment variables" )
294
296
}
0 commit comments