@@ -18,19 +18,21 @@ import (
1818// It includes the target URL, HTTP/HTTPS usage preference, count of pings,
1919// headers to be retrieved, and sleep duration between pings.
2020type config struct {
21- url string
22- useHTTP bool
23- count int
24- headers string
25- sleep int64
21+ url string
22+ useHTTP bool
23+ count int
24+ headers string
25+ sleep int64
26+ useragent string
2627}
2728
2829var (
29- url string
30- useHTTP bool
31- count int
32- headers string
33- sleep int64
30+ url string
31+ useHTTP bool
32+ count int
33+ headers string
34+ sleep int64
35+ useragent string
3436)
3537
3638// init is an initialization function that sets up command-line flags
@@ -41,6 +43,7 @@ func init() {
4143 pflag .StringVar (& url , "url" , "" , "Specify the URL to ping. (required)" )
4244 pflag .BoolVar (& useHTTP , "insecure" , false , "Use HTTP instead of HTTPS. By default, HTTPS is used." )
4345 pflag .StringVar (& headers , "headers" , "" , "A comma-separated list of response headers to include in the output." )
46+ pflag .StringVar (& useragent , "user-agent" , "" , "The user-agent value to include in the request headers." )
4447 pflag .IntVar (& count , "count" , 4 , "Set the number of pings to send. Default is 4." )
4548 pflag .Int64Var (& sleep , "sleep" , 0 , "Set the delay (in seconds) between successive pings. Default is 0 (no delay)." )
4649 pflag .Usage = usage
@@ -83,11 +86,12 @@ func main() {
8386 }
8487
8588 config := config {
86- url : url ,
87- useHTTP : useHTTP ,
88- count : count ,
89- headers : headers ,
90- sleep : sleep ,
89+ url : url ,
90+ useHTTP : useHTTP ,
91+ count : count ,
92+ headers : headers ,
93+ sleep : sleep ,
94+ useragent : useragent ,
9195 }
9296
9397 go func () {
@@ -107,9 +111,9 @@ func main() {
107111}
108112
109113// run executes the httping process based on the provided configuration and context.
110- // It handles pinging the specified URL, collecting response data, and writing the
111- // output to the provided writer. The function respects context cancellation,
112- // allowing for graceful termination. It accumulates statistics throughout the
114+ // It handles pinging the specified URL, collecting response data, and writing the
115+ // output to the provided writer. The function respects context cancellation,
116+ // allowing for graceful termination. It accumulates statistics throughout the
113117// execution and prints a summary at the end or upon early termination.
114118func run (ctx context.Context , config config , writer io.Writer ) error {
115119 var count int
@@ -138,7 +142,7 @@ func run(ctx context.Context, config config, writer io.Writer) error {
138142 fmt .Println (stats .String ())
139143 return ctx .Err ()
140144 default :
141- response , err := httping .MakeRequest (config .useHTTP , config .url , config .headers )
145+ response , err := httping .MakeRequest (config .useHTTP , config .useragent , config . url , config .headers )
142146 if err != nil {
143147 return err
144148 }
0 commit comments