-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.go
40 lines (33 loc) · 922 Bytes
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"github.com/rs/zerolog/log"
)
// getResponseBodyByURL makes a GET request to the given URL and returns the response body in a buffer.
func getResponseBodyByURL(url string) (*bytes.Buffer, error) {
log.Debug().Str("url", url).Msg("Making request to URL")
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
buf := new(bytes.Buffer)
bytesRead, err := io.Copy(buf, resp.Body)
if err != nil {
return nil, err
}
log.Debug().Str("url", url).Int64("bytesRead", bytesRead).Msg("Wrote response body into buffer")
return buf, nil
}
// getRequiredEnv returns the environment variable or an error if it is not set.
func getRequiredEnv(envVarName string) (string, error) {
val, exists := os.LookupEnv(envVarName)
if !exists {
return "", fmt.Errorf("env var '%s' does not exist", envVarName)
}
return val, nil
}