|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "syscall" |
| 8 | + |
| 9 | + // Packages |
| 10 | + kong "github.com/alecthomas/kong" |
| 11 | + tablewriter "github.com/djthorpe/go-tablewriter" |
| 12 | + opt "github.com/mutablelogic/go-client" |
| 13 | + ctx "github.com/mutablelogic/go-server/pkg/context" |
| 14 | + client "github.com/mutablelogic/go-whisper/pkg/client" |
| 15 | +) |
| 16 | + |
| 17 | +//////////////////////////////////////////////////////////////////////////////// |
| 18 | +// TYPES |
| 19 | + |
| 20 | +type Globals struct { |
| 21 | + Url string `name:"url" help:"URL of whisper service (can be set from WHISPER_URL env)" default:"${WHISPER_URL}"` |
| 22 | + Debug bool `name:"debug" help:"Enable debug output"` |
| 23 | + |
| 24 | + // Writer, service and context |
| 25 | + writer *tablewriter.Writer |
| 26 | + client *client.Client |
| 27 | + ctx context.Context |
| 28 | +} |
| 29 | + |
| 30 | +type CLI struct { |
| 31 | + Globals |
| 32 | + |
| 33 | + Ping PingCmd `cmd help:"Ping the whisper service"` |
| 34 | +} |
| 35 | + |
| 36 | +//////////////////////////////////////////////////////////////////////////////// |
| 37 | +// GLOBALS |
| 38 | + |
| 39 | +const ( |
| 40 | + defaultEndpoint = "http://localhost:8080/api/v1" |
| 41 | +) |
| 42 | + |
| 43 | +//////////////////////////////////////////////////////////////////////////////// |
| 44 | +// MAIN |
| 45 | + |
| 46 | +func main() { |
| 47 | + // The name of the executable |
| 48 | + name, err := os.Executable() |
| 49 | + if err != nil { |
| 50 | + panic(err) |
| 51 | + } else { |
| 52 | + name = filepath.Base(name) |
| 53 | + } |
| 54 | + |
| 55 | + // Create a cli parser |
| 56 | + cli := CLI{} |
| 57 | + cmd := kong.Parse(&cli, |
| 58 | + kong.Name(name), |
| 59 | + kong.Description("speech transcription and translation service client"), |
| 60 | + kong.UsageOnError(), |
| 61 | + kong.ConfigureHelp(kong.HelpOptions{Compact: true}), |
| 62 | + kong.Vars{ |
| 63 | + "WHISPER_URL": envOrDefault("WHISPER_URL", defaultEndpoint), |
| 64 | + }, |
| 65 | + ) |
| 66 | + |
| 67 | + // Set whisper client options |
| 68 | + opts := []opt.ClientOpt{} |
| 69 | + if cli.Globals.Debug { |
| 70 | + opts = append(opts, opt.OptTrace(os.Stderr, true)) |
| 71 | + } |
| 72 | + |
| 73 | + // Create a whisper client |
| 74 | + client, err := client.New(cli.Globals.Url, opts...) |
| 75 | + if err != nil { |
| 76 | + cmd.FatalIfErrorf(err) |
| 77 | + return |
| 78 | + } else { |
| 79 | + cli.Globals.client = client |
| 80 | + } |
| 81 | + |
| 82 | + // Create a tablewriter object with text output |
| 83 | + writer := tablewriter.New(os.Stdout, tablewriter.OptOutputText()) |
| 84 | + cli.Globals.writer = writer |
| 85 | + |
| 86 | + // Create a context |
| 87 | + cli.Globals.ctx = ctx.ContextForSignal(os.Interrupt, syscall.SIGQUIT) |
| 88 | + |
| 89 | + // Run the command |
| 90 | + if err := cmd.Run(&cli.Globals); err != nil { |
| 91 | + cmd.FatalIfErrorf(err) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func envOrDefault(name, def string) string { |
| 96 | + if value := os.Getenv(name); value != "" { |
| 97 | + return value |
| 98 | + } else { |
| 99 | + return def |
| 100 | + } |
| 101 | +} |
0 commit comments