This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
OBS Scene Switcher is a command-line utility written in Go that toggles between two OBS Studio scenes using the OBS WebSocket protocol. The entire application is contained in a single main.go file with a focus on simplicity and reliability.
go buildThe build produces a binary named obs_switchscene.
go test -v ./...# Basic usage
./obs_switchscene <scene1> <scene2>
# With options (OBS WebSocket 5.x default port is 4455)
./obs_switchscene -host=localhost -port=4455 -timeout=5s -verbose <scene1> <scene2>
# With password authentication (command-line)
./obs_switchscene -password="your-password" <scene1> <scene2>
# With password authentication (from file)
echo "your-password" > ~/.obspwd && chmod 600 ~/.obspwd
./obs_switchscene <scene1> <scene2>
# Show version
./obs_switchscene -versionThe project uses GoReleaser for cross-platform builds:
goreleaser release --cleanGoReleaser builds for Linux, Windows, and Darwin (amd64) with version information injected via ldflags:
-X main.version={{.Version}}-X main.commit={{.Commit}}-X main.date={{.Date}}
The entire application is in main.go:1-158 with a straightforward structure:
- Config struct (main.go:18-26): Holds all runtime configuration including connection details, password, and scene names
- readPasswordFromFile() (main.go:60-82): Reads password from a file, expanding ~ for home directory and handling file existence
- parseFlags() (main.go:85-137): Handles CLI argument parsing with custom usage message, version display, and password support (both command-line and file-based). Command-line password takes precedence over password file.
- createClient(): Creates OBS WebSocket 5.x client with connection timeout using goroutines and channels
- switchScene(): Core logic that:
- Gets the current active scene from OBS
- Retrieves scene list and validates both target scenes exist
- Determines which scene to switch to (toggles between scene1 and scene2)
- Executes the scene change using the OBS WebSocket 5.x API
github.com/andreykaipov/goobs: OBS WebSocket 5.x client library (v1.4.0)
- Timeout pattern: Uses goroutines and channels for connection timeout
- Testability: Uses
osExitvariable (main.go:28) to allow mocking ofos.Exitin tests - Scene validation: Always validates scene existence before attempting switches to prevent errors
- OBS WebSocket 5.x: Uses the newer protocol (default port 4455) with optional password authentication
- Password security: Supports reading password from file (default
~/.obspwd) to avoid exposing passwords in command-line arguments. Command-line-passwordflag takes precedence over-password-file. - Automatic retry: If initial connection fails without a password, automatically retries using the password from the default file (
~/.obspwd), providing a seamless experience for users who have configured the password file.
All Go source files should include the MIT license header:
// filename.go
// Copyright (C) 2024 Ricardo Melo
//
// Distributed under terms of the MIT license.Releases are automated via GitHub Actions (.github/workflows/release.yml) when tags matching v* are pushed. The workflow:
- Runs tests with
go test -v ./... - Uses GoReleaser to build cross-platform binaries
- Creates GitHub releases with archives (tar.gz for Linux/Darwin, zip for Windows)