Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Commit3a6edf6

Browse files
authored
feat: add username to 'coder ws ls' (#486)
* feat: add username to 'coder ws ls'* make lint* make lint
1 parent9a9a1f3 commit3a6edf6

File tree

3 files changed

+68
-19
lines changed

3 files changed

+68
-19
lines changed

‎internal/cmd/login.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func pingAPI(ctx context.Context, workspaceURL *url.URL, token string) error {
118118
returnnil
119119
}
120120

121-
// isWSL determines if coder-cli is running within Windows Subsystem for Linux
121+
// isWSL determines if coder-cli is running within Windows Subsystem for Linux.
122122
funcisWSL() (bool,error) {
123123
ifruntime.GOOS==goosDarwin||runtime.GOOS==goosWindows {
124124
returnfalse,nil
@@ -130,7 +130,7 @@ func isWSL() (bool, error) {
130130
returnstrings.Contains(strings.ToLower(string(data)),"microsoft"),nil
131131
}
132132

133-
// openURL opens the provided URL via user's default browser
133+
// openURL opens the provided URL via user's default browser.
134134
funcopenURL(urlstring)error {
135135
varcmdstring
136136
varargs []string

‎internal/coderutil/workspace.go

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,15 @@ func DefaultWorkspaceProvider(ctx context.Context, c coder.Client) (*coder.Kuber
7979
// WorkspaceTable defines an Workspace-like structure with associated entities composed in a human
8080
// readable form.
8181
typeWorkspaceTablestruct {
82-
Namestring`table:"Name"`
83-
Imagestring`table:"Image"`
84-
CPUfloat32`table:"vCPU"`
85-
MemoryGBfloat32`table:"MemoryGB"`
86-
DiskGBint`table:"DiskGB"`
87-
Statusstring`table:"Status"`
88-
Providerstring`table:"Provider"`
89-
CVMbool`table:"CVM"`
82+
Namestring`table:"Name" json:"name"`
83+
Imagestring`table:"Image" json:"image"`
84+
CPUfloat32`table:"vCPU" json:"cpu"`
85+
MemoryGBfloat32`table:"MemoryGB" json:"memory_gb"`
86+
DiskGBint`table:"DiskGB" json:"disk_gb"`
87+
Statusstring`table:"Status" json:"status"`
88+
Providerstring`table:"Provider" json:"provider"`
89+
CVMbool`table:"CVM" json:"cvm"`
90+
Usernamestring`table:"Username" json:"username"`
9091
}
9192

9293
// WorkspacesHumanTable performs the composition of each Workspace with its associated ProviderName and ImageRepo.
@@ -96,6 +97,11 @@ func WorkspacesHumanTable(ctx context.Context, client coder.Client, workspaces [
9697
returnnil,err
9798
}
9899

100+
userMap,err:=MakeUserMap(ctx,client,workspaces)
101+
iferr!=nil {
102+
returnnil,err
103+
}
104+
99105
pooledWorkspaces:=make([]WorkspaceTable,0,len(workspaces))
100106
providers,err:=client.WorkspaceProviders(ctx)
101107
iferr!=nil {
@@ -105,25 +111,66 @@ func WorkspacesHumanTable(ctx context.Context, client coder.Client, workspaces [
105111
for_,p:=rangeproviders.Kubernetes {
106112
providerMap[p.ID]=p
107113
}
108-
for_,e:=rangeworkspaces {
109-
workspaceProvider,ok:=providerMap[e.ResourcePoolID]
114+
for_,ws:=rangeworkspaces {
115+
workspaceProvider,ok:=providerMap[ws.ResourcePoolID]
110116
if!ok {
111117
returnnil,xerrors.Errorf("fetch workspace workspace provider: %w",coder.ErrNotFound)
112118
}
113119
pooledWorkspaces=append(pooledWorkspaces,WorkspaceTable{
114-
Name:e.Name,
115-
Image:fmt.Sprintf("%s:%s",imageMap[e.ImageID].Repository,e.ImageTag),
116-
CPU:e.CPUCores,
117-
MemoryGB:e.MemoryGB,
118-
DiskGB:e.DiskGB,
119-
Status:string(e.LatestStat.ContainerStatus),
120+
Name:ws.Name,
121+
Image:fmt.Sprintf("%s:%s",imageMap[ws.ImageID].Repository,ws.ImageTag),
122+
CPU:ws.CPUCores,
123+
MemoryGB:ws.MemoryGB,
124+
DiskGB:ws.DiskGB,
125+
Status:string(ws.LatestStat.ContainerStatus),
120126
Provider:workspaceProvider.Name,
121-
CVM:e.UseContainerVM,
127+
CVM:ws.UseContainerVM,
128+
Username:userMap[ws.UserID].Username,
122129
})
123130
}
124131
returnpooledWorkspaces,nil
125132
}
126133

134+
funcMakeUserMap(ctx context.Context,client coder.Client,workspaces []coder.Workspace) (map[string]*coder.User,error) {
135+
var (
136+
mu sync.Mutex
137+
egroup=clog.LoggedErrGroup()
138+
)
139+
140+
userMap:=map[string]*coder.User{}
141+
142+
// Iterate over all the workspaces to get a list of unique User IDs.
143+
for_,ws:=rangeworkspaces {
144+
userMap[ws.UserID]=nil
145+
}
146+
147+
fetchIds:=make([]string,0,len(userMap))
148+
forid:=rangeuserMap {
149+
fetchIds=append(fetchIds,id)
150+
}
151+
152+
for_,id:=rangefetchIds {
153+
id:=id
154+
egroup.Go(func()error {
155+
user,err:=client.UserByID(ctx,id)
156+
iferr!=nil {
157+
returnxerrors.Errorf("get user by id: %w",err)
158+
}
159+
mu.Lock()
160+
defermu.Unlock()
161+
162+
userMap[id]=user
163+
returnnil
164+
})
165+
}
166+
167+
iferr:=egroup.Wait();err!=nil {
168+
returnnil,xerrors.Errorf("fetch all workspace users: %w",err)
169+
}
170+
171+
returnuserMap,nil
172+
}
173+
127174
// MakeImageMap fetches all image entities specified in the slice of workspaces, then places them into an ID map.
128175
funcMakeImageMap(ctx context.Context,client coder.Client,workspaces []coder.Workspace) (map[string]*coder.Image,error) {
129176
var (

‎wsnet/listen.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ func (l *listener) dial(ctx context.Context) (<-chan error, error) {
116116
_=l.ws.Close(websocket.StatusNormalClosure,"new connection inbound")
117117
}
118118

119+
// websocket lib documents that the response does not need to be closed.
120+
// nolint
119121
conn,resp,err:=websocket.Dial(ctx,l.broker,nil)
120122
iferr!=nil {
121123
ifresp!=nil {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp