Skip to content

Commit c775791

Browse files
authored
Merge pull request #27 from Team-MostWanted/feature/INFRA-2354-updated-makefile
Feature/infra 2354 updated makefile
2 parents 7e6e557 + 02a4109 commit c775791

File tree

8 files changed

+117
-69
lines changed

8 files changed

+117
-69
lines changed

.github/workflows/actions.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ jobs:
1313
- name: Set up Go
1414
uses: actions/setup-go@v3
1515
with:
16-
go-version: 1.18
16+
go-version: 1.21
1717

1818
- name: checkout repo
1919
uses: actions/checkout@v2
2020

2121
- name: read version file
2222
id: getversion
23-
run: echo "::set-output name=version::$(cat VERSION)"
23+
run: echo "::set-output name=version::$(make version)"
2424

2525
- name: Build tar files
2626
run: make dist

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ All notable changes to this project will be documented in this file.
44
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.
55
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## Upcoming
7+
## [1.9.0] - 2024-01-09
8+
- Changed: updated makefile to a more generic one
9+
- Fixed: minor issues stated from the new make update, eg replaced deprecated ioutils
810

911
## [1.8.0] - 2024-01-09
1012
- Removed: dependabot settings

Makefile

Lines changed: 102 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,131 @@
11
GO ?= go
22
GOOPTS ?=
3-
4-
pkgs = ./...
3+
GOFMT ?= gofmt
4+
STATICCHECK ?= staticcheck
5+
PACKAGES ?= ./...
6+
CHANGELOG ?= CHANGELOG.md
57

68
BIN_DIR ?= ./bin
79
BUILD_DIR ?= ./build
810
DIST_DIR ?= ./dist
9-
APPNAME ?= script_exporter
10-
VERSION ?= $(shell cat ./VERSION 2> /dev/null)
11+
12+
APPNAME ?= $(shell perl -ne 'if (/^module /) { s/^module .+\///; print; }' go.mod)
13+
VERSION ?= $(shell perl -lne 'if (/^\#\# \[\d+\.\d+\.\d+\] - [^\[A-Za-z]/) { print substr((split(/\s+/))[1], 1, -1); last; }' $(CHANGELOG))
1114
COMMIT ?= $(shell git rev-parse --verify HEAD)
1215
DATE := $(shell date +%FT%T%z)
16+
TARGETS := freebsd/amd64 darwin/amd64 darwin/arm64 linux/amd64 linux/arm64 windows/amd64
1317

14-
GOPATH ?= $(shell go env GOPATH)
1518
GO_LDFLAGS += -X main.name=$(APPNAME)
1619
GO_LDFLAGS += -X main.version=v$(VERSION)
1720
GO_LDFLAGS += -X main.commit=$(COMMIT)
1821
GO_LDFLAGS += -X main.date=$(DATE)
1922
# strip debug info from binary
20-
GO_LDFLAGS += -s -w
23+
GO_LDFLAGS += -s -w
2124

22-
GO_LDFLAGS := -ldflags="$(GO_LDFLAGS)"
25+
GO_LDFLAGS := -ldflags="$(GO_LDFLAGS)"
2326

24-
OS := $(if $(GOOS),$(GOOS),$(shell go env GOOS))
25-
ARCH := $(if $(GOARCH),$(GOARCH),$(shell go env GOARCH))
27+
VERSION_NEXT ?= $(shell echo $(word 1, $(subst ., ,$(VERSION))).$$(($(word 2, $(subst ., ,$(VERSION))) + 1)).0)
28+
CHANGELOG_LINE ?= - Security: dependency and security updates
2629

27-
.PHONY: all
28-
all: test build
29-
30-
.PHONY: update-dependencies
31-
update-dependencies:
32-
$(GO) get -t -u ./...
33-
$(GO) mod tidy
30+
ifeq (,$(shell sed -nE '/\#\# .*(upcoming|unreleased)/Ip' $(CHANGELOG)))
31+
CHANGELOG_LINES ?= \#\# \[$(VERSION_NEXT)\] - $(shell date +%F)\n$(CHANGELOG_LINE)\n\n
32+
else
33+
CHANGELOG_LINES ?= $(CHANGELOG_LINE)\n\n
34+
endif
3435

35-
.PHONY: update
36-
update: clean update-dependencies test build
37-
38-
.PHONY: build
39-
build:
40-
$(GO) build $(GO_LDFLAGS) -o $(BUILD_DIR)/$(APPNAME)-$(VERSION)-$(OS)-$(ARCH)/$(APPNAME) -v ./...
36+
.PHONY: version
37+
version:
38+
@echo "$(VERSION)"
4139

4240
.PHONY: test
4341
test:
44-
mkdir -p $(BUILD_DIR)
45-
$(GO) test -v -race -coverprofile=$(BUILD_DIR)/test-coverage.out $(pkgs)
42+
@echo "Start test for: $(APPNAME) v$(VERSION)"
4643

47-
.PHONY: clean
48-
clean:
49-
$(GO) clean
50-
rm -rf $(BUILD_DIR) $(DIST_DIR)
44+
test -z "$$($(GOFMT) -l .)" # Check Code is formatted correctly
45+
$(GO) mod verify # ensure that go.sum agrees with what's in the module cache
46+
$(GO) vet $(PACKAGES) # examines Go source code and reports suspicious constructs
5147

52-
.PHONY: compile
53-
compile:
54-
# FreeBDS
55-
GOOS=freebsd GOARCH=amd64 $(GO) build $(GO_LDFLAGS) -o $(BUILD_DIR)/$(APPNAME)-$(VERSION)-freebsd-amd64/$(APPNAME) -v ./...
56-
# MacOS
57-
GOOS=darwin GOARCH=amd64 $(GO) build $(GO_LDFLAGS) -o $(BUILD_DIR)/$(APPNAME)-$(VERSION)-darwin-amd64/$(APPNAME) -v ./...
58-
# MacOS M1
59-
GOOS=darwin GOARCH=arm64 $(GO) build $(GO_LDFLAGS) -o $(BUILD_DIR)/$(APPNAME)-$(VERSION)-darwin-arm64/$(APPNAME) -v ./...
60-
# Linux
61-
GOOS=linux GOARCH=amd64 $(GO) build $(GO_LDFLAGS) -o $(BUILD_DIR)/$(APPNAME)-$(VERSION)-linux-amd64/$(APPNAME) -v ./...
62-
# Windows
63-
GOOS=windows GOARCH=amd64 $(GO) build $(GO_LDFLAGS) -o $(BUILD_DIR)/$(APPNAME)-$(VERSION)-windows-amd64/$(APPNAME).exe -v ./...
48+
ifneq (,$(shell which $(STATICCHECK)))
49+
$(STATICCHECK) $(PACKAGES) # extensive analysis of Go code
50+
endif
6451

65-
.PHONY: dist
66-
dist: clean compile
67-
mkdir -p $(DIST_DIR)
52+
mkdir -p $(BUILD_DIR)
53+
# run unit test with coverage
54+
$(GO) test -race -coverprofile=$(BUILD_DIR)/test-coverage.out -json $(PACKAGES) > $(BUILD_DIR)/test-report-unit.json
55+
# run benchmark test once to make sure they work
56+
$(GO) test -run=- -bench=. -benchtime=1x -json $(PACKAGES) > $(BUILD_DIR)/test-report-benchmark.json
6857

69-
tar -C $(BUILD_DIR) -cvf $(BUILD_DIR)/$(APPNAME)-$(VERSION)-freebsd-amd64.tar $(APPNAME)-$(VERSION)-freebsd-amd64
70-
gzip $(BUILD_DIR)/$(APPNAME)-$(VERSION)-freebsd-amd64.tar
71-
mv $(BUILD_DIR)/$(APPNAME)-$(VERSION)-freebsd-amd64.tar.gz $(DIST_DIR)/$(APPNAME)-$(VERSION)-freebsd-amd64.tar.gz
58+
.PHONY: build
59+
build:
60+
@echo "Start build for: $(APPNAME) v$(VERSION)"
61+
62+
@if grep -q "package main" *.go 2>/dev/null ; then \
63+
mkdir -p $(BUILD_DIR); \
64+
for target in $(TARGETS); do \
65+
os=$$(echo $$target | cut -d/ -f1); \
66+
arch=$$(echo $$target | cut -d/ -f2); \
67+
GOOS=$$os GOARCH=$$arch $(GO) build $(GO_LDFLAGS) -o $(BUILD_DIR)/$(VERSION)-$$os-$$arch/ $(PACKAGES); \
68+
done; \
69+
else \
70+
echo "No building required, module does not contain the 'main' package."; \
71+
fi
72+
73+
.PHONY: dist-check
74+
dist-check:
75+
ifneq (,$(filter $(bamboo_planRepository_branchName),release/acceptance master))
76+
@echo "Performing dist check for: $(bamboo_planRepository_branchName)"
77+
78+
@if ! test -z "$$(sed -E -n '/(upcoming|unreleased)/I,/##/p' changelog.md | sed '1d;$$d' | sed 's/[[:space:]-]//g')"; then \
79+
echo "Error: cannot generate dist, changelog.md must not contain unreleased lines."; \
80+
exit 1; \
81+
fi
82+
83+
# Check for changes in go.mod or go.sum
84+
@mkdir -p $(BUILD_DIR)
85+
@cp go.mod $(BUILD_DIR)/go.mod.chk
86+
@cp go.sum $(BUILD_DIR)/go.sum.chk
87+
88+
test -z $$($(GO) mod tidy)
89+
90+
diff go.mod $(BUILD_DIR)/go.mod.chk
91+
diff go.sum $(BUILD_DIR)/go.sum.chk
92+
93+
@rm $(BUILD_DIR)/go.mod.chk $(BUILD_DIR)/go.sum.chk
94+
else
95+
@echo "Skipping dist check."
96+
endif
97+
98+
.PHONY: dist-create
99+
dist-create:
100+
@echo "Create dist for: $(APPNAME) v$(VERSION)"
101+
102+
@if grep -q "package main" *.go 2>/dev/null ; then \
103+
mkdir -p $(DIST_DIR); \
104+
for target in $(TARGETS); do \
105+
os=$$(echo $$target | cut -d/ -f1); \
106+
arch=$$(echo $$target | cut -d/ -f2); \
107+
tar -C $(BUILD_DIR) -cvzf $(DIST_DIR)/$(APPNAME)-$(VERSION)-$$os-$$arch.tar.gz $(VERSION)-$$os-$$arch; \
108+
done; \
109+
fi
72110

73-
tar -C $(BUILD_DIR) -cvf $(BUILD_DIR)/$(APPNAME)-$(VERSION)-darwin-amd64.tar $(APPNAME)-$(VERSION)-darwin-amd64
74-
gzip $(BUILD_DIR)/$(APPNAME)-$(VERSION)-darwin-amd64.tar
75-
mv $(BUILD_DIR)/$(APPNAME)-$(VERSION)-darwin-amd64.tar.gz $(DIST_DIR)/$(APPNAME)-$(VERSION)-darwin-amd64.tar.gz
111+
.PHONY: dist
112+
dist: clean dist-check build dist-create
113+
114+
.PHONY: update-dependencies
115+
update-dependencies:
116+
$(GO) get go@latest
117+
# Remove patch level of GO version
118+
@sed -i "" -E 's/(go [0-9]+\.[0-9]+)\.[0-9]+/\1/' go.mod
119+
$(GO) get -t -u $(PACKAGES)
120+
$(GO) mod tidy
76121

77-
tar -C $(BUILD_DIR) -cvf $(BUILD_DIR)/$(APPNAME)-$(VERSION)-darwin-arm64.tar $(APPNAME)-$(VERSION)-darwin-arm64
78-
gzip $(BUILD_DIR)/$(APPNAME)-$(VERSION)-darwin-arm64.tar
79-
mv $(BUILD_DIR)/$(APPNAME)-$(VERSION)-darwin-arm64.tar.gz $(DIST_DIR)/$(APPNAME)-$(VERSION)-darwin-arm64.tar.gz
122+
# Adding lines to changelog
123+
@sed -i "" -e 's/\(## \[$(VERSION)\]\)/$(CHANGELOG_LINES)\1/' $(CHANGELOG)
80124

81-
tar -C $(BUILD_DIR) -cvf $(BUILD_DIR)/$(APPNAME)-$(VERSION)-linux-amd64.tar $(APPNAME)-$(VERSION)-linux-amd64
82-
gzip $(BUILD_DIR)/$(APPNAME)-$(VERSION)-linux-amd64.tar
83-
mv $(BUILD_DIR)/$(APPNAME)-$(VERSION)-linux-amd64.tar.gz $(DIST_DIR)/$(APPNAME)-$(VERSION)-linux-amd64.tar.gz
125+
.PHONY: update
126+
update: clean update-dependencies test
84127

85-
tar -C $(BUILD_DIR) -cvf $(BUILD_DIR)/$(APPNAME)-$(VERSION)-windows-amd64.tar $(APPNAME)-$(VERSION)-windows-amd64
86-
gzip $(BUILD_DIR)/$(APPNAME)-$(VERSION)-windows-amd64.tar
87-
mv $(BUILD_DIR)/$(APPNAME)-$(VERSION)-windows-amd64.tar.gz $(DIST_DIR)/$(APPNAME)-$(VERSION)-windows-amd64.tar.gz
128+
.PHONY: clean
129+
clean:
130+
$(GO) clean
131+
rm -rf $(BUILD_DIR) $(DIST_DIR)

VERSION

Lines changed: 0 additions & 1 deletion
This file was deleted.

config.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"flag"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
"path"
98
"regexp"
@@ -80,7 +79,6 @@ type probeArgument struct {
8079
var restrictedParams = []string{"module", "debug"}
8180

8281
var config internalConfig
83-
var probes []string
8482

8583
func setup() {
8684
// retrieve flags since that could contain the config folder
@@ -107,7 +105,7 @@ func setup() {
107105
func readConfig() {
108106
log.Infof("Looking for configuration files in: %s", *flags.configDir)
109107

110-
files, err := ioutil.ReadDir(*flags.configDir)
108+
files, err := os.ReadDir(*flags.configDir)
111109
if err != nil {
112110
log.Fatal("Could not load config files: ", err)
113111
}
@@ -118,7 +116,7 @@ func readConfig() {
118116
if !file.IsDir() && (strings.HasSuffix(file.Name(), ".yaml") || strings.HasSuffix(file.Name(), ".yml")) {
119117
log.Debug("[readConfig] loading config file: ", file.Name())
120118

121-
yamlFile, err := ioutil.ReadFile(
119+
yamlFile, err := os.ReadFile(
122120
path.Join(*flags.configDir, file.Name()),
123121
)
124122
if err != nil {
@@ -170,7 +168,8 @@ func configProbes(probesConfig YamlProbeConfig, fileName string) {
170168
for _, probe := range probesConfig {
171169
log.Debug("[configProbes] found probe: ", probe.Name)
172170

173-
if match, _ := regexp.MatchString("^[a-zA-Z0-9:_]+$", probe.Name); !match {
171+
matchName := regexp.MustCompile("^[a-zA-Z0-9:_]+$")
172+
if match := matchName.MatchString(probe.Name); !match {
174173
log.Fatalf("Config failure probe with name '%s' name must match ^[a-zA-Z0-9:_]+$ (%s)", probe.Name, fileName)
175174
}
176175

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module mostwanted.io/prometheus/script_exporter
22

3-
go 1.20
3+
go 1.21
44

55
require (
66
github.com/gorilla/handlers v1.5.2

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
99
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
1010
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
1111
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
12+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
1213
github.com/gorilla/handlers v1.5.2 h1:cLTUSsNkgcwhgRqvCNmdbRWG0A3N4F+M2nWKdScwyEE=
1314
github.com/gorilla/handlers v1.5.2/go.mod h1:dX+xVpaxdSw+q0Qek8SSsl3dfMk3jNddUkMzo0GtH0w=
1415
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
16+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
1517
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
1618
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
1719
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
2729
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
2830
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
2931
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
32+
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
3033
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
3134
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
3235
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
4043
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
4144
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4245
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
46+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
4347
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4448
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
4549
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func main() {
2929
log.Info("Started on ", addr)
3030

3131
r := http.NewServeMux()
32-
32+
3333
r.HandleFunc("/", landingpage)
3434
r.Handle("/metrics", promhttp.Handler())
3535
r.HandleFunc("/probe", probe)

0 commit comments

Comments
 (0)