Skip to content

Commit 94fcca8

Browse files
committed
chore(scripts): clean up devbox-init and common.sh with reusable helpers
1 parent ac0fdb3 commit 94fcca8

3 files changed

Lines changed: 145 additions & 160 deletions

File tree

justfile

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ buildflags := "-trimpath"
3232
default:
3333
@just --list
3434

35+
# === Build Recipes ===
36+
37+
# Build the binary with optimizations (reduced size)
38+
build:
39+
@. {{ logger }} && log_info "Building optimized binary..."
40+
mkdir -p {{ build_dir }}
41+
{{ gobuild }} {{ buildflags }} -ldflags="{{ ldflags }}" -o {{ build_dir }}/{{ app_name }} ./{{ cmd_dir }}
42+
@. {{ logger }} && log_info "Binary size:"
43+
@ls -lh {{ build_dir }}/{{ app_name }} | awk '{print " " $5}'
44+
45+
# Install the binary using Go install
46+
install: check test-force
47+
@. {{ logger }} && log_info "Install the binary using Go install"
48+
cd {{ cmd_dir }} && {{ go }} install {{ buildflags }} -ldflags="{{ ldflags }}" .
49+
3550
# Clean and build
3651
all: clean build
3752

@@ -42,16 +57,18 @@ clean:
4257
rm -f coverage.out coverage.html
4358
{{ goclean }} -cache
4459

60+
# === Code Quality ===
61+
62+
# Format code
63+
fmt:
64+
@. {{ logger }} && log_info "Running fmt and gofumpt"
65+
{{ go }} fmt ./...
66+
4567
# Run go-modernize with auto-fix
4668
modernize:
4769
@. {{ logger }} && log_info "Running go-modernize"
4870
modernize --fix ./...
4971

50-
# Run govulncheck
51-
security-scan:
52-
@. {{ logger }} && log_info "Running govulncheck"
53-
govulncheck ./...
54-
5572
# Run golangci-lint
5673
lint:
5774
@. {{ logger }} && log_info "Running golangci-lint"
@@ -62,6 +79,26 @@ reportcard:
6279
@. {{ logger }} && log_info "Running goreportcard-cli"
6380
goreportcard-cli -v
6481

82+
# Run govulncheck
83+
security-scan:
84+
@. {{ logger }} && log_info "Running govulncheck"
85+
govulncheck ./...
86+
87+
# Run modernize, lint, and reportcard
88+
check: fmt modernize lint reportcard
89+
90+
# Run go mod tidy
91+
tidy:
92+
@. {{ logger }} && log_info "Running go mod tidy"
93+
{{ go }} mod tidy
94+
95+
# Run go mod download
96+
deps:
97+
@. {{ logger }} && log_info "Running go mod download"
98+
{{ go }} mod download
99+
100+
# === Test Recipes ===
101+
65102
# Run all tests and print code coverage value
66103
test:
67104
@. {{ logger }} && log_info "Run all tests"
@@ -85,18 +122,10 @@ test-race:
85122
@. {{ logger }} && log_info "Running tests with race detector"
86123
{{ go }} test -race $({{ go }} list ./... | grep -Ev 'internal/testutils')
87124

88-
# Run modernize, lint, and reportcard
89-
check: modernize lint reportcard
90-
91-
# Build the binary with optimizations (reduced size)
92-
build:
93-
@. {{ logger }} && log_info "Building optimized binary..."
94-
mkdir -p {{ build_dir }}
95-
{{ gobuild }} {{ buildflags }} -ldflags="{{ ldflags }}" -o {{ build_dir }}/{{ app_name }} ./{{ cmd_dir }}
96-
@. {{ logger }} && log_info "Binary size:"
97-
@ls -lh {{ build_dir }}/{{ app_name }} | awk '{print " " $5}'
125+
# === Utilities ===
98126

99-
# Install the binary using Go install
100-
install: check test-force
101-
@. {{ logger }} && log_info "Install the binary using Go install"
102-
cd {{ cmd_dir }} && {{ go }} install {{ buildflags }} -ldflags="{{ ldflags }}" .
127+
# Update dependencies
128+
deps-update:
129+
@. {{ logger }} && log_info "Running go update deps"
130+
{{ go }} get -u ./...
131+
{{ go }} mod tidy

scripts/devbox-init.sh

Lines changed: 69 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,93 @@
11
#!/usr/bin/env bash
2-
# devbox-init.sh - one-time setup for local dev environments
2+
# devbox-init.sh - Development environment setup for sley CLI
3+
# Called automatically by devbox shell init_hook
4+
35
set -eu
46

5-
# -------- Config --------
6-
. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"/lib/common.sh
7+
# Load common utilities
8+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9+
# shellcheck source=lib/common.sh
10+
source "${SCRIPT_DIR}/lib/common.sh"
711

8-
# -------- Start --------
9-
h1 'Welcome to the sley devbox!'
12+
h1 "sley CLI - Development Environment Setup"
1013

11-
log_default ""
14+
# === Go Dependencies ===
15+
h2 "Go Dependencies"
1216

13-
# Go dependencies and tools
14-
log_info 'Setting up Go dependencies and tools...'
15-
if [ -f "go.mod" ]; then
16-
log_info 'Downloading Go modules...'
17-
(go mod download)
18-
log_success 'Go modules downloaded'
17+
if command_exists go; then
18+
if [ -f "go.mod" ]; then
19+
log_info "Downloading Go modules..."
20+
go mod download
21+
log_success "Go modules downloaded"
22+
else
23+
log_warning "go.mod not found - skipping Go module download"
24+
fi
1925
else
20-
log_warning 'go.mod not found - skipping Go module download'
26+
log_warning "Go not found, skipping module download"
2127
fi
2228

23-
if command -v go >/dev/null 2>&1; then
24-
log_info 'Installing Go tools...'
29+
# === Go Tools ===
30+
h2 "Go Tools"
2531

26-
if go install golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest; then
27-
log_success 'go-modernize installed'
28-
else
29-
log_warning 'Failed to install go-modernize'
30-
fi
32+
if command_exists go; then
33+
install_go_tool "modernize" "golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest"
34+
install_go_tool "govulncheck" "golang.org/x/vuln/cmd/govulncheck@latest"
3135

32-
if go install golang.org/x/vuln/cmd/govulncheck@latest; then
33-
log_success 'govulncheck installed'
34-
else
35-
log_warning 'Failed to install govulncheck'
36-
fi
37-
38-
# goreportcard-cli requires manual installation:
39-
# git clone https://github.com/gojp/goreportcard.git && cd goreportcard && make install && go install ./cmd/goreportcard-cli
40-
if command -v goreportcard-cli >/dev/null 2>&1; then
41-
log_success 'goreportcard-cli already installed'
42-
else
43-
log_faint 'goreportcard-cli not installed (optional) - see: https://github.com/gojp/goreportcard'
44-
fi
36+
# goreportcard-cli requires manual installation:
37+
# git clone https://github.com/gojp/goreportcard.git && cd goreportcard && make install && go install ./cmd/goreportcard-cli
38+
if command_exists goreportcard-cli; then
39+
log_faint "goreportcard-cli already installed"
40+
else
41+
log_faint "goreportcard-cli not installed (optional) - see: https://github.com/gojp/goreportcard"
42+
fi
4543
else
46-
log_warning 'Go not available - skipping Go tools installation'
44+
log_warning "Go not available - skipping Go tools installation"
4745
fi
4846

49-
log_default ""
47+
# === Git Hooks ===
48+
h2 "Git Hooks"
5049

51-
# Git hooks (prek)
52-
log_info 'Setting up Git hooks with prek...'
5350
# Ensure custom hooks are executable
54-
if [ -f scripts/githooks/commit-msg ]; then
55-
chmod +x scripts/githooks/commit-msg
56-
fi
57-
if [ -f scripts/githooks/pre-push ]; then
58-
chmod +x scripts/githooks/pre-push
59-
fi
60-
log_success 'Custom hooks made executable'
51+
for hook in scripts/githooks/commit-msg scripts/githooks/pre-push; do
52+
if [ -f "$hook" ]; then
53+
chmod +x "$hook"
54+
fi
55+
done
56+
log_success "Custom hooks made executable"
6157

6258
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
63-
if command -v prek >/dev/null 2>&1; then
64-
# Install prek hooks for commit-msg and pre-push stages
65-
if prek install --hook-type commit-msg --hook-type pre-push; then
66-
log_success 'prek hooks installed (commit-msg, pre-push)'
59+
if command_exists prek; then
60+
log_info "Installing git hooks via prek..."
61+
prek install --hook-type commit-msg --hook-type pre-push && log_success "Git hooks installed (commit-msg, pre-push)" || log_warning "Failed to install git hooks"
6762
else
68-
log_warning 'Failed to install prek hooks'
63+
log_warning "prek not found - run: cargo install prek"
6964
fi
70-
else
71-
log_warning 'prek not installed - run: cargo install prek'
72-
fi
7365
else
74-
log_warning 'not a git repository - skipping hooks installation'
66+
log_warning "Not a git repository - skipping hooks installation"
7567
fi
7668

77-
log_default ""
78-
# Helpful commands
79-
h3 'Available just commands:'
80-
cat <<'TXT'
81-
just help - Show help message
82-
just all - Clean and build
83-
just clean - Clean the build directory and Go cache
84-
just test - Run all tests and generate coverage report
85-
just test-force - Clean go tests cache and run all tests
86-
just modernize - Run go-modernize with auto-fix
87-
just check - Run modernize, lint, and test
88-
just lint - Run golangci-lint
89-
just build - Build the binary to build/sley
90-
just install - Install the binary using Go install
91-
92-
Quick start: `just check` to run all quality checks!
93-
TXT
69+
# === Summary ===
70+
h1 "Setup Complete"
9471

95-
# End
96-
printf '\n%s\n\n' '===================================='
72+
log_default ""
73+
log_info "Available commands:"
74+
log_faint " just build - Build the binary with optimizations (reduced size)"
75+
log_faint " just install - Install the binary using Go install"
76+
log_faint " just clean - Clean the build directory and Go cache"
77+
log_faint " just all - Clean and build"
78+
log_faint " just fmt - Format code"
79+
log_faint " just modernize - Run go-modernize with auto-fix"
80+
log_faint " just lint - Run golangci-lint"
81+
log_faint " just reportcard - Run goreportcard-cli"
82+
log_faint " just check - Run modernize, lint, and reportcard"
83+
log_faint " just security-scan - Run govulncheck"
84+
log_faint " just test - Run all tests and print code coverage value"
85+
log_faint " just test-force - Clean go tests cache and run all tests"
86+
log_faint " just test-coverage - Run all tests and generate coverage report"
87+
log_faint " just test-race - Run all tests with race detector"
88+
log_faint " just deps - Run go mod download"
89+
log_faint " just deps-update - Update dependencies"
90+
log_faint " just tidy - Run go mod tidy"
91+
log_default ""
92+
log_faint "Quick start: just check to run all quality checks!"
93+
log_default ""

scripts/lib/common.sh

Lines changed: 28 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,46 @@
11
#!/usr/bin/env bash
2-
# Common utilities for all scripts
3-
# This file provides shared functionality across all shell scripts in the project
2+
# common.sh - Shared utilities for sley CLI scripts
3+
# Auto-loads logger.sh and provides common functions
44

5-
# -------- Auto-detect and load logger --------
6-
# This works from any script location in the project
5+
set -euo pipefail
76

8-
# Function to find the scripts directory from any location
7+
# Find the scripts directory (works from any location)
98
find_scripts_dir() {
10-
local current_dir="$1"
11-
while [ "$current_dir" != "/" ]; do
12-
if [ -d "$current_dir/scripts" ] && [ -f "$current_dir/scripts/lib/logger.sh" ]; then
13-
echo "$current_dir/scripts"
14-
return 0
15-
fi
16-
current_dir="$(dirname "$current_dir")"
17-
done
18-
return 1
19-
}
20-
21-
# Auto-load logger utility
22-
load_logger() {
23-
# Try to determine scripts directory relative to current script
24-
local script_path
25-
if [ -n "${BASH_SOURCE[0]}" ]; then
26-
script_path="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
27-
else
28-
script_path="$(pwd)"
29-
fi
30-
31-
# Find scripts directory
32-
local scripts_dir
33-
scripts_dir="$(find_scripts_dir "$script_path")"
9+
local dir
10+
dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
3411

35-
if [ -z "$scripts_dir" ]; then
36-
echo "ERROR: Could not locate scripts/lib/logger.sh" >&2
37-
exit 1
12+
# If we're in lib/, go up one level
13+
if [[ "$(basename "$dir")" == "lib" ]]; then
14+
dir="$(dirname "$dir")"
3815
fi
3916

40-
# Source the logger
41-
# shellcheck source=logger.sh
42-
. "$scripts_dir/lib/logger.sh"
43-
44-
# Export scripts directory for other uses
45-
export SCRIPTS_DIR="$scripts_dir"
17+
echo "$dir"
4618
}
4719

48-
# -------- Shared Utility Functions --------
49-
50-
# Check if a command exists
51-
require_command() {
52-
local cmd="$1"
53-
local install_msg="${2:-Install $cmd}"
54-
55-
if ! command -v "$cmd" >/dev/null 2>&1; then
56-
log_error "$cmd not found"
57-
log_faint "$install_msg"
58-
exit 1
20+
# Load logger if not already loaded
21+
load_logger() {
22+
if ! declare -f log_info >/dev/null 2>&1; then
23+
local scripts_dir
24+
scripts_dir="$(find_scripts_dir)"
25+
# shellcheck source=logger.sh
26+
source "${scripts_dir}/lib/logger.sh"
5927
fi
6028
}
6129

62-
# Check if a directory exists
63-
require_directory() {
64-
local dir="$1"
65-
local create_msg="${2:-Create directory: $dir}"
66-
67-
if [ ! -d "$dir" ]; then
68-
log_error "Directory not found: $dir"
69-
log_faint "$create_msg"
70-
exit 1
71-
fi
72-
}
30+
# -------- Shared Utility Functions --------
7331

74-
# Check if a file exists
75-
require_file() {
76-
local file="$1"
77-
local create_msg="${2:-Create file: $file}"
32+
# Install a Go tool if not already present
33+
install_go_tool() {
34+
local name="$1"
35+
local pkg="$2"
7836

79-
if [ ! -f "$file" ]; then
80-
log_error "File not found: $file"
81-
log_faint "$create_msg"
82-
exit 1
37+
if command_exists "$name"; then
38+
log_faint "$name already installed"
39+
else
40+
log_info "Installing $name..."
41+
go install "$pkg" && log_success "$name installed" || log_warning "Failed to install $name"
8342
fi
8443
}
8544

86-
# Automatically load logger when this file is sourced
45+
# Auto-load logger on source
8746
load_logger

0 commit comments

Comments
 (0)