Skip to content

Commit

Permalink
make: generate auto-completion files
Browse files Browse the repository at this point in the history
This patch extends the Makefile with targets to generate, install,
and uninstall auto-completion files.

Signed-off-by: Radostin Stoyanov <[email protected]>
  • Loading branch information
rst0git committed Jan 26, 2024
1 parent 96cf0a8 commit a01cd25
Show file tree
Hide file tree
Showing 6 changed files with 829 additions and 27 deletions.
40 changes: 38 additions & 2 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 Down Expand Up @@ -48,14 +52,14 @@ release:
CGO_ENABLED=0 $(GO_BUILD) -o $(NAME) -ldflags "-X main.name=$(NAME) -X main.version=${VERSION}"

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

.PHONY: uninstall
uninstall:
uninstall: uninstall.completions
@make -C docs uninstall
@echo " UNINSTALL" $(NAME)
@$(RM) $(addprefix $(DESTDIR)$(BINDIR)/,$(NAME))
Expand Down Expand Up @@ -110,9 +114,41 @@ vendor:
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 Down
25 changes: 0 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,31 +202,6 @@ Please note that writing large memory pages to a file can take several minutes.
make DESTDIR=/some/new/place install
```

## Enable autocompletion

You now need to ensure that the autocompletion script gets sourced in all your shell sessions.
There are two ways in which you can do this:

### User

```console
echo 'source <(checkpointctl completion bash)' >>~/.bashrc
```

### System

```console
checkpointctl completion bash | sudo tee /etc/bash_completion.d/checkpointctl > /dev/null
sudo chmod a+r /etc/bash_completion.d/checkpointctl
```

Both approaches are equivalent. After reloading your shell, autocompletion should be working.
To enable bash autocompletion in current shell session, source the `~/.bashrc` file:

```console
source ~/.bashrc
```

## Uninstalling

The following command can be used to clean up a previously installed checkpointctl instance.
Expand Down
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

0 comments on commit a01cd25

Please sign in to comment.