Skip to content

Commit f5a9f3e

Browse files
committed
improved vnc session retrival
1 parent 818cbec commit f5a9f3e

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

internal/cmd/cluster.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,9 @@ When the username is specified by the "-u" option, only the VNCs owned by the us
393393

394394
// filling access node hosts
395395
go func() {
396-
var mlist []string
396+
397+
// counter for number of nodes to visit
398+
mcnt := 0
397399

398400
// 1. read machinelist from user provided hosts from commandline arguments
399401
sort.Strings(args)
@@ -402,11 +404,12 @@ When the username is specified by the "-u" option, only the VNCs owned by the us
402404
n = fmt.Sprintf("%s.%s", n, NetDomain)
403405
}
404406
log.Debugf("add node %s\n", n)
405-
mlist = append(mlist, n)
407+
nodes <- n
408+
mcnt++
406409
}
407410

408411
// 2. read machinelist from the machinelist file
409-
if len(mlist) == 0 {
412+
if mcnt == 0 {
410413
// read nodes from user provided machinelist
411414

412415
if fml, err := os.Open(vncMachineListFile); err == nil {
@@ -417,7 +420,8 @@ When the username is specified by the "-u" option, only the VNCs owned by the us
417420
if !strings.HasSuffix(n, fmt.Sprintf(".%s", NetDomain)) {
418421
n = fmt.Sprintf("%s.%s", n, NetDomain)
419422
}
420-
mlist = append(mlist, n)
423+
nodes <- n
424+
mcnt++
421425
}
422426

423427
if err := scanner.Err(); err != nil {
@@ -429,7 +433,7 @@ When the username is specified by the "-u" option, only the VNCs owned by the us
429433
}
430434

431435
// 3. read machinelist from the Gangalia
432-
if len(mlist) == 0 {
436+
if mcnt == 0 {
433437
// TODO: append hostname of all of the access nodes.
434438
accs, err := dg.GetAccessNodes()
435439
// sort nodes
@@ -438,28 +442,20 @@ When the username is specified by the "-u" option, only the VNCs owned by the us
438442
log.Errorln(err)
439443
}
440444

441-
mlist = append(mlist, accs...)
445+
for _, n := range accs {
446+
nodes <- n
447+
}
442448
}
443449

444-
// fill mlist to node channel
445-
for _, n := range mlist {
446-
nodes <- n
447-
}
450+
// close the nodes channel
448451
close(nodes)
449452
}()
450453

451454
// reorganise internal data structure for sorting
452455
var _vncs []trqhelper.VNCServer
453-
for d := range vncservers {
454-
_vncs = append(_vncs, d)
455-
}
456-
457-
// sort _vncs and make tabluar display on stdout
458-
table := tablewriter.NewWriter(os.Stdout)
459-
table.SetHeader([]string{"Username", "VNC session"})
460-
461-
sort.Slice(_vncs, func(i, j int) bool {
462456

457+
// function for sorting VNC sessions by host.
458+
vncSortByHost := func(i, j int) bool {
463459
datai := strings.Split(_vncs[i].ID, ":")
464460
dataj := strings.Split(_vncs[j].ID, ":")
465461

@@ -474,8 +470,17 @@ When the username is specified by the "-u" option, only the VNCs owned by the us
474470
idj, _ := strconv.ParseUint(dataj[1], 10, 32)
475471

476472
return idi < idj
477-
})
473+
}
474+
475+
for d := range vncservers {
476+
_vncs = append(_vncs, d)
477+
// perform sorting when a vnc session is added to the list.
478+
sort.Slice(_vncs, vncSortByHost)
479+
}
478480

481+
// sort _vncs and make tabluar display on stdout
482+
table := tablewriter.NewWriter(os.Stdout)
483+
table.SetHeader([]string{"Username", "VNC session"})
479484
for _, vnc := range _vncs {
480485
table.Append([]string{vnc.Owner, vnc.ID})
481486
}

0 commit comments

Comments
 (0)