xair-cli/main.go

114 lines
3.1 KiB
Go

package main
import (
"fmt"
"io"
"os"
"runtime/debug"
"strings"
"github.com/alecthomas/kong"
"github.com/charmbracelet/log"
kongcompletion "github.com/jotaen/kong-completion"
"github.com/onyx-and-iris/xair-cli/internal/xair"
)
var version string // Version of the CLI, set at build time.
// VersionFlag is a custom flag type that prints the version and exits.
type VersionFlag string
func (v VersionFlag) Decode(_ *kong.DecodeContext) error { return nil } // nolint: revive
func (v VersionFlag) IsBool() bool { return true } // nolint: revive
func (v VersionFlag) BeforeApply(app *kong.Kong, vars kong.Vars) error { // nolint: revive, unparam
fmt.Printf("xair-cli version: %s\n", vars["version"])
app.Exit(0)
return nil
}
type context struct {
Client *xair.Client
Out io.Writer
}
type Config struct {
Host string `default:"mixer.local" help:"The host of the X-Air device." env:"XAIR_CLI_HOST"`
Port int `default:"10024" help:"The port of the X-Air device." env:"XAIR_CLI_PORT"`
Kind string `default:"xr18" help:"The kind of the X-Air device." env:"XAIR_CLI_KIND"`
}
type CLI struct {
Config `embed:"" prefix:"" help:"The configuration for the CLI."`
Version VersionFlag `help:"Print gobs-cli version information and quit" name:"version" short:"v"`
Completion kongcompletion.Completion `help:"Generate shell completion scripts." cmd:"" aliases:"c"`
Raw RawCmdGroup `help:"Send raw OSC messages to the mixer." cmd:"" group:"Raw"`
Main MainCmdGroup `help:"Control the Main L/R output" cmd:"" group:"Main"`
Strip StripCmdGroup `help:"Control the strips." cmd:"" group:"Strip"`
Bus BusCmdGroup `help:"Control the buses." cmd:"" group:"Bus"`
Headamp HeadampCmdGroup `help:"Control input gain and phantom power." cmd:"" group:"Headamp"`
}
func main() {
var cli CLI
kongcompletion.Register(kong.Must(&cli))
ctx := kong.Parse(
&cli,
kong.Name("xair-cli"),
kong.Description("A CLI to control Behringer X-Air mixers."),
kong.UsageOnError(),
kong.ConfigureHelp(kong.HelpOptions{
Compact: true,
}),
kong.Vars{
"version": func() string {
if version == "" {
info, ok := debug.ReadBuildInfo()
if !ok {
return "(unable to read build info)"
}
version = strings.Split(info.Main.Version, "-")[0]
}
return version
}(),
},
)
ctx.FatalIfErrorf(run(ctx, cli.Config))
}
// run is the main entry point for the CLI.
// It connects to the X-Air device, retrieves mixer info, and then runs the command.
func run(ctx *kong.Context, config Config) error {
client, err := connect(config)
if err != nil {
return fmt.Errorf("failed to connect to X-Air device: %w", err)
}
defer client.Close()
client.StartListening()
resp, err := client.RequestInfo()
if err != nil {
return err
}
log.Infof("Received mixer info: %+v", resp)
ctx.Bind(&context{
Client: client,
Out: os.Stdout,
})
return ctx.Run()
}
func connect(config Config) (*xair.Client, error) {
client, err := xair.NewClient(config.Host, config.Port, xair.WithKind(config.Kind))
if err != nil {
return nil, err
}
return client, nil
}