Skip to content

Commit 32bca5a

Browse files
committed
put back gid and timeout options
1 parent 5d56c1d commit 32bca5a

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

userinfo/main.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ func printVersion() {
2929
func main() {
3030
var (
3131
baseURL = flag.String("url", "http://foxden:8302", "URL of ClasseInfoService")
32+
gid = flag.String("gid", "", "Lookup by GID number")
33+
timeout = flag.Duration("timeout", 10*time.Second, "HTTP request timeout")
3234
jsonOut = flag.Bool("json", false, "Output raw JSON instead of formatted display")
3335
)
3436

@@ -43,6 +45,7 @@ func main() {
4345
fmt.Fprintf(os.Stderr, " %-24s %s\n", "<anything else>", "Lookup by name (e.g. \"Jane Smith\")")
4446
fmt.Fprintf(os.Stderr, "\n%s\n", "OPTIONS")
4547
fmt.Fprintf(os.Stderr, " %-24s %s\n", "-gid <number>", "Lookup by GID number")
48+
fmt.Fprintf(os.Stderr, " %-24s %s\n", "-timeout <duration>", "Request timeout (default: 10s)")
4649
fmt.Fprintf(os.Stderr, " %-24s %s\n", "-url <string>", "Base URL (default: http://host-dev:8080)")
4750
fmt.Fprintf(os.Stderr, " %-24s %s\n", "-json", "Print raw JSON output")
4851
fmt.Fprintf(os.Stderr, " %-24s %s\n", "-version", "Print version and exit")
@@ -71,15 +74,24 @@ func main() {
7174
var paramKey, paramValue string
7275

7376
userInput := UserInput{}
74-
args := flag.Args()
75-
if len(args) == 0 {
76-
flag.Usage()
77-
os.Exit(1)
78-
}
79-
if len(args) > 1 {
80-
fatalf("expected a single argument, got %d — wrap multi-word names in quotes", len(args))
77+
if *gid != "" {
78+
// Explicit -gid flag takes priority
79+
if !reUidNumber.MatchString(*gid) {
80+
fatalf("-gid value must be a number, got %q", *gid)
81+
}
82+
paramKey, paramValue = "gidNumber", *gid
83+
userInput.GidNumber = *gid
84+
} else {
85+
args := flag.Args()
86+
if len(args) == 0 {
87+
flag.Usage()
88+
os.Exit(1)
89+
}
90+
if len(args) > 1 {
91+
fatalf("expected a single argument, got %d — wrap multi-word names in quotes", len(args))
92+
}
93+
paramKey, paramValue = classifyArg(args[0])
8194
}
82-
paramKey, paramValue = classifyArg(args[0])
8395
switch paramKey {
8496
case "name":
8597
userInput.Name = paramValue
@@ -89,7 +101,6 @@ func main() {
89101
userInput.Uid = paramValue
90102
case "uidNumber":
91103
userInput.UidNumber = paramValue
92-
93104
}
94105

95106
var users []UserInfo
@@ -99,7 +110,7 @@ func main() {
99110
if err != nil {
100111
fatalf("%v", err)
101112
}
102-
users, err = fetchUsers(targetURL)
113+
users, err = fetchUsers(targetURL, *timeout)
103114
if err != nil {
104115
fatalf("%v", err)
105116
}

userinfo/utils.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ func buildURL(base, paramKey, paramValue string) (string, error) {
8282
return u.String(), nil
8383
}
8484

85-
func fetchUsers(rawURL string) ([]UserInfo, error) {
86-
timeout := time.Duration(30 * time.Second)
85+
func fetchUsers(rawURL string, timeout time.Duration) ([]UserInfo, error) {
8786
client := &http.Client{Timeout: timeout}
8887

8988
resp, err := client.Get(rawURL)

0 commit comments

Comments
 (0)