@@ -17,12 +17,10 @@ import (
17
17
"github.com/spf13/cobra"
18
18
"golang.org/x/sync/errgroup"
19
19
"golang.org/x/xerrors"
20
- "google.golang.org/grpc"
21
- "google.golang.org/grpc/credentials/insecure"
22
20
23
- "github.com/gitpod-io/gitpod/common-go/util "
21
+ "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor "
24
22
serverapi "github.com/gitpod-io/gitpod/gitpod-protocol"
25
- supervisor "github.com/gitpod-io/gitpod/supervisor/api"
23
+ supervisorapi "github.com/gitpod-io/gitpod/supervisor/api"
26
24
)
27
25
28
26
var exportEnvs = false
@@ -31,8 +29,8 @@ var unsetEnvs = false
31
29
// envCmd represents the env command
32
30
var envCmd = & cobra.Command {
33
31
Use : "env" ,
34
- Short : "Controls user-defined, persistent environment variables." ,
35
- Long : `This command can print and modify the persistent environment variables associated with your user, for this repository .
32
+ Short : "Controls workspace environment variables." ,
33
+ Long : `This command can print and modify the persistent environment variables associated with your workspace .
36
34
37
35
To set the persistent environment variable 'foo' to the value 'bar' use:
38
36
gp env foo=bar
@@ -78,15 +76,20 @@ delete environment variables with a repository pattern of */foo, foo/* or */*.
78
76
79
77
type connectToServerResult struct {
80
78
repositoryPattern string
79
+ wsInfo * supervisorapi.WorkspaceInfoResponse
81
80
client * serverapi.APIoverJSONRPC
81
+
82
+ useDeprecatedGetEnvVar bool
82
83
}
83
84
84
85
func connectToServer (ctx context.Context ) (* connectToServerResult , error ) {
85
- supervisorConn , err := grpc . Dial ( util . GetSupervisorAddress (), grpc . WithTransportCredentials ( insecure . NewCredentials ()) )
86
+ supervisorClient , err := supervisor . New ( ctx )
86
87
if err != nil {
87
88
return nil , xerrors .Errorf ("failed connecting to supervisor: %w" , err )
88
89
}
89
- wsinfo , err := supervisor .NewInfoServiceClient (supervisorConn ).WorkspaceInfo (ctx , & supervisor.WorkspaceInfoRequest {})
90
+ defer supervisorClient .Close ()
91
+
92
+ wsinfo , err := supervisorClient .Info .WorkspaceInfo (ctx , & supervisorapi.WorkspaceInfoRequest {})
90
93
if err != nil {
91
94
return nil , xerrors .Errorf ("failed getting workspace info from supervisor: %w" , err )
92
95
}
@@ -100,16 +103,32 @@ func connectToServer(ctx context.Context) (*connectToServerResult, error) {
100
103
return nil , xerrors .New ("repository info is missing name" )
101
104
}
102
105
repositoryPattern := wsinfo .Repository .Owner + "/" + wsinfo .Repository .Name
103
- clientToken , err := supervisor .NewTokenServiceClient (supervisorConn ).GetToken (ctx , & supervisor.GetTokenRequest {
106
+
107
+ var useDeprecatedGetEnvVar bool
108
+ clientToken , err := supervisorClient .Token .GetToken (ctx , & supervisorapi.GetTokenRequest {
104
109
Host : wsinfo .GitpodApi .Host ,
105
110
Kind : "gitpod" ,
106
111
Scope : []string {
107
- "function:getEnvVars " ,
112
+ "function:getWorkspaceEnvVars " ,
108
113
"function:setEnvVar" ,
109
114
"function:deleteEnvVar" ,
110
115
"resource:envVar::" + repositoryPattern + "::create/get/update/delete" ,
111
116
},
112
117
})
118
+ if err != nil {
119
+ // TODO remove then GetWorkspaceEnvVars is deployed
120
+ clientToken , err = supervisorClient .Token .GetToken (ctx , & supervisorapi.GetTokenRequest {
121
+ Host : wsinfo .GitpodApi .Host ,
122
+ Kind : "gitpod" ,
123
+ Scope : []string {
124
+ "function:getEnvVars" , // TODO remove then getWorkspaceEnvVars is deployed
125
+ "function:setEnvVar" ,
126
+ "function:deleteEnvVar" ,
127
+ "resource:envVar::" + repositoryPattern + "::create/get/update/delete" ,
128
+ },
129
+ })
130
+ useDeprecatedGetEnvVar = true
131
+ }
113
132
if err != nil {
114
133
return nil , xerrors .Errorf ("failed getting token from supervisor: %w" , err )
115
134
}
@@ -121,7 +140,7 @@ func connectToServer(ctx context.Context) (*connectToServerResult, error) {
121
140
if err != nil {
122
141
return nil , xerrors .Errorf ("failed connecting to server: %w" , err )
123
142
}
124
- return & connectToServerResult {repositoryPattern , client }, nil
143
+ return & connectToServerResult {repositoryPattern , wsinfo , client , useDeprecatedGetEnvVar }, nil
125
144
}
126
145
127
146
func getEnvs (ctx context.Context ) error {
@@ -131,13 +150,18 @@ func getEnvs(ctx context.Context) error {
131
150
}
132
151
defer result .client .Close ()
133
152
134
- vars , err := result .client .GetEnvVars (ctx )
153
+ var vars []* serverapi.EnvVar
154
+ if ! result .useDeprecatedGetEnvVar {
155
+ vars , err = result .client .GetWorkspaceEnvVars (ctx , result .wsInfo .WorkspaceId )
156
+ } else {
157
+ vars , err = result .client .GetEnvVars (ctx )
158
+ }
135
159
if err != nil {
136
160
return xerrors .Errorf ("failed to fetch env vars from server: %w" , err )
137
161
}
138
162
139
163
for _ , v := range vars {
140
- printVar (v , exportEnvs )
164
+ printVar (v . Name , v . Value , exportEnvs )
141
165
}
142
166
143
167
return nil
@@ -163,7 +187,7 @@ func setEnvs(ctx context.Context, args []string) error {
163
187
if err != nil {
164
188
return err
165
189
}
166
- printVar (v , exportEnvs )
190
+ printVar (v . Name , v . Value , exportEnvs )
167
191
return nil
168
192
})
169
193
}
@@ -189,12 +213,12 @@ func deleteEnvs(ctx context.Context, args []string) error {
189
213
return g .Wait ()
190
214
}
191
215
192
- func printVar (v * serverapi. UserEnvVarValue , export bool ) {
193
- val := strings .Replace (v . Value , "\" " , "\\ \" " , - 1 )
216
+ func printVar (name string , value string , export bool ) {
217
+ val := strings .Replace (value , "\" " , "\\ \" " , - 1 )
194
218
if export {
195
- fmt .Printf ("export %s=\" %s\" \n " , v . Name , val )
219
+ fmt .Printf ("export %s=\" %s\" \n " , name , val )
196
220
} else {
197
- fmt .Printf ("%s=%s\n " , v . Name , val )
221
+ fmt .Printf ("%s=%s\n " , name , val )
198
222
}
199
223
}
200
224
0 commit comments