Skip to content

Commit 9335472

Browse files
authored
Merge pull request #13 from Bandwidth/SWI-11010
SWI-11010 Add Cobra Doc Gen
2 parents 34a6b48 + 591580c commit 9335472

7 files changed

Lines changed: 116 additions & 0 deletions

File tree

.github/workflows/release.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,27 @@ jobs:
6161
env:
6262
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6363

64+
dispatch-docsite:
65+
needs: [release]
66+
runs-on: ubuntu-latest
67+
steps:
68+
- name: Mint app token for api-specs
69+
id: app-token
70+
uses: actions/create-github-app-token@v3
71+
with:
72+
client-id: ${{ secrets.APP_CLIENT_ID }}
73+
private-key: ${{ secrets.APP_PRIVATE_KEY }}
74+
owner: Bandwidth
75+
repositories: api-specs
76+
77+
- name: Trigger docsite CLI reference update
78+
env:
79+
GH_TOKEN: ${{ steps.app-token.outputs.token }}
80+
run: |
81+
gh api repos/Bandwidth/api-specs/dispatches \
82+
-f event_type=cli-docs-update \
83+
-F client_payload[version]="${{ github.ref_name }}"
84+
6485
bump-formula:
6586
needs: [release]
6687
runs-on: ubuntu-latest

.goreleaser.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
version: 2
22

3+
before:
4+
hooks:
5+
- go run ./internal/tools/docgen -out ./dist-docs
6+
- tar -czf cli-docs.tar.gz -C ./dist-docs .
7+
38
builds:
49
- main: ./cmd/band
510
binary: band
@@ -25,6 +30,11 @@ archives:
2530
checksum:
2631
name_template: "checksums.txt"
2732

33+
release:
34+
# Attach docs to the GitHub release
35+
extra_files:
36+
- glob: ./cli-docs.tar.gz
37+
2838
dockers:
2939
- image_templates:
3040
- "ghcr.io/bandwidth/cli:{{ .Version }}-amd64"

cmd/band/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// Command band is the official Bandwidth CLI for managing voice, messaging,
2+
// numbers, and more from the command line.
3+
//
4+
// It lets you build and debug voice applications, send messages, manage phone
5+
// numbers, and control calls without leaving your terminal. Global flags like
6+
// --plain give flat JSON output, create commands support --if-not-exists for
7+
// safe retries, and long-running operations support --wait, so it works equally
8+
// well interactively and from scripts or AI agents.
9+
//
10+
// Install with Homebrew (brew install Bandwidth/tap/band) or go install
11+
// (go install github.com/Bandwidth/cli/cmd/band@latest), then run "band --help"
12+
// to get started.
13+
//
14+
// For the full command reference and guides, see https://dev.bandwidth.com and
15+
// the project README at https://github.com/Bandwidth/cli.
116
package main
217

318
import (

cmd/root.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Package cmd defines the band CLI's cobra command tree: the root command and
2+
// every subcommand for voice, messaging, numbers, sub-accounts, and more.
13
package cmd
24

35
import (
@@ -118,6 +120,10 @@ var versionCmd = &cobra.Command{
118120
},
119121
}
120122

123+
func Root() *cobra.Command {
124+
return rootCmd
125+
}
126+
121127
func Execute() error {
122128
api.Version = version
123129
return rootCmd.Execute()

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313

1414
require (
1515
github.com/alessio/shellescape v1.4.1 // indirect
16+
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
1617
github.com/danieljoos/wincred v1.2.0 // indirect
1718
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
1819
github.com/godbus/dbus/v5 v5.1.0 // indirect
@@ -21,7 +22,9 @@ require (
2122
github.com/mattn/go-isatty v0.0.20 // indirect
2223
github.com/mattn/go-runewidth v0.0.9 // indirect
2324
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
25+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
2426
github.com/spf13/pflag v1.0.5 // indirect
2527
github.com/stretchr/testify v1.9.0 // indirect
2628
golang.org/x/sys v0.42.0 // indirect
29+
gopkg.in/yaml.v3 v3.0.1 // indirect
2730
)

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVK
22
github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
33
github.com/briandowns/spinner v1.23.2 h1:Zc6ecUnI+YzLmJniCfDNaMbW0Wid1d5+qcTq4L2FW8w=
44
github.com/briandowns/spinner v1.23.2/go.mod h1:LaZeM4wm2Ywy6vO571mvhQNRcWfRUnXOs0RcKV0wYKM=
5+
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
56
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
67
github.com/danieljoos/wincred v1.2.0 h1:ozqKHaLK0W/ii4KVbbvluM91W2H3Sh0BncbUNPS7jLE=
78
github.com/danieljoos/wincred v1.2.0/go.mod h1:FzQLLMKBFdvu+osBrnFODiv32YGwCfx0SkRa/eYHgec=
@@ -23,6 +24,7 @@ github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N
2324
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
2425
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
2526
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
27+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
2628
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
2729
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
2830
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
@@ -39,6 +41,7 @@ golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
3941
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
4042
golang.org/x/term v0.41.0 h1:QCgPso/Q3RTJx2Th4bDLqML4W6iJiaXFq2/ftQF13YU=
4143
golang.org/x/term v0.41.0/go.mod h1:3pfBgksrReYfZ5lvYM0kSO0LIkAl4Yl2bXOkKP7Ec2A=
44+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
4245
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4346
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
4447
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/tools/docgen/main.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"log"
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
11+
"github.com/Bandwidth/cli/cmd"
12+
"github.com/spf13/cobra/doc"
13+
)
14+
15+
func main() {
16+
out := flag.String("out", "./docs/cli", "output directory")
17+
format := flag.String("format", "markdown", "markdown|man|rest")
18+
front := flag.Bool("frontmatter", false, "prepend simple YAML front matter to markdown")
19+
flag.Parse()
20+
21+
if err := os.MkdirAll(*out, 0o755); err != nil {
22+
log.Fatal(err)
23+
}
24+
25+
root := cmd.Root()
26+
root.DisableAutoGenTag = true // stable, reproducible files (no timestamp footer)
27+
28+
switch *format {
29+
case "markdown":
30+
if *front {
31+
prep := func(filename string) string {
32+
base := filepath.Base(filename)
33+
name := strings.TrimSuffix(base, filepath.Ext(base))
34+
title := strings.ReplaceAll(name, "_", " ")
35+
return fmt.Sprintf("---\ntitle: %q\nslug: %q\ndescription: \"CLI reference for %s\"\n---\n\n", title, name, title)
36+
}
37+
link := func(name string) string { return strings.ToLower(name) }
38+
if err := doc.GenMarkdownTreeCustom(root, *out, prep, link); err != nil {
39+
log.Fatal(err)
40+
}
41+
} else {
42+
if err := doc.GenMarkdownTree(root, *out); err != nil {
43+
log.Fatal(err)
44+
}
45+
}
46+
case "man":
47+
hdr := &doc.GenManHeader{Title: strings.ToUpper(root.Name()), Section: "1"}
48+
if err := doc.GenManTree(root, hdr, *out); err != nil {
49+
log.Fatal(err)
50+
}
51+
case "rest":
52+
if err := doc.GenReSTTree(root, *out); err != nil {
53+
log.Fatal(err)
54+
}
55+
default:
56+
log.Fatalf("unknown format: %s", *format)
57+
}
58+
}

0 commit comments

Comments
 (0)