diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml index d3d0053..6a19f44 100644 --- a/.github/workflows/actions.yml +++ b/.github/workflows/actions.yml @@ -13,14 +13,14 @@ jobs: - name: Set up Go uses: actions/setup-go@v3 with: - go-version: 1.18 + go-version: 1.21 - name: checkout repo uses: actions/checkout@v2 - name: read version file id: getversion - run: echo "::set-output name=version::$(cat VERSION)" + run: echo "::set-output name=version::$(make version)" - name: Build tar files run: make dist diff --git a/CHANGELOG.md b/CHANGELOG.md index 71de5c5..52e9af8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,9 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) with the minor change that we use a prefix instead of grouping. This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## Upcoming +## [1.9.0] - 2024-01-09 +- Changed: updated makefile to a more generic one +- Fixed: minor issues stated from the new make update, eg replaced deprecated ioutils ## [1.8.0] - 2024-01-09 - Removed: dependabot settings diff --git a/Makefile b/Makefile index 0caabca..edc1429 100644 --- a/Makefile +++ b/Makefile @@ -1,87 +1,131 @@ GO ?= go GOOPTS ?= - -pkgs = ./... +GOFMT ?= gofmt +STATICCHECK ?= staticcheck +PACKAGES ?= ./... +CHANGELOG ?= CHANGELOG.md BIN_DIR ?= ./bin BUILD_DIR ?= ./build DIST_DIR ?= ./dist -APPNAME ?= script_exporter -VERSION ?= $(shell cat ./VERSION 2> /dev/null) + +APPNAME ?= $(shell perl -ne 'if (/^module /) { s/^module .+\///; print; }' go.mod) +VERSION ?= $(shell perl -lne 'if (/^\#\# \[\d+\.\d+\.\d+\] - [^\[A-Za-z]/) { print substr((split(/\s+/))[1], 1, -1); last; }' $(CHANGELOG)) COMMIT ?= $(shell git rev-parse --verify HEAD) DATE := $(shell date +%FT%T%z) +TARGETS := freebsd/amd64 darwin/amd64 darwin/arm64 linux/amd64 linux/arm64 windows/amd64 -GOPATH ?= $(shell go env GOPATH) GO_LDFLAGS += -X main.name=$(APPNAME) GO_LDFLAGS += -X main.version=v$(VERSION) GO_LDFLAGS += -X main.commit=$(COMMIT) GO_LDFLAGS += -X main.date=$(DATE) # strip debug info from binary -GO_LDFLAGS += -s -w +GO_LDFLAGS += -s -w -GO_LDFLAGS := -ldflags="$(GO_LDFLAGS)" +GO_LDFLAGS := -ldflags="$(GO_LDFLAGS)" -OS := $(if $(GOOS),$(GOOS),$(shell go env GOOS)) -ARCH := $(if $(GOARCH),$(GOARCH),$(shell go env GOARCH)) +VERSION_NEXT ?= $(shell echo $(word 1, $(subst ., ,$(VERSION))).$$(($(word 2, $(subst ., ,$(VERSION))) + 1)).0) +CHANGELOG_LINE ?= - Security: dependency and security updates -.PHONY: all -all: test build - -.PHONY: update-dependencies -update-dependencies: - $(GO) get -t -u ./... - $(GO) mod tidy +ifeq (,$(shell sed -nE '/\#\# .*(upcoming|unreleased)/Ip' $(CHANGELOG))) + CHANGELOG_LINES ?= \#\# \[$(VERSION_NEXT)\] - $(shell date +%F)\n$(CHANGELOG_LINE)\n\n +else + CHANGELOG_LINES ?= $(CHANGELOG_LINE)\n\n +endif -.PHONY: update -update: clean update-dependencies test build - -.PHONY: build -build: - $(GO) build $(GO_LDFLAGS) -o $(BUILD_DIR)/$(APPNAME)-$(VERSION)-$(OS)-$(ARCH)/$(APPNAME) -v ./... +.PHONY: version +version: + @echo "$(VERSION)" .PHONY: test test: - mkdir -p $(BUILD_DIR) - $(GO) test -v -race -coverprofile=$(BUILD_DIR)/test-coverage.out $(pkgs) + @echo "Start test for: $(APPNAME) v$(VERSION)" -.PHONY: clean -clean: - $(GO) clean - rm -rf $(BUILD_DIR) $(DIST_DIR) + test -z "$$($(GOFMT) -l .)" # Check Code is formatted correctly + $(GO) mod verify # ensure that go.sum agrees with what's in the module cache + $(GO) vet $(PACKAGES) # examines Go source code and reports suspicious constructs -.PHONY: compile -compile: - # FreeBDS - GOOS=freebsd GOARCH=amd64 $(GO) build $(GO_LDFLAGS) -o $(BUILD_DIR)/$(APPNAME)-$(VERSION)-freebsd-amd64/$(APPNAME) -v ./... - # MacOS - GOOS=darwin GOARCH=amd64 $(GO) build $(GO_LDFLAGS) -o $(BUILD_DIR)/$(APPNAME)-$(VERSION)-darwin-amd64/$(APPNAME) -v ./... - # MacOS M1 - GOOS=darwin GOARCH=arm64 $(GO) build $(GO_LDFLAGS) -o $(BUILD_DIR)/$(APPNAME)-$(VERSION)-darwin-arm64/$(APPNAME) -v ./... - # Linux - GOOS=linux GOARCH=amd64 $(GO) build $(GO_LDFLAGS) -o $(BUILD_DIR)/$(APPNAME)-$(VERSION)-linux-amd64/$(APPNAME) -v ./... - # Windows - GOOS=windows GOARCH=amd64 $(GO) build $(GO_LDFLAGS) -o $(BUILD_DIR)/$(APPNAME)-$(VERSION)-windows-amd64/$(APPNAME).exe -v ./... +ifneq (,$(shell which $(STATICCHECK))) + $(STATICCHECK) $(PACKAGES) # extensive analysis of Go code +endif -.PHONY: dist -dist: clean compile - mkdir -p $(DIST_DIR) + mkdir -p $(BUILD_DIR) + # run unit test with coverage + $(GO) test -race -coverprofile=$(BUILD_DIR)/test-coverage.out -json $(PACKAGES) > $(BUILD_DIR)/test-report-unit.json + # run benchmark test once to make sure they work + $(GO) test -run=- -bench=. -benchtime=1x -json $(PACKAGES) > $(BUILD_DIR)/test-report-benchmark.json - tar -C $(BUILD_DIR) -cvf $(BUILD_DIR)/$(APPNAME)-$(VERSION)-freebsd-amd64.tar $(APPNAME)-$(VERSION)-freebsd-amd64 - gzip $(BUILD_DIR)/$(APPNAME)-$(VERSION)-freebsd-amd64.tar - mv $(BUILD_DIR)/$(APPNAME)-$(VERSION)-freebsd-amd64.tar.gz $(DIST_DIR)/$(APPNAME)-$(VERSION)-freebsd-amd64.tar.gz +.PHONY: build +build: + @echo "Start build for: $(APPNAME) v$(VERSION)" + + @if grep -q "package main" *.go 2>/dev/null ; then \ + mkdir -p $(BUILD_DIR); \ + for target in $(TARGETS); do \ + os=$$(echo $$target | cut -d/ -f1); \ + arch=$$(echo $$target | cut -d/ -f2); \ + GOOS=$$os GOARCH=$$arch $(GO) build $(GO_LDFLAGS) -o $(BUILD_DIR)/$(VERSION)-$$os-$$arch/ $(PACKAGES); \ + done; \ + else \ + echo "No building required, module does not contain the 'main' package."; \ + fi + +.PHONY: dist-check +dist-check: +ifneq (,$(filter $(bamboo_planRepository_branchName),release/acceptance master)) + @echo "Performing dist check for: $(bamboo_planRepository_branchName)" + + @if ! test -z "$$(sed -E -n '/(upcoming|unreleased)/I,/##/p' changelog.md | sed '1d;$$d' | sed 's/[[:space:]-]//g')"; then \ + echo "Error: cannot generate dist, changelog.md must not contain unreleased lines."; \ + exit 1; \ + fi + + # Check for changes in go.mod or go.sum + @mkdir -p $(BUILD_DIR) + @cp go.mod $(BUILD_DIR)/go.mod.chk + @cp go.sum $(BUILD_DIR)/go.sum.chk + + test -z $$($(GO) mod tidy) + + diff go.mod $(BUILD_DIR)/go.mod.chk + diff go.sum $(BUILD_DIR)/go.sum.chk + + @rm $(BUILD_DIR)/go.mod.chk $(BUILD_DIR)/go.sum.chk +else + @echo "Skipping dist check." +endif + +.PHONY: dist-create +dist-create: + @echo "Create dist for: $(APPNAME) v$(VERSION)" + + @if grep -q "package main" *.go 2>/dev/null ; then \ + mkdir -p $(DIST_DIR); \ + for target in $(TARGETS); do \ + os=$$(echo $$target | cut -d/ -f1); \ + arch=$$(echo $$target | cut -d/ -f2); \ + tar -C $(BUILD_DIR) -cvzf $(DIST_DIR)/$(APPNAME)-$(VERSION)-$$os-$$arch.tar.gz $(VERSION)-$$os-$$arch; \ + done; \ + fi - tar -C $(BUILD_DIR) -cvf $(BUILD_DIR)/$(APPNAME)-$(VERSION)-darwin-amd64.tar $(APPNAME)-$(VERSION)-darwin-amd64 - gzip $(BUILD_DIR)/$(APPNAME)-$(VERSION)-darwin-amd64.tar - mv $(BUILD_DIR)/$(APPNAME)-$(VERSION)-darwin-amd64.tar.gz $(DIST_DIR)/$(APPNAME)-$(VERSION)-darwin-amd64.tar.gz +.PHONY: dist +dist: clean dist-check build dist-create + +.PHONY: update-dependencies +update-dependencies: + $(GO) get go@latest + # Remove patch level of GO version + @sed -i "" -E 's/(go [0-9]+\.[0-9]+)\.[0-9]+/\1/' go.mod + $(GO) get -t -u $(PACKAGES) + $(GO) mod tidy - tar -C $(BUILD_DIR) -cvf $(BUILD_DIR)/$(APPNAME)-$(VERSION)-darwin-arm64.tar $(APPNAME)-$(VERSION)-darwin-arm64 - gzip $(BUILD_DIR)/$(APPNAME)-$(VERSION)-darwin-arm64.tar - mv $(BUILD_DIR)/$(APPNAME)-$(VERSION)-darwin-arm64.tar.gz $(DIST_DIR)/$(APPNAME)-$(VERSION)-darwin-arm64.tar.gz + # Adding lines to changelog + @sed -i "" -e 's/\(## \[$(VERSION)\]\)/$(CHANGELOG_LINES)\1/' $(CHANGELOG) - tar -C $(BUILD_DIR) -cvf $(BUILD_DIR)/$(APPNAME)-$(VERSION)-linux-amd64.tar $(APPNAME)-$(VERSION)-linux-amd64 - gzip $(BUILD_DIR)/$(APPNAME)-$(VERSION)-linux-amd64.tar - mv $(BUILD_DIR)/$(APPNAME)-$(VERSION)-linux-amd64.tar.gz $(DIST_DIR)/$(APPNAME)-$(VERSION)-linux-amd64.tar.gz +.PHONY: update +update: clean update-dependencies test - tar -C $(BUILD_DIR) -cvf $(BUILD_DIR)/$(APPNAME)-$(VERSION)-windows-amd64.tar $(APPNAME)-$(VERSION)-windows-amd64 - gzip $(BUILD_DIR)/$(APPNAME)-$(VERSION)-windows-amd64.tar - mv $(BUILD_DIR)/$(APPNAME)-$(VERSION)-windows-amd64.tar.gz $(DIST_DIR)/$(APPNAME)-$(VERSION)-windows-amd64.tar.gz +.PHONY: clean +clean: + $(GO) clean + rm -rf $(BUILD_DIR) $(DIST_DIR) diff --git a/VERSION b/VERSION deleted file mode 100644 index 27f9cd3..0000000 --- a/VERSION +++ /dev/null @@ -1 +0,0 @@ -1.8.0 diff --git a/config.go b/config.go index 2acb862..ea65cb1 100644 --- a/config.go +++ b/config.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "io/ioutil" "os" "path" "regexp" @@ -80,7 +79,6 @@ type probeArgument struct { var restrictedParams = []string{"module", "debug"} var config internalConfig -var probes []string func setup() { // retrieve flags since that could contain the config folder @@ -107,7 +105,7 @@ func setup() { func readConfig() { log.Infof("Looking for configuration files in: %s", *flags.configDir) - files, err := ioutil.ReadDir(*flags.configDir) + files, err := os.ReadDir(*flags.configDir) if err != nil { log.Fatal("Could not load config files: ", err) } @@ -118,7 +116,7 @@ func readConfig() { if !file.IsDir() && (strings.HasSuffix(file.Name(), ".yaml") || strings.HasSuffix(file.Name(), ".yml")) { log.Debug("[readConfig] loading config file: ", file.Name()) - yamlFile, err := ioutil.ReadFile( + yamlFile, err := os.ReadFile( path.Join(*flags.configDir, file.Name()), ) if err != nil { @@ -170,7 +168,8 @@ func configProbes(probesConfig YamlProbeConfig, fileName string) { for _, probe := range probesConfig { log.Debug("[configProbes] found probe: ", probe.Name) - if match, _ := regexp.MatchString("^[a-zA-Z0-9:_]+$", probe.Name); !match { + matchName := regexp.MustCompile("^[a-zA-Z0-9:_]+$") + if match := matchName.MatchString(probe.Name); !match { log.Fatalf("Config failure probe with name '%s' name must match ^[a-zA-Z0-9:_]+$ (%s)", probe.Name, fileName) } diff --git a/go.mod b/go.mod index cd4f67f..551efb3 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module mostwanted.io/prometheus/script_exporter -go 1.20 +go 1.21 require ( github.com/gorilla/handlers v1.5.2 diff --git a/go.sum b/go.sum index 4ca1255..b05036b 100644 --- a/go.sum +++ b/go.sum @@ -9,9 +9,11 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/gorilla/handlers v1.5.2 h1:cLTUSsNkgcwhgRqvCNmdbRWG0A3N4F+M2nWKdScwyEE= github.com/gorilla/handlers v1.5.2/go.mod h1:dX+xVpaxdSw+q0Qek8SSsl3dfMk3jNddUkMzo0GtH0w= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg= @@ -27,6 +29,7 @@ github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGy github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -40,6 +43,7 @@ google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7 google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index e0c0012..f63d0d1 100644 --- a/main.go +++ b/main.go @@ -29,7 +29,7 @@ func main() { log.Info("Started on ", addr) r := http.NewServeMux() - + r.HandleFunc("/", landingpage) r.Handle("/metrics", promhttp.Handler()) r.HandleFunc("/probe", probe)