Skip to content

Commit c2322bc

Browse files
Merge pull request #52 from Bandwidth/feat/sample-command
feat(sample): add band sample run command
2 parents 4812029 + 35aed8e commit c2322bc

6 files changed

Lines changed: 670 additions & 0 deletions

File tree

‎cmd/root.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
portincmd "github.com/Bandwidth/cli/cmd/portin"
3333
quickstartcmd "github.com/Bandwidth/cli/cmd/quickstart"
3434
recordingcmd "github.com/Bandwidth/cli/cmd/recording"
35+
samplecmd "github.com/Bandwidth/cli/cmd/sample"
3536
shortcodecmd "github.com/Bandwidth/cli/cmd/shortcode"
3637
sipcmd "github.com/Bandwidth/cli/cmd/sip"
3738
sitecmd "github.com/Bandwidth/cli/cmd/site"
@@ -115,6 +116,7 @@ func init() {
115116
rootCmd.AddCommand(transcriptioncmd.Cmd)
116117
rootCmd.AddCommand(bxmlcmd.Cmd)
117118
rootCmd.AddCommand(quickstartcmd.Cmd)
119+
rootCmd.AddCommand(samplecmd.Cmd)
118120
rootCmd.AddCommand(vcpcmd.Cmd)
119121
rootCmd.AddCommand(tendlccmd.Cmd)
120122
rootCmd.AddCommand(shortcodecmd.Cmd)

‎cmd/sample/catalog.go‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package sample
2+
3+
// envPrompt describes an environment variable that a sample app requires beyond
4+
// the standard Bandwidth credentials (which are auto-populated from band auth).
5+
type envPrompt struct {
6+
Key string // env var name written to .env
7+
Flag string // CLI flag name (without --)
8+
Description string // shown in --help
9+
}
10+
11+
// langSetup describes how to install dependencies and run the app for a given language.
12+
type langSetup struct {
13+
// AppDir is the working directory to cd into before running.
14+
AppDir string
15+
// InstallCmd is the command to install dependencies (run once after clone).
16+
InstallCmd []string
17+
// RunCmd is the command to start the app (run in the AppDir).
18+
RunCmd []string
19+
// HealthPath is the HTTP path polled to detect a ready server.
20+
HealthPath string
21+
}
22+
23+
// SampleEntry describes one sample app and its variants.
24+
type SampleEntry struct {
25+
Description string
26+
// Repos maps language name → GitHub clone URL.
27+
Repos map[string]string
28+
// ExtraEnv lists environment variables required beyond BW_* credentials.
29+
ExtraEnv []envPrompt
30+
// Setup maps language name → build/run instructions.
31+
Setup map[string]langSetup
32+
// Port is the default local port the app listens on.
33+
Port int
34+
}
35+
36+
// catalog is the authoritative list of runnable Bandwidth sample apps.
37+
var catalog = map[string]*SampleEntry{
38+
"live-assistant": {
39+
Description: "Real-time AI voice assistant powered by OpenAI Realtime API",
40+
Repos: map[string]string{
41+
"python": "https://github.com/Bandwidth-Samples/openai-live-websockets-python",
42+
},
43+
ExtraEnv: []envPrompt{
44+
{Key: "OPENAI_API_KEY", Flag: "openai-key", Description: "OpenAI API key (must have Realtime API access)"},
45+
{Key: "TRANSFER_TO", Flag: "transfer-to", Description: "Phone number to transfer calls to (E.164, e.g. +19195551234)"},
46+
},
47+
Setup: map[string]langSetup{
48+
"python": {
49+
AppDir: "app",
50+
InstallCmd: []string{".venv/bin/pip", "install", "-q", "-r", "app/requirements.txt"},
51+
RunCmd: []string{".venv/bin/python3", "main.py"},
52+
HealthPath: "/health",
53+
},
54+
},
55+
Port: 3000,
56+
},
57+
"voice-gather": {
58+
Description: "In-call DTMF gather example",
59+
Repos: map[string]string{
60+
"python": "https://github.com/Bandwidth-Samples/in-call-gather-python",
61+
},
62+
Setup: map[string]langSetup{
63+
"python": {
64+
AppDir: ".",
65+
InstallCmd: []string{".venv/bin/pip", "install", "-q", "-r", "requirements.txt"},
66+
RunCmd: []string{".venv/bin/python3", "app.py"},
67+
HealthPath: "/",
68+
},
69+
},
70+
Port: 3000,
71+
},
72+
}

‎cmd/sample/list.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package sample
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var listCmd = &cobra.Command{
11+
Use: "list",
12+
Short: "List available sample applications",
13+
RunE: runList,
14+
}
15+
16+
func runList(cmd *cobra.Command, args []string) error {
17+
names := make([]string, 0, len(catalog))
18+
for n := range catalog {
19+
names = append(names, n)
20+
}
21+
sort.Strings(names)
22+
23+
for _, name := range names {
24+
entry := catalog[name]
25+
langs := make([]string, 0, len(entry.Repos))
26+
for l := range entry.Repos {
27+
langs = append(langs, l)
28+
}
29+
sort.Strings(langs)
30+
31+
fmt.Printf("%-20s %s\n", name, entry.Description)
32+
fmt.Printf("%-20s languages: %v\n\n", "", langs)
33+
}
34+
return nil
35+
}

0 commit comments

Comments
 (0)