Skip to content

Commit 747c932

Browse files
authored
Merge pull request #3 from ConSol-Monitoring:cli-library-split
Cli-library-split
2 parents bdaf23e + beba2ca commit 747c932

File tree

11 files changed

+728
-348
lines changed

11 files changed

+728
-348
lines changed

.golangci.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
version: "2"
2+
linters:
3+
default: standard
4+
settings:
5+
gocritic:
6+
enabled-tags:
7+
- performance
8+
- style
9+
- experimental
10+
- security
11+
- opinionated
12+
- diagnostic
13+
lll:
14+
line-length: 200
15+
exclusions:
16+
generated: lax
17+
presets:
18+
- comments
19+
- common-false-positives
20+
- legacy
21+
- std-error-handling
22+
formatters:
23+
enable:
24+
- gofmt
25+
- goimports

go.mod

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
module github.com/consol-monitoring/check_prometheus
22

3-
go 1.19
4-
5-
replace internal/helper => ./internal/helper
6-
7-
replace internal/mode => ./internal/mode
3+
go 1.25
84

95
require (
10-
github.com/consol-monitoring/check_x v0.0.0-20230423195421-be7cfdc8c478
11-
github.com/urfave/cli v1.22.14
12-
internal/helper v0.0.0-00010101000000-000000000000
13-
internal/mode v0.0.0-00010101000000-000000000000
6+
github.com/consol-monitoring/check_x v0.0.0-20260108170459-f7c19720a9ad
7+
github.com/prometheus/client_golang v1.23.2
8+
github.com/prometheus/common v0.67.4
9+
github.com/urfave/cli/v3 v3.6.1
1410
)
1511

1612
require (
17-
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
1813
github.com/json-iterator/go v1.1.12 // indirect
14+
github.com/kr/text v0.2.0 // indirect
1915
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2016
github.com/modern-go/reflect2 v1.0.2 // indirect
21-
github.com/prometheus/client_golang v1.18.0 // indirect
22-
github.com/prometheus/common v0.45.0 // indirect
23-
github.com/russross/blackfriday/v2 v2.1.0 // indirect
17+
github.com/prometheus/client_model v0.6.2 // indirect
18+
go.yaml.in/yaml/v2 v2.4.3 // indirect
19+
google.golang.org/protobuf v1.36.10 // indirect
2420
)

go.sum

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,69 @@
1-
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
21
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
3-
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
4-
github.com/consol-monitoring/check_x v0.0.0-20230423195421-be7cfdc8c478 h1:1Gkwxgpji7m40MWYhS2ur/9nNQJTL9SO8hMBIFuzLPs=
5-
github.com/consol-monitoring/check_x v0.0.0-20230423195421-be7cfdc8c478/go.mod h1:NVETti6i+ZU5nH7slbTo6Muugvml+0heXX1oswt1FHE=
6-
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
7-
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
8-
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
2+
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
3+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
4+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
5+
github.com/consol-monitoring/check_x v0.0.0-20260108170459-f7c19720a9ad h1:vf0rv/4E0dT4fc6A8H0hL/U9zUQBJPq2EXLEfwqc+yI=
6+
github.com/consol-monitoring/check_x v0.0.0-20260108170459-f7c19720a9ad/go.mod h1:NVETti6i+ZU5nH7slbTo6Muugvml+0heXX1oswt1FHE=
7+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
98
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
109
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1110
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12-
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
11+
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
12+
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
13+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
14+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
1315
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
16+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
17+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1418
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
19+
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
1520
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
1621
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
17-
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg=
22+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
23+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
24+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
25+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
1826
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
1927
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
2028
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
2129
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
2230
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
31+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
32+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
2333
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU=
34+
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
2435
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2536
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
26-
github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk=
27-
github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA=
28-
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
29-
github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM=
30-
github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY=
31-
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
32-
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
33-
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
37+
github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o=
38+
github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg=
39+
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
40+
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
41+
github.com/prometheus/common v0.67.4 h1:yR3NqWO1/UyO1w2PhUvXlGQs/PtFmoveVO0KZ4+Lvsc=
42+
github.com/prometheus/common v0.67.4/go.mod h1:gP0fq6YjjNCLssJCQp0yk4M8W6ikLURwkdd/YKtTbyI=
43+
github.com/prometheus/procfs v0.16.1 h1:hZ15bTNuirocR6u0JZ6BAHHmwS1p8B4P6MRqxtzMyRg=
44+
github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlTmWl8x/U+Is=
45+
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
46+
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
3447
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
35-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
36-
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
3748
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
38-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
39-
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
40-
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
41-
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
42-
github.com/urfave/cli v1.22.14 h1:ebbhrRiGK2i4naQJr+1Xj92HXZCrK7MsyTS/ob3HnAk=
43-
github.com/urfave/cli v1.22.14/go.mod h1:X0eDS6pD6Exaclxm99NJ3FiCDRED7vIHpx2mDOHLvkA=
44-
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
45-
golang.org/x/oauth2 v0.12.0 h1:smVPGxink+n1ZI5pkQa8y6fZT0RW0MgCO5bFpepy4B4=
46-
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
47-
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
48-
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
49-
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
49+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
50+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
51+
github.com/urfave/cli/v3 v3.6.1 h1:j8Qq8NyUawj/7rTYdBGrxcH7A/j7/G8Q5LhWEW4G3Mo=
52+
github.com/urfave/cli/v3 v3.6.1/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso=
53+
go.yaml.in/yaml/v2 v2.4.3 h1:6gvOSjQoTB3vt1l+CU+tSyi/HOjfOjRLJ4YwYZGwRO0=
54+
go.yaml.in/yaml/v2 v2.4.3/go.mod h1:zSxWcmIDjOzPXpjlTTbAsKokqkDNAVtZO0WOMiT90s8=
55+
golang.org/x/net v0.46.0 h1:giFlY12I07fugqwPuWJi68oOnpfqFnJIJzaIIm2JVV4=
56+
golang.org/x/net v0.46.0/go.mod h1:Q9BGdFy1y4nkUwiLvT5qtyhAnEHgnQ/zd8PfU6nc210=
57+
golang.org/x/oauth2 v0.32.0 h1:jsCblLleRMDrxMN29H3z/k1KliIvpLgCkE6R8FXXNgY=
58+
golang.org/x/oauth2 v0.32.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=
59+
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
60+
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
61+
golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k=
62+
golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM=
63+
google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE=
64+
google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
5065
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
51-
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
52-
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
53-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
66+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
67+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
5468
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
5569
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/helper/go.mod

Lines changed: 0 additions & 3 deletions
This file was deleted.

internal/helper/prometheus.go

Lines changed: 101 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,139 @@
11
package helper
22

33
import (
4+
"crypto/tls"
45
"fmt"
5-
"io/ioutil"
6+
"io"
67
"net/http"
8+
"net/http/cookiejar"
9+
"net/url"
10+
"strings"
711
"time"
812

9-
"github.com/consol-monitoring/check_x"
1013
"github.com/prometheus/client_golang/api"
1114
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
1215
"github.com/prometheus/common/model"
1316
)
1417

18+
// TimestampFreshness is the amount of second a result is treated as valid
19+
var TimestampFreshness int
20+
21+
// InsecureSkipVerify will skip TLS certificate verification when set to true. It will be used when constructing http Transport
22+
var InsecureSkipVerify bool
23+
24+
// Cookies parsed into []*http.Cookie
25+
var Cookies []*http.Cookie
26+
27+
// Verbose flag writes to here
28+
var Verbose bool
29+
30+
type prometheusInterceptor struct {
31+
next http.RoundTripper
32+
}
33+
34+
// Interceptor function used in verbose mode
35+
func (i *prometheusInterceptor) RoundTrip(req *http.Request) (*http.Response, error) {
36+
if Verbose {
37+
fmt.Printf("Sending %s request to %s\n", req.Method, req.URL.String())
38+
fmt.Printf("Request:\n%+v\n", req)
39+
fmt.Printf("Url:\n%+v\n", req.URL)
40+
fmt.Printf("Header:\n%+v\n", req.Header)
41+
42+
// Read and print the body content
43+
if req.Body != nil {
44+
bodyBytes, err := io.ReadAll(req.Body)
45+
if err != nil {
46+
fmt.Printf("Error reading body: %v\n", err)
47+
} else {
48+
fmt.Printf("Body:\n%s\n", string(bodyBytes))
49+
// Restore the body for further processing
50+
req.Body = io.NopCloser(strings.NewReader(string(bodyBytes)))
51+
}
52+
} else {
53+
fmt.Printf("Body is empty\n")
54+
}
55+
}
56+
57+
// 2. Ensure the Content-Type is set
58+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
59+
60+
return i.next.RoundTrip(req)
61+
}
62+
1563
// NewAPIClientV1 will create an prometheus api client v1
16-
func NewAPIClientV1(address string) (v1.API, error) {
17-
client, err := api.NewClient(api.Config{
18-
Address: address,
64+
func NewAPIClientV1(address *url.URL) (v1.API, error) {
65+
baseTransport := http.DefaultTransport.(*http.Transport).Clone()
66+
baseTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: InsecureSkipVerify}
67+
68+
interceptedTransport := &prometheusInterceptor{
69+
next: baseTransport,
70+
}
71+
72+
httpClient := &http.Client{
73+
Transport: interceptedTransport,
74+
}
75+
76+
// Initialize cookie jar only when Cookies are provided
77+
if len(Cookies) > 0 {
78+
jar, _ := cookiejar.New(nil)
79+
httpClient.Jar = jar
80+
httpClient.Jar.SetCookies(address, Cookies)
81+
}
82+
83+
prometheusClient, err := api.NewClient(api.Config{
84+
Address: address.String(),
85+
Client: httpClient,
1986
})
87+
2088
if err != nil {
2189
return nil, err
2290
}
23-
return v1.NewAPI(client), nil
91+
92+
return v1.NewAPI(prometheusClient), nil
2493
}
2594

2695
// DoAPIRequest does the http handling for an api request
27-
func DoAPIRequest(address string) ([]byte, error) {
28-
resp, err := http.DefaultClient.Get(address)
96+
func DoAPIRequest(url *url.URL) ([]byte, error) {
97+
transport := http.DefaultTransport.(*http.Transport).Clone()
98+
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: InsecureSkipVerify}
99+
100+
httpClient := &http.Client{
101+
Transport: transport,
102+
}
103+
104+
if len(Cookies) > 0 {
105+
jar, _ := cookiejar.New(nil)
106+
httpClient.Jar = jar
107+
httpClient.Jar.SetCookies(url, Cookies)
108+
}
109+
110+
resp, err := httpClient.Get(url.String())
29111
if err != nil {
30112
return nil, err
31113
}
32-
body, err := ioutil.ReadAll(resp.Body)
114+
defer resp.Body.Close()
115+
116+
body, err := io.ReadAll(resp.Body)
33117
if err != nil {
34118
return nil, err
35119
}
120+
36121
return body, nil
37122
}
38123

39-
// TimestampFreshness is the amount of second a result is treated as valid
40-
var TimestampFreshness int
41-
42124
// CheckTimestampFreshness tests if the data is still valid
43-
func CheckTimestampFreshness(timestamp model.Time) {
44-
CheckTimeFreshness(time.Unix(int64(timestamp), 0))
125+
func CheckTimestampFreshness(timestamp model.Time) error {
126+
return CheckTimeFreshness(time.Unix(int64(timestamp), 0))
45127
}
46128

47129
// CheckTimeFreshness tests if the data is still valid
48-
func CheckTimeFreshness(timestamp time.Time) {
130+
func CheckTimeFreshness(timestamp time.Time) error {
49131
if TimestampFreshness == 0 {
50-
return
132+
return fmt.Errorf("error when checking time freshness, timestampFreshness is zero")
51133
}
52-
timeDiff := time.Now().Sub(timestamp)
134+
timeDiff := time.Since(timestamp)
53135
if int(timeDiff.Seconds()) > TimestampFreshness {
54-
check_x.Exit(check_x.Unknown, fmt.Sprintf("One of the scraped data exceed the freshness by %ds", int(timeDiff.Seconds())-TimestampFreshness))
136+
return fmt.Errorf("one of the scraped data exceed the freshness by %ds", int(timeDiff.Seconds())-TimestampFreshness)
55137
}
138+
return nil
56139
}

internal/mode/go.mod

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)