Skip to content

Commit ee115de

Browse files
authored
Sort-contexts (#663)
* sort contexts before printing to stdout * bumped version to 0.1.25
1 parent 6b30fa1 commit ee115de

File tree

3 files changed

+46
-26
lines changed

3 files changed

+46
-26
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=v0.1.24
1+
VERSION=v0.1.25
22

33
OUT_DIR=dist
44
YEAR?=$(shell date +"%Y")

docs/releases/release_notes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ cf version
2323

2424
```bash
2525
# download and extract the binary
26-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.1.24/cf-linux-amd64.tar.gz | tar zx
26+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.1.25/cf-linux-amd64.tar.gz | tar zx
2727

2828
# move the binary to your $PATH
2929
mv ./cf-linux-amd64 /usr/local/bin/cf
@@ -36,7 +36,7 @@ cf version
3636

3737
```bash
3838
# download and extract the binary
39-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.1.24/cf-darwin-amd64.tar.gz | tar zx
39+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.1.25/cf-darwin-amd64.tar.gz | tar zx
4040

4141
# move the binary to your $PATH
4242
mv ./cf-darwin-amd64 /usr/local/bin/cf

pkg/config/config.go

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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

249256
func (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\tNAME\tURL\tACCOUNT\tSTATUS")
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

Comments
 (0)