diff --git a/Makefile b/Makefile index 59b3ff0d..24ef8d51 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION=v0.1.24 +VERSION=v0.1.25 OUT_DIR=dist YEAR?=$(shell date +"%Y") diff --git a/docs/releases/release_notes.md b/docs/releases/release_notes.md index 53b98b18..fcc61181 100644 --- a/docs/releases/release_notes.md +++ b/docs/releases/release_notes.md @@ -23,7 +23,7 @@ cf version ```bash # download and extract the binary -curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.1.24/cf-linux-amd64.tar.gz | tar zx +curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.1.25/cf-linux-amd64.tar.gz | tar zx # move the binary to your $PATH mv ./cf-linux-amd64 /usr/local/bin/cf @@ -36,7 +36,7 @@ cf version ```bash # download and extract the binary -curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.1.24/cf-darwin-amd64.tar.gz | tar zx +curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.1.25/cf-darwin-amd64.tar.gz | tar zx # move the binary to your $PATH mv ./cf-darwin-amd64 /usr/local/bin/cf diff --git a/pkg/config/config.go b/pkg/config/config.go index 62de6223..e9764c05 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -23,7 +23,7 @@ import ( "net/http" "os" "path/filepath" - "sync" + "sort" "time" "github.com/codefresh-io/cli-v2/pkg/log" @@ -66,6 +66,13 @@ type ( DefaultRuntime string `mapstructure:"defaultRuntime" json:"defaultRuntime"` config *Config } + + authContextWithStatus struct { + AuthContext + current bool + status string + account string + } ) // Errors @@ -248,7 +255,6 @@ func (c *Config) clientForContext(ctx *AuthContext) codefresh.Codefresh { func (c *Config) Write(ctx context.Context, w io.Writer) error { tb := ansiterm.NewTabWriter(w, 0, 0, 4, ' ', 0) - writerLock := sync.Mutex{} ar := util.NewAsyncRunner(len(c.Contexts)) _, err := fmt.Fprintln(tb, "CURRENT\tNAME\tURL\tACCOUNT\tSTATUS") @@ -256,43 +262,39 @@ func (c *Config) Write(ctx context.Context, w io.Writer) error { return err } - for name, context := range c.Contexts { + contexts := make([]*authContextWithStatus, 0, len(c.Contexts)) + for _, context := range c.Contexts { + contexts = append(contexts, &authContextWithStatus{ + AuthContext: *context, + }) + } + + sort.SliceStable(contexts, func(i, j int) bool { + return contexts[i].Name < contexts[j].Name + }) + + for _, context := range contexts { // capture local variables for closure - name := name context := context ar.Run(func() error { - status := "VALID" - accName := "" - current := "" + context.status = "VALID" usr, err := context.GetUser(ctx) if err != nil { if ctx.Err() != nil { // context canceled return ctx.Err() } - status = err.Error() + context.status = err.Error() } else { - accName = usr.GetActiveAccount().Name + context.account = usr.GetActiveAccount().Name } - if name == c.CurrentContext { - current = greenStar + if context.Name == c.CurrentContext { + context.current = true } - writerLock.Lock() - _, err = fmt.Fprintf(tb, "%s\t%s\t%s\t%s\t%s\n", - current, - name, - context.URL, - accName, - status, - ) - writerLock.Unlock() - if err != nil { - return err - } return nil }) } @@ -301,6 +303,24 @@ func (c *Config) Write(ctx context.Context, w io.Writer) error { return err } + for _, context := range contexts { + current := "" + if context.current { + current = greenStar + } + + _, err = fmt.Fprintf(tb, "%s\t%s\t%s\t%s\t%s\n", + current, + context.Name, + context.URL, + context.account, + context.status, + ) + if err != nil { + return err + } + } + return tb.Flush() }