changes to help:

- print help on error
- print in compact mode

move version + VersionFlag into main
store version in kong.Vars

add 0.13.3 to CHANGELOG
This commit is contained in:
onyx-and-iris 2025-06-26 12:13:32 +01:00
parent f01fd0ca84
commit d2f0a64180
3 changed files with 40 additions and 33 deletions

View File

@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
# [0.13.3] - 2025-06-26
### Changed
- usage is now printed on errors.
- help is printed in compact mode. This should make it easier to page through help on the root command.
# [0.13.0] - 2025-06-23 # [0.13.0] - 2025-06-23
### Added ### Added

36
main.go
View File

@ -8,6 +8,8 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"runtime/debug"
"strings"
"time" "time"
"github.com/alecthomas/kong" "github.com/alecthomas/kong"
@ -16,6 +18,19 @@ import (
kongdotenv "github.com/titusjaka/kong-dotenv-go" kongdotenv "github.com/titusjaka/kong-dotenv-go"
) )
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("gobs-cli version: %s\n", vars["version"])
app.Exit(0)
return nil
}
// ObsConfig holds the configuration for connecting to the OBS WebSocket server. // ObsConfig holds the configuration for connecting to the OBS WebSocket server.
type ObsConfig struct { type ObsConfig struct {
Host string `flag:"host" help:"Host to connect to." default:"localhost" env:"OBS_HOST" short:"H"` Host string `flag:"host" help:"Host to connect to." default:"localhost" env:"OBS_HOST" short:"H"`
@ -31,7 +46,7 @@ type StyleConfig struct {
} }
// CLI is the main command line interface structure. // CLI is the main command line interface structure.
// It embeds the ObsConfig struct to inherit its fields and flags. // It embeds ObsConfig and StyleConfig to provide configuration options.
type CLI struct { type CLI struct {
ObsConfig `embed:"" help:"OBS WebSocket configuration."` ObsConfig `embed:"" help:"OBS WebSocket configuration."`
StyleConfig `embed:"" help:"Style configuration."` StyleConfig `embed:"" help:"Style configuration."`
@ -81,10 +96,25 @@ func main() {
var cli CLI var cli CLI
ctx := kong.Parse( ctx := kong.Parse(
&cli, &cli,
kong.Name("GOBS-CLI"), kong.Name("gobs-cli"),
kong.Description("A command line tool to interact with OBS Websocket."), kong.Description("A command line tool to interact with OBS Websocket."),
kong.Configuration(kongdotenv.ENVFileReader, ".env", filepath.Join(userConfigDir, "gobs-cli", "config.env")), kong.Configuration(kongdotenv.ENVFileReader, ".env", filepath.Join(userConfigDir, "gobs-cli", "config.env")),
) 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
}(),
})
client, err := connectObs(cli.ObsConfig) client, err := connectObs(cli.ObsConfig)
ctx.FatalIfErrorf(err) ctx.FatalIfErrorf(err)

View File

@ -2,38 +2,8 @@ package main
import ( import (
"fmt" "fmt"
"runtime/debug"
"strings"
"github.com/alecthomas/kong"
) )
var version string
// VersionFlag is a custom flag type for displaying version information.
type VersionFlag string
// Decode implements the kong.Flag interface.
func (v VersionFlag) Decode(_ *kong.DecodeContext) error { return nil }
// IsBool implements the kong.Flag interface.
func (v VersionFlag) IsBool() bool { return true }
// BeforeApply implements the kong.Flag interface.
func (v VersionFlag) BeforeApply(app *kong.Kong, _ kong.Vars) error { // nolint: unparam
if version == "" {
info, ok := debug.ReadBuildInfo()
if !ok {
return fmt.Errorf("failed to read build info")
}
version = strings.Split(info.Main.Version, "-")[0]
}
fmt.Printf("gobs-cli version: %s\n", version)
app.Exit(0) // Exit the application after printing the version
return nil
}
// ObsVersionCmd handles the version command. // ObsVersionCmd handles the version command.
type ObsVersionCmd struct{} // size = 0x0 type ObsVersionCmd struct{} // size = 0x0