Skip to content
Snippets Groups Projects
Commit 92f6f555 authored by Florent Gluck's avatar Florent Gluck
Browse files

Changed userlist output to only display last name, first name and email instead of all information.

To output all info, specify a new "-l" argument.
parent f81c3aaf
Branches
No related tags found
No related merge requests found
package cmdUser
import (
"strings"
"regexp"
"strings"
"encoding/json"
"nexus-client/cmd"
u "nexus-client/utils"
......@@ -29,14 +29,18 @@ func (user *User)String() string {
if err != nil {
return err.Error()
}
return string(output)
}
func printUsage(c cmd.Command) {
func printRegexUsage(c cmd.Command) {
for _, desc := range c.GetDesc() {
u.PrintlnErr(desc)
}
u.PrintlnErr("USAGE: ",c.GetName()," [regex ...]")
}
func printRegexUsageDetails() {
const usage string = `Only users matching the specified regexes will be listed.
Any number of regexes can be specified.
The regex matches the user email, first name and last name and is case-insensitive.
......@@ -54,12 +58,29 @@ Regex examples:
// "" -> matches everything
// "." -> matches everything
// "bla" -> matches any user containing "bla"
func printFilteredUsers(c cmd.Command, regexes []string, route string) int {
if len(regexes) < 1 {
func printFilteredUsers(c cmd.Command, args []string, route string) int {
if len(args) < 1 {
c.PrintUsage()
return 1
}
// Helper function to remove an element from a string array at a given index
removeArgAtIndex := func (slice []string, index int) []string {
return append(slice[:index], slice[index+1:]...)
}
// Check if a "-l" argument is specified
foundLongOutputFlag := -1
for idx, arg := range args {
if arg == "-l" {
foundLongOutputFlag = idx
}
}
if foundLongOutputFlag >= 0 {
removeArgAtIndex(args, foundLongOutputFlag)
}
client := g.GetInstance().Client
host := g.GetInstance().Host
......@@ -77,14 +98,18 @@ func printFilteredUsers(c cmd.Command, regexes []string, route string) int {
}
for _, user := range users {
for _, regex := range regexes {
for _, regex := range args {
fieldsToMatch := user.Email+" "+user.FirstName+" "+user.LastName
match, err := regexp.MatchString(strings.ToLower(regex), strings.ToLower(fieldsToMatch))
if err != nil {
u.PrintlnErr("Error matching \""+regex+"\": "+err.Error())
} else {
if match {
if foundLongOutputFlag >= 0 {
u.Println(user.String())
} else {
u.Println(user.LastName+" | "+user.FirstName+" | "+user.Email)
}
break
}
}
......
package cmdUser
import (
u "nexus-client/utils"
)
type List struct {
Name string
}
......@@ -15,7 +19,12 @@ func (cmd *List)GetDesc() []string {
}
func (cmd *List)PrintUsage() {
printUsage(cmd)
for _, desc := range cmd.GetDesc() {
u.PrintlnErr(desc)
}
u.PrintlnErr("USAGE: ",cmd.GetName(), " [-l] [regex ...]")
u.PrintlnErr("Use \"-l\" to specify detailed user output.")
printRegexUsageDetails()
}
func (cmd *List)Run(args []string) int {
......
......@@ -73,7 +73,7 @@ Regex examples:
// Prints a list of filtered VMs for a given route.
// Return 0 if everything went well or 1 in case of failure.
func printFilteredVMs(c cmd.Command, args []string, route string) int {
if len(args) == 0 {
if len(args) < 1 {
c.PrintUsage()
return 1
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment