@@ -17,12 +17,10 @@ import (
1717 "github.com/spf13/cobra"
1818 "golang.org/x/sync/errgroup"
1919 "golang.org/x/xerrors"
20- "google.golang.org/grpc"
21- "google.golang.org/grpc/credentials/insecure"
2220
23- "github.com/gitpod-io/gitpod/common-go/util "
21+ "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor "
2422 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"
2624)
2725
2826var exportEnvs = false
@@ -31,8 +29,8 @@ var unsetEnvs = false
3129// envCmd represents the env command
3230var envCmd = & cobra.Command {
3331 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 .
3634
3735To set the persistent environment variable 'foo' to the value 'bar' use:
3836 gp env foo=bar
@@ -78,15 +76,20 @@ delete environment variables with a repository pattern of */foo, foo/* or */*.
7876
7977type connectToServerResult struct {
8078 repositoryPattern string
79+ wsInfo * supervisorapi.WorkspaceInfoResponse
8180 client * serverapi.APIoverJSONRPC
81+
82+ useDeprecatedGetEnvVar bool
8283}
8384
8485func connectToServer (ctx context.Context ) (* connectToServerResult , error ) {
85- supervisorConn , err := grpc . Dial ( util . GetSupervisorAddress (), grpc . WithTransportCredentials ( insecure . NewCredentials ()) )
86+ supervisorClient , err := supervisor . New ( ctx )
8687 if err != nil {
8788 return nil , xerrors .Errorf ("failed connecting to supervisor: %w" , err )
8889 }
89- wsinfo , err := supervisor .NewInfoServiceClient (supervisorConn ).WorkspaceInfo (ctx , & supervisor.WorkspaceInfoRequest {})
90+ defer supervisorClient .Close ()
91+
92+ wsinfo , err := supervisorClient .Info .WorkspaceInfo (ctx , & supervisorapi.WorkspaceInfoRequest {})
9093 if err != nil {
9194 return nil , xerrors .Errorf ("failed getting workspace info from supervisor: %w" , err )
9295 }
@@ -100,16 +103,32 @@ func connectToServer(ctx context.Context) (*connectToServerResult, error) {
100103 return nil , xerrors .New ("repository info is missing name" )
101104 }
102105 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 {
104109 Host : wsinfo .GitpodApi .Host ,
105110 Kind : "gitpod" ,
106111 Scope : []string {
107- "function:getEnvVars " ,
112+ "function:getWorkspaceEnvVars " ,
108113 "function:setEnvVar" ,
109114 "function:deleteEnvVar" ,
110115 "resource:envVar::" + repositoryPattern + "::create/get/update/delete" ,
111116 },
112117 })
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+ }
113132 if err != nil {
114133 return nil , xerrors .Errorf ("failed getting token from supervisor: %w" , err )
115134 }
@@ -121,7 +140,7 @@ func connectToServer(ctx context.Context) (*connectToServerResult, error) {
121140 if err != nil {
122141 return nil , xerrors .Errorf ("failed connecting to server: %w" , err )
123142 }
124- return & connectToServerResult {repositoryPattern , client }, nil
143+ return & connectToServerResult {repositoryPattern , wsinfo , client , useDeprecatedGetEnvVar }, nil
125144}
126145
127146func getEnvs (ctx context.Context ) error {
@@ -131,13 +150,18 @@ func getEnvs(ctx context.Context) error {
131150 }
132151 defer result .client .Close ()
133152
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+ }
135159 if err != nil {
136160 return xerrors .Errorf ("failed to fetch env vars from server: %w" , err )
137161 }
138162
139163 for _ , v := range vars {
140- printVar (v , exportEnvs )
164+ printVar (v . Name , v . Value , exportEnvs )
141165 }
142166
143167 return nil
@@ -163,7 +187,7 @@ func setEnvs(ctx context.Context, args []string) error {
163187 if err != nil {
164188 return err
165189 }
166- printVar (v , exportEnvs )
190+ printVar (v . Name , v . Value , exportEnvs )
167191 return nil
168192 })
169193 }
@@ -189,12 +213,12 @@ func deleteEnvs(ctx context.Context, args []string) error {
189213 return g .Wait ()
190214}
191215
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 )
194218 if export {
195- fmt .Printf ("export %s=\" %s\" \n " , v . Name , val )
219+ fmt .Printf ("export %s=\" %s\" \n " , name , val )
196220 } else {
197- fmt .Printf ("%s=%s\n " , v . Name , val )
221+ fmt .Printf ("%s=%s\n " , name , val )
198222 }
199223}
200224
0 commit comments