15
15
package util
16
16
17
17
import (
18
+ "bytes"
18
19
"context"
19
20
"fmt"
21
+ "github.com/pkg/browser"
22
+ "net/url"
20
23
"os"
21
24
"os/signal"
22
25
"regexp"
@@ -29,6 +32,7 @@ import (
29
32
"github.com/codefresh-io/cli-v2/pkg/log"
30
33
"github.com/codefresh-io/cli-v2/pkg/reporter"
31
34
"github.com/codefresh-io/cli-v2/pkg/store"
35
+ "github.com/codefresh-io/go-sdk/pkg/codefresh"
32
36
33
37
"k8s.io/client-go/tools/clientcmd"
34
38
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
@@ -171,7 +175,7 @@ func kubeConfig() *clientcmdapi.Config {
171
175
}
172
176
173
177
type kubeContext struct {
174
- Name string
178
+ Name string
175
179
Current bool
176
180
}
177
181
@@ -181,7 +185,7 @@ func KubeContexts() []kubeContext {
181
185
i := 0
182
186
for key := range conf .Contexts {
183
187
contexts [i ] = kubeContext {
184
- Name : key ,
188
+ Name : key ,
185
189
Current : key == conf .CurrentContext ,
186
190
}
187
191
i += 1
@@ -218,6 +222,39 @@ func KubeCurrentServer() (string, error) {
218
222
return KubeServerByContextName ("" )
219
223
}
220
224
225
+ func CurrentAccount (user * codefresh.User ) (string , error ) {
226
+ for i := range user .Accounts {
227
+ if user .Accounts [i ].Name == user .ActiveAccountName {
228
+ return user .Accounts [i ].ID , nil
229
+ }
230
+ }
231
+ return "" , fmt .Errorf ("account id for \" %s\" not found" , user .ActiveAccountName )
232
+ }
233
+
234
+ func OpenBrowserForGitLogin (ingressHost string , user string , account string ) error {
235
+ var b bytes.Buffer
236
+ if ! strings .HasPrefix (ingressHost , "http" ) {
237
+ b .WriteString ("https://" )
238
+ }
239
+ b .WriteString (ingressHost )
240
+ b .WriteString ("/app-proxy/api/git-auth/github?userId=" + user + "&accountId=" + account )
241
+
242
+ url , err := url .Parse (b .String ())
243
+ if err != nil {
244
+ return err
245
+ }
246
+
247
+ err = browser .OpenURL (url .String ())
248
+ if err != nil {
249
+ return err
250
+ }
251
+
252
+ fmt .Println ("Follow instructions in web browser" )
253
+ time .Sleep (2 * time .Second )
254
+
255
+ return nil
256
+ }
257
+
221
258
func KubeContextNameByServer (server string ) (string , error ) {
222
259
conf := kubeConfig ()
223
260
for contextName , context := range conf .Contexts {
0 commit comments