Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort-contexts #663

Merged
merged 3 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=v0.1.24
VERSION=v0.1.25

OUT_DIR=dist
YEAR?=$(shell date +"%Y")
Expand Down
4 changes: 2 additions & 2 deletions docs/releases/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
66 changes: 43 additions & 23 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"net/http"
"os"
"path/filepath"
"sync"
"sort"
"time"

"github.com/codefresh-io/cli-v2/pkg/log"
Expand Down Expand Up @@ -66,6 +66,13 @@ type (
DefaultRuntime string `mapstructure:"defaultRuntime" json:"defaultRuntime"`
config *Config
}

authContextWithStatus struct {
AuthContext
current bool
status string
account string
}
)

// Errors
Expand Down Expand Up @@ -248,51 +255,46 @@ 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")
if err != nil {
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
})
}
Expand All @@ -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()
}

Expand Down