From ff037009090a42f7e535e4bf00beb09450782474 Mon Sep 17 00:00:00 2001 From: ryan nemeth Date: Mon, 5 Feb 2024 10:33:34 -0500 Subject: [PATCH] build for windows --- .github/workflows/build.yaml | 5 +++-- cmd/httping/main.go | 42 ++++++++++++++++++++---------------- httping.go | 6 ++++-- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index c794e19..d23c955 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -48,7 +48,8 @@ jobs: fi go mod tidy go build -o httping -ldflags "-X main.Version=${GITHUB_REF_NAME} -X main.BuiltBy=github-actions" main.go - mv httping builds/ + env GOOS=windows GOARCH=amd64 go build -o httping-win -ldflags "-X main.Version=${GITHUB_REF_NAME} -X main.BuiltBy=github-actions" main.go + mv httping* builds/ ls -lisa builds - name: go test @@ -94,7 +95,7 @@ jobs: with: name: ${{ env.GITHUB_REF_NAME }} tag: ${{ env.GITHUB_REF_NAME }} - artifacts: ./cmd/httping/builds/httping + artifacts: ./cmd/httping/builds/httping* bodyFile: "body.log" token: ${{ secrets.GITHUB_TOKEN }} removeArtifacts: true diff --git a/cmd/httping/main.go b/cmd/httping/main.go index ea063f3..e0940de 100644 --- a/cmd/httping/main.go +++ b/cmd/httping/main.go @@ -18,19 +18,21 @@ import ( // It includes the target URL, HTTP/HTTPS usage preference, count of pings, // headers to be retrieved, and sleep duration between pings. type config struct { - url string - useHTTP bool - count int - headers string - sleep int64 + url string + useHTTP bool + count int + headers string + sleep int64 + useragent string } var ( - url string - useHTTP bool - count int - headers string - sleep int64 + url string + useHTTP bool + count int + headers string + sleep int64 + useragent string ) // init is an initialization function that sets up command-line flags @@ -41,6 +43,7 @@ func init() { pflag.StringVar(&url, "url", "", "Specify the URL to ping. (required)") pflag.BoolVar(&useHTTP, "insecure", false, "Use HTTP instead of HTTPS. By default, HTTPS is used.") pflag.StringVar(&headers, "headers", "", "A comma-separated list of response headers to include in the output.") + pflag.StringVar(&useragent, "user-agent", "", "The user-agent value to include in the request headers.") pflag.IntVar(&count, "count", 4, "Set the number of pings to send. Default is 4.") pflag.Int64Var(&sleep, "sleep", 0, "Set the delay (in seconds) between successive pings. Default is 0 (no delay).") pflag.Usage = usage @@ -83,11 +86,12 @@ func main() { } config := config{ - url: url, - useHTTP: useHTTP, - count: count, - headers: headers, - sleep: sleep, + url: url, + useHTTP: useHTTP, + count: count, + headers: headers, + sleep: sleep, + useragent: useragent, } go func() { @@ -107,9 +111,9 @@ func main() { } // run executes the httping process based on the provided configuration and context. -// It handles pinging the specified URL, collecting response data, and writing the -// output to the provided writer. The function respects context cancellation, -// allowing for graceful termination. It accumulates statistics throughout the +// It handles pinging the specified URL, collecting response data, and writing the +// output to the provided writer. The function respects context cancellation, +// allowing for graceful termination. It accumulates statistics throughout the // execution and prints a summary at the end or upon early termination. func run(ctx context.Context, config config, writer io.Writer) error { var count int @@ -138,7 +142,7 @@ func run(ctx context.Context, config config, writer io.Writer) error { fmt.Println(stats.String()) return ctx.Err() default: - response, err := httping.MakeRequest(config.useHTTP, config.url, config.headers) + response, err := httping.MakeRequest(config.useHTTP, config.useragent, config.url, config.headers) if err != nil { return err } diff --git a/httping.go b/httping.go index bb462e3..3c48b31 100644 --- a/httping.go +++ b/httping.go @@ -59,9 +59,11 @@ func ParseURL(url string, useHTTP bool) string { // MakeRequest performs an HTTP GET request to the specified URL. // It returns an HttpResponse struct filled with response data. -func MakeRequest(useHTTP bool, url, headers string) (*HttpResponse, error) { +func MakeRequest(useHTTP bool, userAgent, url, headers string) (*HttpResponse, error) { var result *HttpResponse - userAgent := "httping" + if userAgent == "" { + userAgent = "httping" + } tr := &http.Transport{} if !useHTTP {