Skip to content

Commit 3beec71

Browse files
authored
feat: adds doc auto generation for CLI commands (#140)
Signed-off-by: ChrisJBurns <29541485+ChrisJBurns@users.noreply.github.com>
1 parent 0bc8de1 commit 3beec71

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+881
-43
lines changed

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This file is documented at https://git-scm.com/docs/gitattributes.
2+
# Linguist-specific attributes are documented at
3+
# https://github.com/github/linguist.
4+
5+
docs/cli/thv*.md linguist-generated=true

.github/workflows/image-build-and-publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
- name: Build and Push Image to GHCR
5353
run: |
5454
TAG=$(echo "${{ steps.version-string.outputs.tag }}" | sed 's/+/_/g')
55-
KO_DOCKER_REPO=$BASE_REPO ko build --platform=linux/amd64,linux/arm64 --bare -t $TAG ./cmd/thv \
55+
KO_DOCKER_REPO=$BASE_REPO ko build --platform=linux/amd64,linux/arm64 --bare -t $TAG ./cmd \
5656
--image-label=org.opencontainers.image.source=https://github.com/StacklokLabs/toolhive,org.opencontainers.image.title="toolhive",org.opencontainers.image.vendor=Stacklok
5757
5858
- name: Sign Image with Cosign

.github/workflows/run-on-pr.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ jobs:
1212
tests:
1313
name: Tests
1414
uses: ./.github/workflows/test.yml
15+
docs:
16+
name: Docs
17+
uses: ./.github/workflows/verify-docgen.yml

.github/workflows/verify-docgen.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Docgen
2+
3+
on:
4+
workflow_call:
5+
6+
jobs:
7+
docgen:
8+
name: Verify Docgen
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
13+
- uses: actions/setup-go@0aaccfd150d50ccaeb58ebd88d36e91967a5f35b # v5
14+
with:
15+
go-version: '1.24.x'
16+
- run: ./cmd/help/verify.sh

.goreleaser.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ builds:
2222
goarch:
2323
- amd64
2424
- arm64
25-
main: ./cmd/thv
25+
main: ./cmd
2626
binary: thv
2727
# This section defines the release format.
2828
archives:

Taskfile.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
version: '3'
22

33
tasks:
4+
5+
docs:
6+
desc: Generate the docs
7+
cmds:
8+
- go run cmd/help/main.go --dir docs/cli
9+
410
lint:
511
desc: Run linting tools
612
cmds:
@@ -37,7 +43,7 @@ tasks:
3743
sh: date -u +"%Y-%m-%dT%H:%M:%SZ"
3844
cmds:
3945
- mkdir -p bin
40-
- go build -ldflags "-s -w -X github.com/StacklokLabs/toolhive/cmd/thv.Version={{.VERSION}} -X github.com/stacklok/toolhive/cmd/thv.Commit={{.COMMIT}} -X github.com/stacklok/toolhive/cmd/thv.BuildDate={{.BUILD_DATE}}" -o bin/thv ./cmd/thv
46+
- go build -ldflags "-s -w -X github.com/StacklokLabs/toolhive/cmd/thv.Version={{.VERSION}} -X github.com/StacklokLabs/toolhive/cmd/thv.Commit={{.COMMIT}} -X github.com/StacklokLabs/toolhive/cmd/thv.BuildDate={{.BUILD_DATE}}" -o bin/thv ./cmd
4147

4248
install:
4349
desc: Install the thv binary to GOPATH/bin
@@ -49,7 +55,7 @@ tasks:
4955
BUILD_DATE:
5056
sh: date -u +"%Y-%m-%dT%H:%M:%SZ"
5157
cmds:
52-
- go install -ldflags "-s -w -X github.com/StacklokLabs/toolhive/cmd/thv.Version={{.VERSION}} -X github.com/stacklok/toolhive/cmd/thv.Commit={{.COMMIT}} -X github.com/stacklok/toolhive/cmd/thv.BuildDate={{.BUILD_DATE}}" -v ./cmd/thv
58+
- go install -ldflags "-s -w -X github.com/StacklokLabs/toolhive/cmd/thv.Version={{.VERSION}} -X github.com/StacklokLabs/toolhive/cmd/thv.Commit={{.COMMIT}} -X github.com/StacklokLabs/toolhive/cmd/thv.BuildDate={{.BUILD_DATE}}" -v ./cmd
5359

5460
all:
5561
desc: Run linting, tests, and build
@@ -62,13 +68,13 @@ tasks:
6268
build-image:
6369
desc: Build the image with ko
6470
cmds:
65-
- ko build --platform linux/amd64,linux/arm64 --local ./cmd/thv
71+
- ko build --platform linux/amd64,linux/arm64 --local ./cmd
6672

6773
test-k8s-apply:
6874
desc: Builds the image, loads it into kind, and applies the Kubernetes manifests
6975
vars:
7076
IMAGE:
71-
sh: ko build --platform linux/amd64 --local -B ./cmd/thv | tail -n 1
77+
sh: ko build --platform linux/amd64 --local -B ./cmd | tail -n 1
7278
cmds:
7379
# gets the local kind kubeconfig
7480
- kind get kubeconfig > kconfig.yaml

cmd/help/main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Package main is the entry point for the ToolHive CLI Doc Generator.
2+
package main
3+
4+
import (
5+
"fmt"
6+
"os"
7+
8+
"github.com/spf13/cobra"
9+
"github.com/spf13/cobra/doc"
10+
11+
cli "github.com/StacklokLabs/toolhive/cmd/thv"
12+
)
13+
14+
func main() {
15+
var dir string
16+
root := &cobra.Command{
17+
Use: "gendoc",
18+
Short: "Generate ToolHive's help docs",
19+
SilenceUsage: true,
20+
Args: cobra.NoArgs,
21+
RunE: func(*cobra.Command, []string) error {
22+
return doc.GenMarkdownTree(cli.NewRootCmd(), dir)
23+
},
24+
}
25+
root.Flags().StringVarP(&dir, "dir", "d", "doc", "Path to directory in which to generate docs")
26+
if err := root.Execute(); err != nil {
27+
fmt.Println(err)
28+
os.Exit(1)
29+
}
30+
}

cmd/help/verify.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
# Verify that generated Markdown docs are up-to-date.
5+
tmpdir=$(mktemp -d)
6+
go run cmd/help/main.go --dir "$tmpdir"
7+
echo "###########################################"
8+
echo "If diffs are found, run: `task docs`"
9+
echo "###########################################"
10+
diff -Naur "$tmpdir" docs/cli/

cmd/main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Package main is the entry point for the ToolHive CLI.
2+
package main
3+
4+
import (
5+
"os"
6+
7+
cli "github.com/StacklokLabs/toolhive/cmd/thv"
8+
"github.com/StacklokLabs/toolhive/pkg/logger"
9+
)
10+
11+
func main() {
12+
// Initialize the logger system
13+
logger.Initialize()
14+
15+
if err := cli.NewRootCmd().Execute(); err != nil {
16+
logger.Log.Error("%v, %v", os.Stderr, err)
17+
os.Exit(1)
18+
}
19+
}
Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
package main
1+
// Package cli provides the entry point for the toolhive command-line application.
2+
package cli
23

34
import (
45
"fmt"
5-
"os"
66

77
"github.com/spf13/cobra"
88

99
"github.com/StacklokLabs/toolhive/pkg/logger"
1010
)
1111

1212
var rootCmd = &cobra.Command{
13-
Use: "thv",
14-
Short: "ToolHive (thv) is a lightweight, secure, and fast manager for MCP servers",
13+
Use: "thv",
14+
DisableAutoGenTag: true,
15+
Short: "ToolHive (thv) is a lightweight, secure, and fast manager for MCP servers",
1516
Long: `ToolHive (thv) is a lightweight, secure, and fast manager for MCP (Model Context Protocol) servers.
1617
It is written in Go and has extensive test coverage—including input validation—to ensure reliability and security.
1718
@@ -26,10 +27,8 @@ container-based isolation for running MCP servers.`,
2627
},
2728
}
2829

29-
func init() {
30-
// Initialize the logger system
31-
logger.Initialize()
32-
30+
// NewRootCmd creates a new root command for the ToolHive CLI.
31+
func NewRootCmd() *cobra.Command {
3332
// Add persistent flags
3433
rootCmd.PersistentFlags().Bool("debug", false, "Enable debug mode")
3534

@@ -43,11 +42,6 @@ func init() {
4342
rootCmd.AddCommand(newVersionCmd())
4443
rootCmd.AddCommand(newLogsCommand())
4544
rootCmd.AddCommand(newSecretCommand())
46-
}
4745

48-
func main() {
49-
if err := rootCmd.Execute(); err != nil {
50-
logger.Log.Error("%v, %v", os.Stderr, err)
51-
os.Exit(1)
52-
}
46+
return rootCmd
5347
}

0 commit comments

Comments
 (0)