5
5
package cmd
6
6
7
7
import (
8
+ "context"
9
+ "errors"
8
10
"fmt"
9
11
"os"
10
12
"strings"
13
+ "time"
11
14
12
15
"github.com/spf13/cobra"
16
+ "google.golang.org/grpc"
13
17
14
18
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/theialib"
19
+ serverapi "github.com/gitpod-io/gitpod/gitpod-protocol"
20
+ supervisor "github.com/gitpod-io/gitpod/supervisor/api"
15
21
)
16
22
17
23
var exportEnvs = false
@@ -42,79 +48,155 @@ delete environment variables with a repository pattern of */foo, foo/* or */*.
42
48
` ,
43
49
Args : cobra .ArbitraryArgs ,
44
50
Run : func (cmd * cobra.Command , args []string ) {
45
- fail := func (msg string ) {
46
- fmt .Fprintln (os .Stderr , msg )
47
- os .Exit (- 1 )
51
+ if len (args ) > 0 {
52
+ if unsetEnvs {
53
+ deleteEnvs (args )
54
+ return
55
+ }
56
+
57
+ setEnvs (args )
58
+ } else {
59
+ getEnvs ()
48
60
}
61
+ },
62
+ }
49
63
50
- service , err := theialib .NewServiceFromEnv ()
64
+ func getEnvs () {
65
+ if ! isTheiaIDE () {
66
+ ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Minute )
67
+ defer cancel ()
68
+ supervisorAddr := os .Getenv ("SUPERVISOR_ADDR" )
69
+ if supervisorAddr == "" {
70
+ supervisorAddr = "localhost:22999"
71
+ }
72
+ supervisorConn , err := grpc .Dial (supervisorAddr , grpc .WithInsecure ())
51
73
if err != nil {
52
- fail (err .Error ())
74
+ fail ("failed connecting to supervisor: " + err .Error ())
75
+ }
76
+ wsinfo , err := supervisor .NewInfoServiceClient (supervisorConn ).WorkspaceInfo (ctx , & supervisor.WorkspaceInfoRequest {})
77
+ if err != nil {
78
+ fail ("failed getting workspace info from supervisor: " + err .Error ())
79
+ }
80
+ if wsinfo .Repository == nil {
81
+ fail ("workspace info is missing repository" )
82
+ }
83
+ if wsinfo .Repository .Owner == "" {
84
+ fail ("repository info is missing owner" )
85
+ }
86
+ if wsinfo .Repository .Name == "" {
87
+ fail ("repository info is missing name" )
53
88
}
54
89
55
- setEnvs := func () {
56
- vars := make ([]theialib.EnvironmentVariable , len (args ))
57
- for i , arg := range args {
58
- kv := strings .Split (arg , "=" )
59
- if len (kv ) != 2 {
60
- fail (fmt .Sprintf ("%s has no value (correct format is %s=some_value)" , arg , arg ))
61
- }
62
-
63
- key := strings .TrimSpace (kv [0 ])
64
- if key == "" {
65
- fail (fmt .Sprintf ("variable must have a name" ))
66
- }
67
- // Do not trim value - the user might want whitespace here
68
- // Also do not check if the value is empty, as an empty value means we want to delete the variable
69
- val := kv [1 ]
70
- if val == "" {
71
- fail (fmt .Sprintf ("variable must have a value; use -u to unset a variable" ))
72
- }
73
-
74
- vars [i ] = theialib.EnvironmentVariable {Name : key , Value : val }
75
- }
76
-
77
- _ , err = service .SetEnvVar (theialib.SetEnvvarRequest {Variables : vars })
78
- if err != nil {
79
- fail (fmt .Sprintf ("cannot set environment variables: %v" , err ))
80
- }
90
+ clientToken , err := supervisor .NewTokenServiceClient (supervisorConn ).GetToken (ctx , & supervisor.GetTokenRequest {
91
+ Host : wsinfo .GitpodApi .Host ,
92
+ Kind : "gitpod" ,
93
+ Scope : []string {
94
+ "function:getEnvVars" ,
95
+ "resource:envVar::" + wsinfo .Repository .Owner + "/" + wsinfo .Repository .Name + "::get" ,
96
+ },
97
+ })
98
+ if err != nil {
99
+ fail ("failed getting token from supervisor: " + err .Error ())
100
+ }
101
+ client , err := serverapi .ConnectToServer (wsinfo .GitpodApi .Endpoint , serverapi.ConnectToServerOpts {Token : clientToken .Token , Context : ctx })
102
+ if err != nil {
103
+ fail ("failed connecting to server: " + err .Error ())
104
+ }
105
+ vars , err := client .GetEnvVars (ctx )
106
+ if err != nil {
107
+ fail ("failed to fetch env vars from server: " + err .Error ())
108
+ }
81
109
82
- for _ , v := range vars {
83
- printVar (v , exportEnvs )
110
+ for _ , v := range vars {
111
+ val := strings .Replace (v .Value , "\" " , "\\ \" " , - 1 )
112
+ if exportEnvs {
113
+ fmt .Printf ("export %s=\" %s\" \n " , v .Name , val )
114
+ } else {
115
+ fmt .Printf ("%s=%s\n " , v .Name , val )
84
116
}
85
117
}
86
- getEnvs := func () {
87
- vars , err := service .GetEnvVars (theialib.GetEnvvarsRequest {})
88
- if err != nil {
89
- fail (fmt .Sprintf ("cannot get environment variables: %v" , err ))
90
- }
118
+ return
119
+ }
91
120
92
- for _ , v := range vars .Variables {
93
- printVar (v , exportEnvs )
94
- }
121
+ service , err := theialib .NewServiceFromEnv ()
122
+ if err != nil {
123
+ fail (err .Error ())
124
+ }
125
+
126
+ vars , err := service .GetEnvVars (theialib.GetEnvvarsRequest {})
127
+ if err != nil {
128
+ fail (fmt .Sprintf ("cannot get environment variables: %v" , err ))
129
+ }
130
+
131
+ for _ , v := range vars .Variables {
132
+ printVar (v , exportEnvs )
133
+ }
134
+ }
135
+
136
+ func setEnvs (args []string ) {
137
+ if ! isTheiaIDE () {
138
+ fail ("not supported" )
139
+ }
140
+
141
+ service , err := theialib .NewServiceFromEnv ()
142
+ if err != nil {
143
+ fail (err .Error ())
144
+ }
145
+
146
+ vars := make ([]theialib.EnvironmentVariable , len (args ))
147
+ for i , arg := range args {
148
+ kv := strings .Split (arg , "=" )
149
+ if len (kv ) != 2 {
150
+ fail (fmt .Sprintf ("%s has no value (correct format is %s=some_value)" , arg , arg ))
95
151
}
96
- doUnsetEnvs := func () {
97
- resp , err := service .DeleteEnvVar (theialib.DeleteEnvvarRequest {Variables : args })
98
- if err != nil {
99
- fail (fmt .Sprintf ("cannot unset environment variables: %v" , err ))
100
- }
101
152
102
- if len (resp .NotDeleted ) != 0 {
103
- fail (fmt .Sprintf ("cannot unset environment variables: %s" , strings .Join (resp .NotDeleted , ", " )))
104
- }
153
+ key := strings .TrimSpace (kv [0 ])
154
+ if key == "" {
155
+ fail (fmt .Sprintf ("variable must have a name" ))
156
+ }
157
+ // Do not trim value - the user might want whitespace here
158
+ // Also do not check if the value is empty, as an empty value means we want to delete the variable
159
+ val := kv [1 ]
160
+ if val == "" {
161
+ fail (fmt .Sprintf ("variable must have a value; use -u to unset a variable" ))
105
162
}
106
163
107
- if len (args ) > 0 {
108
- if unsetEnvs {
109
- doUnsetEnvs ()
110
- return
111
- }
164
+ vars [i ] = theialib.EnvironmentVariable {Name : key , Value : val }
165
+ }
112
166
113
- setEnvs ()
114
- } else {
115
- getEnvs ()
116
- }
117
- },
167
+ _ , err = service .SetEnvVar (theialib.SetEnvvarRequest {Variables : vars })
168
+ if err != nil {
169
+ fail (fmt .Sprintf ("cannot set environment variables: %v" , err ))
170
+ }
171
+
172
+ for _ , v := range vars {
173
+ printVar (v , exportEnvs )
174
+ }
175
+ }
176
+
177
+ func deleteEnvs (args []string ) {
178
+ if ! isTheiaIDE () {
179
+ fail ("not supported" )
180
+ }
181
+
182
+ service , err := theialib .NewServiceFromEnv ()
183
+ if err != nil {
184
+ fail (err .Error ())
185
+ }
186
+
187
+ resp , err := service .DeleteEnvVar (theialib.DeleteEnvvarRequest {Variables : args })
188
+ if err != nil {
189
+ fail (fmt .Sprintf ("cannot unset environment variables: %v" , err ))
190
+ }
191
+
192
+ if len (resp .NotDeleted ) != 0 {
193
+ fail (fmt .Sprintf ("cannot unset environment variables: %s" , strings .Join (resp .NotDeleted , ", " )))
194
+ }
195
+ }
196
+
197
+ func fail (msg string ) {
198
+ fmt .Fprintln (os .Stderr , msg )
199
+ os .Exit (- 1 )
118
200
}
119
201
120
202
func printVar (v theialib.EnvironmentVariable , export bool ) {
@@ -132,3 +214,8 @@ func init() {
132
214
envCmd .Flags ().BoolVarP (& exportEnvs , "export" , "e" , false , "produce a script that can be eval'ed in Bash" )
133
215
envCmd .Flags ().BoolVarP (& unsetEnvs , "unset" , "u" , false , "deletes/unsets persisted environment variables" )
134
216
}
217
+
218
+ func isTheiaIDE () bool {
219
+ stat , err := os .Stat ("/theia" )
220
+ return ! errors .Is (os .ErrNotExist , err ) && stat != nil && stat .IsDir ()
221
+ }
0 commit comments