-
Notifications
You must be signed in to change notification settings - Fork 13
/
Makefile
146 lines (118 loc) · 4.28 KB
/
Makefile
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
GITHUB_ORG = michael-doubez
GITHUB_REPO = filestat_exporter
VERSION ?= $(shell git describe --tags --match="v[0-9]*")
# Binary build parameters
# - build in release mode
RELEASE_MODE ?= 0
# Go projet
GO ?= go
GOBIN ?= $(shell $(GO) env GOBIN)
ifeq ($(GOBIN),)
GOPATH ?= $(shell $(GO) env GOPATH)
ifeq ($(GOPATH),)
$(error Expecting GOPATH to be set)
endif
GOBIN = $(GOPATH)/bin
endif
GOLINT = $(shell ls $(GOBIN)/staticcheck 2>/dev/null || true)
# Inject version information
BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
REVISION ?= $(shell git rev-parse --short HEAD)
BUILDUSER ?= $(USER)
BUILDDATE ?= $(shell date +%FT%T%z)
LDFLAGS = -X github.com/prometheus/common/version.Version=$(VERSION) \
-X github.com/prometheus/common/version.Branch=$(BRANCH) \
-X github.com/prometheus/common/version.Revision=$(REVISION) \
-X github.com/prometheus/common/version.BuildUser=$(BUILDUSER) \
-X github.com/prometheus/common/version.BuildDate=$(BUILDDATE)
# Distribution
DIST_DIR?=./dist
EXPORTER=filestat_exporter
DIST_ARCHITECTURES=darwin-amd64 linux-amd64 windows-amd64 linux-arm64
# Main source files
SRC_MAIN = cmd/filestat_exporter.go
SRCS = $(SRC_MAIN) $(wildcard internal/*/*.go)
# ------------------------------------------------------------------------
# Main targets
# - all: check code and build it
# - build: build exporter for current platform
# - clean: remove build files
# - check: run all following checks
# - fmt: run formating check
# - vet: vetting code
# - lint: run static check
# - test: run unit tests
# - dist: build distribution packages
# - dist-linux-amd64/dist-darwin-amd64/...: distribution for arch
# - run: launch exporter on sample config
# - version: display version number
.PHONY: all build clean check dist fmt vet test lint run dist dist-% docker-build docker-tag docker-push
all:: check build
build: $(EXPORTER)
clean:
@rm -f $(EXPORTER)
check: fmt vet lint test
fmt:
@$(GO) fmt ./...
vet:
@$(GO) vet ./...
test:
@$(GO) test -cover ./internal/...
lint:
ifeq ($(GOLINT),)
@echo >&2 "Warning: staticcheck not installed - lint skipped"
@echo >&2 " run 'go install honnef.co/go/tools/cmd/staticcheck@latest' to install"
else
@$(GOLINT) ./...
endif
RUN_OPTIONS=-log.level=debug -metric.crc32 -metric.nb_lines -tree.name debug -tree.root /
RUN_PATTERN?='/etc/*.conf'
run:
@$(GO) run $(SRC_MAIN) $(RUN_OPTIONS) $(RUN_PATTERN)
version:
@echo $(VERSION)
DIST_EXPORTER=$(DIST_DIR)/$(EXPORTER)-$(VERSION)
dist: $(foreach ARCH, $(DIST_ARCHITECTURES), $(DIST_EXPORTER).$(ARCH).tar.gz)
dist-%: $(DIST_EXPORTER).%.tar.gz
@echo "Done generating $(notdir $<)"
.PRECIOUS: $(DIST_EXPORTER).%.tar.gz
# ------------------------------------------------------------------------
# Build and package exporter
# List of files to include in packages
PACKAGE_FILES = LICENSE NOTICE filestat.yaml
# In release mode
# - build without debug symbol
ifneq ($(RELEASE_MODE),0)
LDFLAGS += -s
LDFLAGS += -w
BUILD_FLAGS += -trimpath
endif
# Simple build for current os/architecture
$(EXPORTER): $(SRCS)
@$(GO) build -ldflags "$(LDFLAGS)" -o $@ $(BUILD_FLAGS) $<
# Ensure dist path exists
$(DIST_EXPORTER)/:
@mkdir -p $@
# Package distribution file
$(DIST_EXPORTER).%.tar.gz: $(DIST_EXPORTER).%/$(EXPORTER) $(PACKAGE_FILES)
@echo "Packaging $(notdir $@)"
@cp -f $(PACKAGE_FILES) $(DIST_EXPORTER).$*/
@cd $(DIST_DIR) ; tar czf $(abspath $@) $(notdir $(DIST_EXPORTER)).$*/
@rm -rf $(DIST_EXPORTER).$*/
# Generating exporter for archi
$(DIST_EXPORTER).%/$(EXPORTER): $(DIST_EXPORTER)/
$(DIST_EXPORTER).%/$(EXPORTER): GOOS=$(word 1,$(subst -, ,$*))
$(DIST_EXPORTER).%/$(EXPORTER): GOARCH=$(word 2,$(subst -, ,$*))
$(DIST_EXPORTER).%/$(EXPORTER): $(SRCS)
@echo "Building $(notdir $@) GOOS=$(GOOS) GOARCH=$(GOARCH)"
@GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO) build -ldflags "$(LDFLAGS)" -o $@ $(SRC_MAIN)
# ------------------------------------------------------------------------
# Docker build of image
docker-build:
docker build -t filestat_exporter:$(VERSION:v%=%) .
docker-tag:
docker tag filestat_exporter:$(VERSION:v%=%) mdoubez/filestat_exporter:$(VERSION:v%=%)
docker tag filestat_exporter:$(VERSION:v%=%) mdoubez/filestat_exporter:latest
docker-push:
docker push mdoubez/filestat_exporter:$(VERSION:v%=%)
docker push mdoubez/filestat_exporter:latest