Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make: generate auto-completion files #118

Merged
merged 3 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v3
- name: Install tools
run: |
sudo dnf -y install ShellCheck bats golang criu asciidoctor iptables iproute kmod jq
sudo dnf -y install ShellCheck bats golang criu asciidoctor iptables iproute kmod jq bash bash-completion zsh fish
sudo modprobe -va ip_tables ip6table_filter nf_conntrack nf_conntrack_netlink
- name: Run make shellcheck
run: make shellcheck
Expand All @@ -33,6 +33,8 @@ jobs:
path: test/junit.xml
- name: Run make install/uninstall
run: test/uninstall.sh
- name: Run make validate.completions
run: make validate.completions

event_file:
name: "Event File"
Expand Down
58 changes: 54 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ GO_SRC = $(shell find . -name \*.go)
GO_BUILD = $(GO) build
NAME = checkpointctl

BASHINSTALLDIR=${PREFIX}/share/bash-completion/completions
ZSHINSTALLDIR=${PREFIX}/share/zsh/site-functions
FISHINSTALLDIR=${PREFIX}/share/fish/vendor_completions.d

include Makefile.versions

COVERAGE_PATH ?= $(shell pwd)/.coverage
Expand All @@ -18,8 +22,10 @@ MIN_GO_MAJOR_VER = 1
MIN_GO_MINOR_VER = 20
GO_VALIDATION_ERR = Go version is not supported. Please update to at least $(MIN_GO_MAJOR_VER).$(MIN_GO_MINOR_VER)

.PHONY: all
all: $(NAME)

.PHONY: check-go-version
check-go-version:
@if [ $(GO_MAJOR_VER) -gt $(MIN_GO_MAJOR_VER) ]; then \
exit 0 ;\
Expand All @@ -41,62 +47,108 @@ $(NAME).coverage: check-go-version $(GO_SRC)
-o $@ \
-ldflags "-X main.name=$(NAME) -X main.version=${VERSION}"

.PHONY: release
release:
CGO_ENABLED=0 $(GO_BUILD) -o $(NAME) -ldflags "-X main.name=$(NAME) -X main.version=${VERSION}"

install: $(NAME)
.PHONY: install
install: $(NAME) install.completions
@echo " INSTALL " $<
@mkdir -p $(DESTDIR)$(BINDIR)
@install -m0755 $< $(DESTDIR)$(BINDIR)
@make -C docs install

uninstall:
.PHONY: uninstall
uninstall: uninstall.completions
@make -C docs uninstall
@echo " UNINSTALL" $(NAME)
@$(RM) $(addprefix $(DESTDIR)$(BINDIR)/,$(NAME))

.PHONY: clean
clean:
rm -f $(NAME) junit.xml $(NAME).coverage $(COVERAGE_PATH)/*
if [ -d $(COVERAGE_PATH) ]; then rmdir $(COVERAGE_PATH); fi
@make -C docs clean

.PHONY: golang-lint
golang-lint:
golangci-lint run

.PHONY: shellcheck
shellcheck:
shellcheck test/*bats

.PHONY: lint
lint: golang-lint shellcheck

.PHONY: test
test: $(NAME)
$(GO) test -v ./...
make -C test

.PHONY: test-junit
test-junit: $(NAME)
make -C test test-junit clean

.PHONY: coverage
coverage: check-go-version $(NAME).coverage
mkdir -p $(COVERAGE_PATH)
COVERAGE_PATH=$(COVERAGE_PATH) COVERAGE=1 make -C test
# Print coverage from this run
$(GO) tool covdata percent -i=${COVERAGE_PATH}
$(GO) tool covdata textfmt -i=${COVERAGE_PATH} -o ${COVERAGE_PATH}/coverage.out

.PHONY: codecov
codecov:
curl -Os https://uploader.codecov.io/latest/linux/codecov
chmod +x codecov
./codecov -f "$(COVERAGE_PATH)"/coverage.out

.PHONY: vendor
vendor:
go mod tidy
go mod vendor
go mod verify

.PHONY: docs
docs:
@make -C docs

.PHONY: completions
completions: $(NAME)
declare -A outfiles=([bash]=%s [zsh]=_%s [fish]=%s.fish);\
for shell in $${!outfiles[*]}; do \
outfile=$$(printf "completions/$$shell/$${outfiles[$$shell]}" $(NAME)); \
./$(NAME) completion $$shell >| $$outfile; \
done

.PHONY: validate.completions
validate.completions: SHELL:=/usr/bin/env bash # Set shell to bash for this target
validate.completions:
# Check if the files can be loaded by the shell
. completions/bash/$(NAME)
if [ -x /bin/zsh ]; then /bin/zsh completions/zsh/_$(NAME); fi
if [ -x /bin/fish ]; then /bin/fish completions/fish/$(NAME).fish; fi

.PHONY: install.completions
install.completions:
@install -d -m 755 ${DESTDIR}${BASHINSTALLDIR}
@install -m 644 completions/bash/$(NAME) ${DESTDIR}${BASHINSTALLDIR}
@install -d -m 755 ${DESTDIR}${ZSHINSTALLDIR}
@install -m 644 completions/zsh/_$(NAME) ${DESTDIR}${ZSHINSTALLDIR}
@install -d -m 755 ${DESTDIR}${FISHINSTALLDIR}
@install -m 644 completions/fish/$(NAME).fish ${DESTDIR}${FISHINSTALLDIR}

.PHONY: uninstall.completions
uninstall.completions:
@$(RM) $(addprefix ${DESTDIR}${BASHINSTALLDIR}/,$(NAME))
@$(RM) $(addprefix ${DESTDIR}${ZSHINSTALLDIR}/,_$(NAME))
@$(RM) $(addprefix ${DESTDIR}${FISHINSTALLDIR}/,$(NAME).fish)

.PHONY: help
help:
@echo "Usage: make <target>"
@echo " * completions - generate auto-completion files"
@echo " * clean - remove artifacts"
@echo " * docs - build man pages"
@echo " * lint - verify the source code (shellcheck/golangci-lint)"
Expand All @@ -111,5 +163,3 @@ help:
@echo " * uninstall - remove the installed binary from $(BINDIR)"
@echo " * release - build a static binary"
@echo " * help - show help"

.PHONY: clean docs install uninstall release lint golang-lint shellcheck vendor test help check-go-version test-junit
6 changes: 6 additions & 0 deletions completions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Shell completion scripts

checkpointctl offers shell completion scripts for bash, zsh and fish. These completion scripts are generated by `make completions`; do not
edit these files directly. To install them you can run `sudo make install.completions`.

For information about these scripts see `checkpointctl completion --help`.
Loading
Loading