Chore: Detaching go tools from the main Grafana workspace (#104861)
* add script for tooling * add to make * not to forget * reworked go tools * add tool installation script * adding readme * updating readme * updating readme * cleanup install.sh and makefile * update the readme file * cleanup scripts * switch variables.mk to lazy evaluation * add tools ache to gitignore * get rid of absolute path in hte Variables.mk file * switch to reusable function for path generation * add debug statements * add create cache tool dir * add debuig statements to make file * drop tool cache * fix race condition n ci * fix race condition n ci * cleanup workspace * add lefthook.rc to codeowners * copy .citools folder to docker image * switch back to main branch of grafana-build * Add .citools to the drone builder * fix wording in generate.sh and README.md
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
## API
|
||||
|
||||
### Adding and Upgrading Tools
|
||||
|
||||
To add a new tool, execute the installation script:
|
||||
|
||||
```bash
|
||||
install.sh <tool>
|
||||
```
|
||||
|
||||
#### Example
|
||||
|
||||
The following command will add `lefthook` to the tracked tools if it is not already installed, or update its version:
|
||||
|
||||
```bash
|
||||
install.sh github.com/evilmartians/lefthook@v1.11.10
|
||||
```
|
||||
|
||||
Behind the scenes, the script performs a few simple steps:
|
||||
|
||||
- Creates a Go module under the `.citools/src/<toolname>` directory to track the tool version and its dependencies.
|
||||
- Creates a reference to the tool binary in the `.citools/Variables.mk` file.
|
||||
|
||||
### Using Tools in the Makefile
|
||||
|
||||
Our Makefile imports `.citools/Variables.mk`, so you can call a tool binary using standard Make syntax.
|
||||
|
||||
#### Example
|
||||
|
||||
```make
|
||||
run:
|
||||
$(bra) run
|
||||
```
|
||||
|
||||
### Using Tracked Tools Without the Makefile
|
||||
|
||||
If you want to use a tool outside of the Makefile, you can locate the tool binary by executing the following command:
|
||||
|
||||
```bash
|
||||
GOWORK=off go tool -n -modfile=<path_to_modfile> <toolname>
|
||||
```
|
||||
@@ -0,0 +1,34 @@
|
||||
# Generated tool paths
|
||||
tools_dir := $(shell cd $(dir $(lastword $(MAKEFILE_LIST))) && pwd)
|
||||
src_dir := $(tools_dir)/src
|
||||
|
||||
# Due to a race condition, after initial call to `go tool` golang may report a wrong binary location pointing to the invalid `/tmp/go-buildXXX` directory
|
||||
define compile_tool
|
||||
$(shell \
|
||||
(cd $(src_dir)/$(1) \
|
||||
&& GOWORK=off go tool -n $(2) > /dev/null \
|
||||
&& GOWORK=off go tool -n $(2)) | sed 's/^[[:space:]]*//g'; \
|
||||
)
|
||||
endef
|
||||
|
||||
|
||||
# Tool: "bra"
|
||||
bra = "$(call compile_tool,bra,github.com/unknwon/bra)"
|
||||
|
||||
# Tool: "cog"
|
||||
cog = "$(call compile_tool,cog,github.com/grafana/cog/cmd/cli)"
|
||||
|
||||
# Tool: "cue"
|
||||
cue = "$(call compile_tool,cue,cuelang.org/go/cmd/cue)"
|
||||
|
||||
# Tool: "golangci-lint"
|
||||
golangci-lint = "$(call compile_tool,golangci-lint,github.com/golangci/golangci-lint/v2/cmd/golangci-lint)"
|
||||
|
||||
# Tool: "jb"
|
||||
jb = "$(call compile_tool,jb,github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb)"
|
||||
|
||||
# Tool: "lefthook"
|
||||
lefthook = "$(call compile_tool,lefthook,github.com/evilmartians/lefthook)"
|
||||
|
||||
# Tool: "swagger"
|
||||
swagger = "$(call compile_tool,swagger,github.com/go-swagger/go-swagger/cmd/swagger)"
|
||||
Executable
+36
@@ -0,0 +1,36 @@
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
TOOLS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
TOOLS_SRC_DIR="$TOOLS_DIR/src"
|
||||
TOOLS_MK="$TOOLS_DIR/Variables.mk"
|
||||
|
||||
echo "# Generated tool paths" > "$TOOLS_MK"
|
||||
|
||||
cat <<'EOL' >> "$TOOLS_MK"
|
||||
tools_dir := $(shell cd $(dir $(lastword $(MAKEFILE_LIST))) && pwd)
|
||||
src_dir := $(tools_dir)/src
|
||||
|
||||
# Due to a race condition, after initial call to `go tool` golang may report a wrong binary location pointing to the invalid `/tmp/go-buildXXX` directory
|
||||
define compile_tool
|
||||
$(shell \
|
||||
(cd $(src_dir)/$(1) \
|
||||
&& GOWORK=off go tool -n $(2) > /dev/null \
|
||||
&& GOWORK=off go tool -n $(2)) | sed 's/^[[:space:]]*//g'; \
|
||||
)
|
||||
endef
|
||||
|
||||
EOL
|
||||
|
||||
for tooldir in "$TOOLS_SRC_DIR"/*; do
|
||||
[ -d "$tooldir" ] || continue
|
||||
tool=$(basename "$tooldir")
|
||||
fqtn=$(awk '/^tool / { print $2 }' "$tooldir/go.mod")
|
||||
|
||||
cat <<EOL >> "$TOOLS_MK"
|
||||
|
||||
# Tool: "$tool"
|
||||
${tool} = "\$(call compile_tool,${tool},${fqtn})"
|
||||
EOL
|
||||
done
|
||||
Executable
+33
@@ -0,0 +1,33 @@
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
TOOLS_BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
TOOLS_SRC_DIR="$TOOLS_BASE_DIR/src"
|
||||
|
||||
IMPORT_PATH_WITH_VERSION="$1"
|
||||
|
||||
if [[ "$IMPORT_PATH_WITH_VERSION" != *"@"* ]]; then
|
||||
echo "Error: tool version must be specified (e.g., github.com/foo/bar@v1.2.3)"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
TOOL_PATH="${IMPORT_PATH_WITH_VERSION%@*}"
|
||||
TOOL_NAME="${TOOL_PATH##*/}"
|
||||
|
||||
TOOL_DIR="$TOOLS_SRC_DIR/$TOOL_NAME"
|
||||
MOD_FILE="$TOOL_DIR/go.mod"
|
||||
|
||||
mkdir -p "$TOOL_DIR"
|
||||
cd "$TOOL_DIR"
|
||||
|
||||
# Create a new module if go.mod doesn't exist
|
||||
if [ ! -f go.mod ]; then
|
||||
go mod init "$TOOL_NAME"
|
||||
fi
|
||||
|
||||
go get -tool --modfile="$MOD_FILE" "$IMPORT_PATH_WITH_VERSION"
|
||||
echo "Installed $TOOL_NAME"
|
||||
echo " Directory: $TOOL_DIR"
|
||||
echo " Modfile: $MOD_FILE"
|
||||
|
||||
exec "$TOOLS_BASE_DIR/generate.sh"
|
||||
Reference in New Issue
Block a user