@@ -23,7 +23,7 @@ import (
23
23
"net/http"
24
24
"os"
25
25
"path/filepath"
26
- "sync "
26
+ "sort "
27
27
"time"
28
28
29
29
"github.com/codefresh-io/cli-v2/pkg/log"
@@ -66,6 +66,13 @@ type (
66
66
DefaultRuntime string `mapstructure:"defaultRuntime" json:"defaultRuntime"`
67
67
config * Config
68
68
}
69
+
70
+ authContextWithStatus struct {
71
+ AuthContext
72
+ current bool
73
+ status string
74
+ account string
75
+ }
69
76
)
70
77
71
78
// Errors
@@ -248,51 +255,46 @@ func (c *Config) clientForContext(ctx *AuthContext) codefresh.Codefresh {
248
255
249
256
func (c * Config ) Write (ctx context.Context , w io.Writer ) error {
250
257
tb := ansiterm .NewTabWriter (w , 0 , 0 , 4 , ' ' , 0 )
251
- writerLock := sync.Mutex {}
252
258
ar := util .NewAsyncRunner (len (c .Contexts ))
253
259
254
260
_ , err := fmt .Fprintln (tb , "CURRENT\t NAME\t URL\t ACCOUNT\t STATUS" )
255
261
if err != nil {
256
262
return err
257
263
}
258
264
259
- for name , context := range c .Contexts {
265
+ contexts := make ([]* authContextWithStatus , 0 , len (c .Contexts ))
266
+ for _ , context := range c .Contexts {
267
+ contexts = append (contexts , & authContextWithStatus {
268
+ AuthContext : * context ,
269
+ })
270
+ }
271
+
272
+ sort .SliceStable (contexts , func (i , j int ) bool {
273
+ return contexts [i ].Name < contexts [j ].Name
274
+ })
275
+
276
+ for _ , context := range contexts {
260
277
// capture local variables for closure
261
- name := name
262
278
context := context
263
279
264
280
ar .Run (func () error {
265
- status := "VALID"
266
- accName := ""
267
- current := ""
281
+ context .status = "VALID"
268
282
269
283
usr , err := context .GetUser (ctx )
270
284
if err != nil {
271
285
if ctx .Err () != nil { // context canceled
272
286
return ctx .Err ()
273
287
}
274
- status = err .Error ()
288
+ context . status = err .Error ()
275
289
276
290
} else {
277
- accName = usr .GetActiveAccount ().Name
291
+ context . account = usr .GetActiveAccount ().Name
278
292
}
279
293
280
- if name == c .CurrentContext {
281
- current = greenStar
294
+ if context . Name == c .CurrentContext {
295
+ context . current = true
282
296
}
283
297
284
- writerLock .Lock ()
285
- _ , err = fmt .Fprintf (tb , "%s\t %s\t %s\t %s\t %s\n " ,
286
- current ,
287
- name ,
288
- context .URL ,
289
- accName ,
290
- status ,
291
- )
292
- writerLock .Unlock ()
293
- if err != nil {
294
- return err
295
- }
296
298
return nil
297
299
})
298
300
}
@@ -301,6 +303,24 @@ func (c *Config) Write(ctx context.Context, w io.Writer) error {
301
303
return err
302
304
}
303
305
306
+ for _ , context := range contexts {
307
+ current := ""
308
+ if context .current {
309
+ current = greenStar
310
+ }
311
+
312
+ _ , err = fmt .Fprintf (tb , "%s\t %s\t %s\t %s\t %s\n " ,
313
+ current ,
314
+ context .Name ,
315
+ context .URL ,
316
+ context .account ,
317
+ context .status ,
318
+ )
319
+ if err != nil {
320
+ return err
321
+ }
322
+ }
323
+
304
324
return tb .Flush ()
305
325
}
306
326
0 commit comments