Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
chore: sort coder envs by name in ssh config
  • Loading branch information
lilshoff committed Mar 2, 2021
commit c20f689c3891b21ba58c30c8c2cbda7fb5b33174
4 changes: 4 additions & 0 deletions internal/cmd/configssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/user"
"path/filepath"
"sort"
"strings"

"cdr.dev/coder-cli/pkg/clog"
Expand Down Expand Up @@ -176,6 +177,9 @@ func writeSSHKey(ctx context.Context, client coder.Client, privateKeyPath string

func makeNewConfigs(userName string, envs []coderutil.EnvWithWorkspaceProvider, privateKeyFilepath string) string {
newConfig := fmt.Sprintf("\n%s\n%s\n\n", sshStartToken, sshStartMessage)

sort.Sort(coderutil.EWPsByEnvName(envs))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personally, I much prefer sort.Slice but your call

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah.
Good call.
I like that better here too actually 👌
Thanks


for _, env := range envs {
if !env.WorkspaceProvider.SSHEnabled {
clog.LogWarn(fmt.Sprintf("SSH is not enabled for workspace provider %q", env.WorkspaceProvider.Name),
Expand Down
8 changes: 8 additions & 0 deletions internal/coderutil/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ type EnvWithWorkspaceProvider struct {
WorkspaceProvider coder.WorkspaceProvider
}

// EWPsByEnvName implements the sort.Interface for sorting
// []coderutil.EnvWithWorkspaceProvider by coder.Environment.Name
type EWPsByEnvName []EnvWithWorkspaceProvider

func (e EWPsByEnvName) Len() int { return len(e) }
func (e EWPsByEnvName) Less(i, j int) bool { return e[i].Env.Name < e[j].Env.Name }
func (e EWPsByEnvName) Swap(i, j int) { e[i], e[j] = e[j], e[i] }

// EnvsWithProvider performs the composition of each Environment with its associated WorkspaceProvider.
func EnvsWithProvider(ctx context.Context, client coder.Client, envs []coder.Environment) ([]EnvWithWorkspaceProvider, error) {
pooledEnvs := make([]EnvWithWorkspaceProvider, 0, len(envs))
Expand Down