@@ -23,7 +23,7 @@ import (
2323 "net/http"
2424 "os"
2525 "path/filepath"
26- "sync "
26+ "sort "
2727 "time"
2828
2929 "github.com/codefresh-io/cli-v2/pkg/log"
@@ -66,6 +66,13 @@ type (
6666 DefaultRuntime string `mapstructure:"defaultRuntime" json:"defaultRuntime"`
6767 config * Config
6868 }
69+
70+ authContextWithStatus struct {
71+ AuthContext
72+ current bool
73+ status string
74+ account string
75+ }
6976)
7077
7178// Errors
@@ -248,51 +255,46 @@ func (c *Config) clientForContext(ctx *AuthContext) codefresh.Codefresh {
248255
249256func (c * Config ) Write (ctx context.Context , w io.Writer ) error {
250257 tb := ansiterm .NewTabWriter (w , 0 , 0 , 4 , ' ' , 0 )
251- writerLock := sync.Mutex {}
252258 ar := util .NewAsyncRunner (len (c .Contexts ))
253259
254260 _ , err := fmt .Fprintln (tb , "CURRENT\t NAME\t URL\t ACCOUNT\t STATUS" )
255261 if err != nil {
256262 return err
257263 }
258264
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 {
260277 // capture local variables for closure
261- name := name
262278 context := context
263279
264280 ar .Run (func () error {
265- status := "VALID"
266- accName := ""
267- current := ""
281+ context .status = "VALID"
268282
269283 usr , err := context .GetUser (ctx )
270284 if err != nil {
271285 if ctx .Err () != nil { // context canceled
272286 return ctx .Err ()
273287 }
274- status = err .Error ()
288+ context . status = err .Error ()
275289
276290 } else {
277- accName = usr .GetActiveAccount ().Name
291+ context . account = usr .GetActiveAccount ().Name
278292 }
279293
280- if name == c .CurrentContext {
281- current = greenStar
294+ if context . Name == c .CurrentContext {
295+ context . current = true
282296 }
283297
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- }
296298 return nil
297299 })
298300 }
@@ -301,6 +303,24 @@ func (c *Config) Write(ctx context.Context, w io.Writer) error {
301303 return err
302304 }
303305
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+
304324 return tb .Flush ()
305325}
306326
0 commit comments