Skip to content

Commit 35121c7

Browse files
authored
makefile updates (#118)
* refactor silence msg handling and add tests Signed-off-by: Chris Collins <collins.christopher@gmail.com> * Improve handling of incident wait msg Improves the waitForIncidentThenDo function, removing the unnecessary sleep that may have been causing slow responsiveness in the TUI, and consolidating specific "wait" functions into the generic waitForSelectedIncidentThenDo function. Signed-off-by: Chris Collins <collins.christopher@gmail.com> * Enable journal/system logging Enable logging to the system log location (/var/log, or Mac equiv.) or the systemd journal if using systemd. Signed-off-by: Chris Collins <collins.christopher@gmail.com> * Update and cleanup Makefile Update and cleanup makefile Signed-off-by: Chris Collins <collins.christopher@gmail.com> --------- Signed-off-by: Chris Collins <collins.christopher@gmail.com>
1 parent 0aa1209 commit 35121c7

File tree

6 files changed

+174
-41
lines changed

6 files changed

+174
-41
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ log/
88
# Build and goreleaser output dirs
99
dist/
1010
build/
11+
12+
coverage.out

Makefile

Lines changed: 68 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,102 @@
11
unexport GOFLAGS
22

3-
GOOS?=linux
4-
TESTOPTS ?=
5-
GOARCH?=amd64
6-
GOENV=GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=0 GOFLAGS=
3+
GOOS ?= linux
4+
GOARCH ?= amd64
5+
GOENV = GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=0 GOFLAGS=
76
GOPATH := $(shell go env GOPATH | awk -F: '{print $1}')
8-
HOME?=$(shell mktemp -d)
7+
BIN_DIR := $(GOPATH)/bin
8+
HOME ?= $(shell echo ~)
99

10-
GOLANGCI_LINT_VERSION=v2.1.5
11-
12-
GORELEASER_VERSION=v2.8.2
10+
GOLANGCI_LINT_VERSION = v2.1.5
11+
GORELEASER_VERSION = v2.8.2
1312

1413
# Ensure go modules are enabled:
15-
export GO111MODULE=on
16-
export GOPROXY=https://proxy.golang.org
14+
export GO111MODULE = on
15+
export GOPROXY = https://proxy.golang.org
1716

1817
# Disable CGO so that we always generate static binaries:
19-
export CGO_ENABLED=0
18+
export CGO_ENABLED = 0
19+
20+
.DEFAULT_GOAL := help
2021

21-
default: build
22+
.PHONY: help
23+
help: ## Show this help message
24+
@echo "Available targets:"
25+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-20s\033[0m %s\n", $$1, $$2}'
2226

2327
.PHONY: build
24-
build:
25-
$(GOENV) go build -o build/$(PROJECT_NAME) .
28+
build: ## Build the application
29+
@echo "Building the application..."
30+
goreleaser build --snapshot --clean --single-target
2631

2732
.PHONY: install
28-
install: build
29-
cp build/srepd ~/.local/bin/$(PROJECT_NAME)
30-
31-
.PHONY: mod
32-
mod:
33+
install: ## Install the application to $(GOPATH)/bin
34+
@echo "Installing the application..."
35+
go build -o ${BIN_DIR}/srepd .
36+
37+
.PHONY: install-local
38+
install-local: build ## Install the application locally to ~/.local/bin
39+
@echo "Installing the application locally..."
40+
cp dist/*/srepd $(HOME)/.local/bin/srepd
41+
42+
.PHONY: tidy
43+
tidy: ## Tidy up go modules
44+
@echo "Tidying up go modules..."
3345
go mod tidy
3446

3547
.PHONY: test
36-
test:
48+
test: ## Run tests
49+
@echo "Running tests..."
3750
go test ./... -v $(TESTOPTS)
3851

3952
.PHONY: coverage
40-
coverage:
53+
coverage: ## Generate test coverage report
54+
@echo "Generating test coverage report..."
4155
hack/codecov.sh
4256

43-
# Installed using instructions from: https://golangci-lint.run/usage/install/#linux-and-windows
44-
getlint:
45-
@mkdir -p $(GOPATH)/bin
46-
@ls $(GOPATH)/bin/golangci-lint 1>/dev/null 2>&1 || (echo "Installing golangci-lint..." && curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin $(GOLANGCI_LINT_VERSION))
57+
.PHONY: getlint
58+
getlint: ## Install golangci-lint if not already installed
59+
@echo "Checking for golangci-lint..."
60+
@which golangci-lint >/dev/null 2>&1 || (echo "Installing golangci-lint..." && curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(BIN_DIR) $(GOLANGCI_LINT_VERSION))
4761

4862
.PHONY: lint
49-
lint: getlint
50-
$(GOPATH)/bin/golangci-lint --version
51-
$(GOPATH)/bin/golangci-lint run --timeout 5m
63+
lint: getlint ## Run golangci-lint
64+
@echo "Running golangci-lint..."
65+
$(BIN_DIR)/golangci-lint --version
66+
$(BIN_DIR)/golangci-lint run --timeout 5m
67+
68+
.PHONY: vet
69+
vet: ## Run go vet to catch common mistakes
70+
@echo "Running go vet..."
71+
go vet ./...
72+
73+
.PHONY: check
74+
check: fmt lint vet ## Run all code checks (fmt, lint, vet)
75+
@echo "Running all code checks..."
5276

5377
.PHONY: ensure-goreleaser
54-
ensure-goreleaser:
55-
@ls $(GOPATH)/bin/goreleaser 1>/dev/null 2>&1 || go install github.com/goreleaser/goreleaser/v2@${GORELEASER_VERSION}
78+
ensure-goreleaser: ## Ensure goreleaser is installed
79+
@echo "Checking for goreleaser..."
80+
@which goreleaser >/dev/null 2>&1 || go install github.com/goreleaser/goreleaser/v2@${GORELEASER_VERSION}
5681

5782
.PHONY: release
58-
release: ensure-goreleaser
83+
release: ensure-goreleaser ## Create a release using goreleaser
84+
@echo "Creating a release..."
5985
GITHUB_TOKEN=$$(jq -r .goreleaser_token ~/.config/goreleaser/goreleaser_token) && \
6086
export GITHUB_TOKEN && \
6187
goreleaser release --clean
6288

6389
.PHONY: fmt
64-
fmt:
65-
gofmt -s -l -w cmd pkg tests
90+
fmt: ## Format the code
91+
@echo "Formatting the code..."
92+
gofmt -s -l -w cmd pkg
6693

6794
.PHONY: clean
68-
clean:
69-
rm -rf \
70-
build/*
71-
72-
rm -rf \
73-
dist/*
95+
clean: ## Clean up build artifacts
96+
@echo "Cleaning up build artifacts..."
97+
rm -rf build/* dist/*
98+
99+
.PHONY: run
100+
run: ## Run the application locally
101+
@echo "Running the application..."
102+
go run main.go

cmd/root.go

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ package cmd
2525
import (
2626
"fmt"
2727
"os"
28+
"runtime"
2829
"strconv"
30+
"strings"
2931
"time"
3032

3133
tea "github.com/charmbracelet/bubbletea"
3234
"github.com/charmbracelet/log"
3335
"github.com/clcollins/srepd/pkg/launcher"
3436
"github.com/clcollins/srepd/pkg/tui"
37+
"github.com/coreos/go-systemd/journal"
3538
"github.com/spf13/cobra"
3639
"github.com/spf13/viper"
3740
)
@@ -136,7 +139,7 @@ func (f cliFlag) BoolValue() bool {
136139

137140
func init() {
138141
var flags = []cliFlag{
139-
{"bool", "debug", "d", "false", "enable debug logging (~/.config/srepd/debug.log)"},
142+
{"bool", "debug", "d", "false", "enable debug logging"},
140143
// TODO - For some reason the parsed cluster-login-command flag does not work (the "%%" is stripped out)
141144
// Commenting out the config options for now, as the config file is the preferred method
142145
// {"string", "token", "T", "", "PagerDuty API token"},
@@ -148,7 +151,7 @@ func init() {
148151
// {"stringSlice", "cluster-login-command", "c", defaultClusterLoginCmd, "cluster login command"},
149152
}
150153

151-
cobra.OnInitialize(initConfig)
154+
cobra.OnInitialize(initConfig, configureLogging)
152155

153156
for _, f := range flags {
154157
switch f.flagType {
@@ -190,3 +193,58 @@ func initConfig() {
190193
}
191194
}
192195
}
196+
197+
func configureLogging() {
198+
log.SetPrefix("srepd")
199+
200+
switch runtime.GOOS {
201+
case "linux":
202+
// Check if running under systemd
203+
if journal.Enabled() {
204+
log.SetOutput(journalWriter{})
205+
log.Info("Logging to systemd journal")
206+
return
207+
}
208+
209+
// Fallback to /var/log/srepd.log for non-systemd Linux
210+
logFile := "/var/log/srepd.log"
211+
setupFileLogging(logFile)
212+
log.Info("Logging to /var/log/srepd.log")
213+
214+
case "darwin":
215+
// macOS: Log to ~/Library/Logs/srepd.log
216+
home, err := os.UserHomeDir()
217+
if err != nil {
218+
log.Fatal("Failed to get user home directory:", err)
219+
}
220+
logFile := home + "/Library/Logs/srepd.log"
221+
setupFileLogging(logFile)
222+
log.Info("Logging to ~/Library/Logs/srepd.log")
223+
224+
default:
225+
// Default fallback for other OSes
226+
log.SetOutput(os.Stderr)
227+
log.Warn("Unsupported OS: logging to stderr")
228+
}
229+
}
230+
231+
// setupFileLogging configures logging to a file
232+
func setupFileLogging(filePath string) {
233+
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
234+
if err != nil {
235+
log.Fatal("Failed to open log file:", err)
236+
}
237+
log.SetOutput(file)
238+
}
239+
240+
// journalWriter implements io.Writer for systemd journal
241+
type journalWriter struct{}
242+
243+
func (jw journalWriter) Write(p []byte) (n int, err error) {
244+
message := strings.TrimSpace(string(p))
245+
err = journal.Send(message, journal.PriInfo, nil)
246+
if err != nil {
247+
return 0, err
248+
}
249+
return len(p), nil
250+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/charmbracelet/glamour v0.10.0
1010
github.com/charmbracelet/lipgloss v1.1.1-0.20250404203927-76690c660834
1111
github.com/charmbracelet/log v0.4.1
12+
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
1213
github.com/spf13/cobra v1.9.1
1314
github.com/spf13/viper v1.20.1
1415
github.com/stretchr/testify v1.10.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ github.com/charmbracelet/x/exp/slice v0.0.0-20250424195755-e256bf9b4ee5 h1:OgXxQ
3636
github.com/charmbracelet/x/exp/slice v0.0.0-20250424195755-e256bf9b4ee5/go.mod h1:vI5nDVMWi6veaYH+0Fmvpbe/+cv/iJfMntdh+N0+Tms=
3737
github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=
3838
github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
39+
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf h1:iW4rZ826su+pqaw19uhpSCzhj44qo35pNgKFGqzDKkU=
40+
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
3941
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
4042
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
4143
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

hack/codecov.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
3+
# filepath: /home/chcollin/Projects/github.com/clcollins/srepd/hack/codecov.sh
4+
5+
set -euo pipefail
6+
7+
# Ensure the script is run from the project root
8+
PROJECT_ROOT=$(git rev-parse --show-toplevel)
9+
cd "$PROJECT_ROOT"
10+
11+
# Output file for coverage
12+
COVERAGE_FILE="coverage.out"
13+
14+
# Generate test coverage report
15+
echo "Generating test coverage report..."
16+
go test ./... -coverprofile="$COVERAGE_FILE" -covermode=atomic
17+
18+
# Check if the coverage file was generated
19+
if [ ! -f "$COVERAGE_FILE" ]; then
20+
echo "Error: Coverage file not generated."
21+
exit 1
22+
fi
23+
24+
# Display coverage summary
25+
echo "Coverage summary:"
26+
go tool cover -func="$COVERAGE_FILE"
27+
28+
# Upload coverage to Codecov if the codecov CLI is installed
29+
if command -v codecov >/dev/null 2>&1; then
30+
echo "Uploading coverage report to Codecov..."
31+
codecov -f "$COVERAGE_FILE"
32+
else
33+
echo "Codecov CLI not found. Skipping upload."
34+
echo "You can install it from https://docs.codecov.io/docs/codecov-cli."
35+
fi
36+
37+
# Clean up
38+
echo "Cleaning up..."
39+
rm -f "$COVERAGE_FILE"
40+
41+
echo "Test coverage report generation complete."

0 commit comments

Comments
 (0)