@@ -8,12 +8,14 @@ import (
8
8
"context"
9
9
"fmt"
10
10
"io"
11
+ "net/http"
11
12
"os"
12
13
"strings"
13
14
"sync"
14
15
"time"
15
16
16
17
log "github.com/sirupsen/logrus"
18
+ "github.com/sourcegraph/jsonrpc2"
17
19
"github.com/spf13/cobra"
18
20
"golang.org/x/sync/errgroup"
19
21
"golang.org/x/xerrors"
36
38
envScopeUser envScope = "user"
37
39
)
38
40
41
+ func envScopeFromString (s string ) envScope {
42
+ switch s {
43
+ case string (envScopeRepo ):
44
+ return envScopeRepo
45
+ case string (envScopeUser ):
46
+ return envScopeUser
47
+ default :
48
+ return envScopeRepo
49
+ }
50
+ }
51
+
39
52
// envCmd represents the env command
40
53
var envCmd = & cobra.Command {
41
54
Use : "env" ,
@@ -75,8 +88,8 @@ delete environment variables with a repository pattern of */foo, foo/* or */*.
75
88
if unsetEnvs {
76
89
err = deleteEnvs (ctx , args )
77
90
} else {
78
- scopeUser := scope == string ( envScopeUser )
79
- err = setEnvs (ctx , scopeUser , args )
91
+ setEnvScope := envScopeFromString ( scope )
92
+ err = setEnvs (ctx , setEnvScope , args )
80
93
}
81
94
} else {
82
95
err = getEnvs (ctx )
@@ -89,14 +102,15 @@ type connectToServerResult struct {
89
102
repositoryPattern string
90
103
wsInfo * supervisorapi.WorkspaceInfoResponse
91
104
client * serverapi.APIoverJSONRPC
105
+ gitpodHost string
92
106
}
93
107
94
108
type connectToServerOptions struct {
95
109
supervisorClient * supervisor.SupervisorClient
96
110
wsInfo * api.WorkspaceInfoResponse
97
111
log * log.Entry
98
112
99
- setEnvScopeUser bool
113
+ setEnvScope envScope
100
114
}
101
115
102
116
func connectToServer (ctx context.Context , options * connectToServerOptions ) (* connectToServerResult , error ) {
@@ -133,7 +147,7 @@ func connectToServer(ctx context.Context, options *connectToServerOptions) (*con
133
147
repositoryPattern := wsinfo .Repository .Owner + "/" + wsinfo .Repository .Name
134
148
135
149
operations := "create/get/update/delete"
136
- if options != nil && options .setEnvScopeUser {
150
+ if options != nil && options .setEnvScope == envScopeUser {
137
151
// Updating user env vars requires a different token with a special scope
138
152
repositoryPattern = "*/*"
139
153
operations = "update"
@@ -166,7 +180,7 @@ func connectToServer(ctx context.Context, options *connectToServerOptions) (*con
166
180
if err != nil {
167
181
return nil , xerrors .Errorf ("failed connecting to server: %w" , err )
168
182
}
169
- return & connectToServerResult {repositoryPattern , wsinfo , client }, nil
183
+ return & connectToServerResult {repositoryPattern , wsinfo , client , wsinfo . GitpodHost }, nil
170
184
}
171
185
172
186
func getWorkspaceEnvs (ctx context.Context , options * connectToServerOptions ) ([]* serverapi.EnvVar , error ) {
@@ -192,9 +206,9 @@ func getEnvs(ctx context.Context) error {
192
206
return nil
193
207
}
194
208
195
- func setEnvs (ctx context.Context , scopeUser bool , args []string ) error {
209
+ func setEnvs (ctx context.Context , setEnvScope envScope , args []string ) error {
196
210
options := connectToServerOptions {
197
- setEnvScopeUser : scopeUser ,
211
+ setEnvScope : setEnvScope ,
198
212
}
199
213
result , err := connectToServer (ctx , & options )
200
214
if err != nil {
@@ -213,6 +227,11 @@ func setEnvs(ctx context.Context, scopeUser bool, args []string) error {
213
227
g .Go (func () error {
214
228
err = result .client .SetEnvVar (ctx , v )
215
229
if err != nil {
230
+ if ferr , ok := err .(* jsonrpc2.Error ); ok && ferr .Code == http .StatusForbidden && setEnvScope == envScopeUser {
231
+ return fmt .Errorf ("" +
232
+ "Can't automatically create env var `%s` for security reasons.\n " +
233
+ "Please create the var manually under %s/user/variables using Name=%s, Scope=*/*, Value=foobar" , v .Name , result .gitpodHost , v .Name )
234
+ }
216
235
return err
217
236
}
218
237
printVar (v .Name , v .Value , exportEnvs )
0 commit comments