refactor: extract commands to domain package
Some checks are pending
ci-build / build (push) Blocked by required conditions
ci-build / release (push) Blocked by required conditions
ci-build / lint (push) Waiting to run
ci-scan / Analyze (go) (push) Waiting to run
ci-scan / Analyze (actions) (push) Waiting to run

This commit is contained in:
Rob Watson 2025-04-20 20:55:10 +02:00
parent 7afa84505e
commit add511e3dd
3 changed files with 16 additions and 16 deletions

View File

@ -155,7 +155,7 @@ func Run(ctx context.Context, params RunParams) error {
logger.Debug("Command received", "cmd", cmd.Name()) logger.Debug("Command received", "cmd", cmd.Name())
switch c := cmd.(type) { switch c := cmd.(type) {
case terminal.CommandAddDestination: case domain.CommandAddDestination:
newCfg := cfg newCfg := cfg
newCfg.Destinations = append(newCfg.Destinations, config.Destination{ newCfg.Destinations = append(newCfg.Destinations, config.Destination{
Name: c.DestinationName, Name: c.DestinationName,
@ -169,7 +169,7 @@ func Run(ctx context.Context, params RunParams) error {
cfg = newCfg cfg = newCfg
handleConfigUpdate(cfg, state, ui) handleConfigUpdate(cfg, state, ui)
ui.DestinationAdded() ui.DestinationAdded()
case terminal.CommandRemoveDestination: case domain.CommandRemoveDestination:
repl.StopDestination(c.URL) // no-op if not live repl.StopDestination(c.URL) // no-op if not live
newCfg := cfg newCfg := cfg
newCfg.Destinations = slices.DeleteFunc(newCfg.Destinations, func(dest config.Destination) bool { newCfg.Destinations = slices.DeleteFunc(newCfg.Destinations, func(dest config.Destination) bool {
@ -183,16 +183,16 @@ func Run(ctx context.Context, params RunParams) error {
cfg = newCfg cfg = newCfg
handleConfigUpdate(cfg, state, ui) handleConfigUpdate(cfg, state, ui)
ui.DestinationRemoved() ui.DestinationRemoved()
case terminal.CommandStartDestination: case domain.CommandStartDestination:
if !state.Source.Live { if !state.Source.Live {
ui.ShowSourceNotLiveModal() ui.ShowSourceNotLiveModal()
continue continue
} }
repl.StartDestination(c.URL) repl.StartDestination(c.URL)
case terminal.CommandStopDestination: case domain.CommandStopDestination:
repl.StopDestination(c.URL) repl.StopDestination(c.URL)
case terminal.CommandQuit: case domain.CommandQuit:
return nil return nil
} }
case <-uiUpdateT.C: case <-uiUpdateT.C:

View File

@ -1,4 +1,4 @@
package terminal package domain
// CommandAddDestination adds a destination. // CommandAddDestination adds a destination.
type CommandAddDestination struct { type CommandAddDestination struct {

View File

@ -40,7 +40,7 @@ const (
// UI is responsible for managing the terminal user interface. // UI is responsible for managing the terminal user interface.
type UI struct { type UI struct {
commandC chan Command commandC chan domain.Command
clipboardAvailable bool clipboardAvailable bool
configFilePath string configFilePath string
rtmpURL, rtmpsURL string rtmpURL, rtmpsURL string
@ -106,7 +106,7 @@ const defaultChanSize = 64
// StartUI starts the terminal user interface. // StartUI starts the terminal user interface.
func StartUI(ctx context.Context, params StartParams) (*UI, error) { func StartUI(ctx context.Context, params StartParams) (*UI, error) {
chanSize := cmp.Or(params.ChanSize, defaultChanSize) chanSize := cmp.Or(params.ChanSize, defaultChanSize)
commandCh := make(chan Command, chanSize) commandCh := make(chan domain.Command, chanSize)
app := tview.NewApplication() app := tview.NewApplication()
@ -268,7 +268,7 @@ func (ui *UI) renderAboutView() {
} }
// C returns a channel that receives commands from the user interface. // C returns a channel that receives commands from the user interface.
func (ui *UI) C() <-chan Command { func (ui *UI) C() <-chan domain.Command {
return ui.commandC return ui.commandC
} }
@ -444,7 +444,7 @@ func (ui *UI) ShowFatalErrorModal(errString string) {
[]string{"Quit"}, []string{"Quit"},
false, false,
func(int, string) { func(int, string) {
ui.commandC <- CommandQuit{} ui.commandC <- domain.CommandQuit{}
}, },
) )
}) })
@ -697,7 +697,7 @@ func (ui *UI) handleMediaServerClosed(exitReason string) {
SetBackgroundColor(tcell.ColorBlack). SetBackgroundColor(tcell.ColorBlack).
SetTextColor(tcell.ColorWhite). SetTextColor(tcell.ColorWhite).
SetDoneFunc(func(int, string) { SetDoneFunc(func(int, string) {
ui.commandC <- CommandQuit{} ui.commandC <- domain.CommandQuit{}
}) })
modal.SetBorderStyle(tcell.StyleDefault.Background(tcell.ColorBlack).Foreground(tcell.ColorWhite)) modal.SetBorderStyle(tcell.StyleDefault.Background(tcell.ColorBlack).Foreground(tcell.ColorWhite))
@ -887,7 +887,7 @@ func (ui *UI) addDestination() {
AddInputField(inputLabelName, "My stream", inputLen, nil, nil). AddInputField(inputLabelName, "My stream", inputLen, nil, nil).
AddInputField(inputLabelURL, "rtmp://", inputLen, nil, nil). AddInputField(inputLabelURL, "rtmp://", inputLen, nil, nil).
AddButton("Add", func() { AddButton("Add", func() {
ui.commandC <- CommandAddDestination{ ui.commandC <- domain.CommandAddDestination{
DestinationName: form.GetFormItemByLabel(inputLabelName).(*tview.InputField).GetText(), DestinationName: form.GetFormItemByLabel(inputLabelName).(*tview.InputField).GetText(),
URL: form.GetFormItemByLabel(inputLabelURL).(*tview.InputField).GetText(), URL: form.GetFormItemByLabel(inputLabelURL).(*tview.InputField).GetText(),
} }
@ -945,7 +945,7 @@ func (ui *UI) removeDestination() {
false, false,
func(buttonIndex int, _ string) { func(buttonIndex int, _ string) {
if buttonIndex == 0 { if buttonIndex == 0 {
ui.commandC <- CommandRemoveDestination{URL: url} ui.commandC <- domain.CommandRemoveDestination{URL: url}
} }
}, },
) )
@ -1009,12 +1009,12 @@ func (ui *UI) toggleDestination() {
switch ss { switch ss {
case startStateNotStarted: case startStateNotStarted:
ui.urlsToStartState[url] = startStateStarting ui.urlsToStartState[url] = startStateStarting
ui.commandC <- CommandStartDestination{URL: url} ui.commandC <- domain.CommandStartDestination{URL: url}
case startStateStarting: case startStateStarting:
// do nothing // do nothing
return return
case startStateStarted: case startStateStarted:
ui.commandC <- CommandStopDestination{URL: url} ui.commandC <- domain.CommandStopDestination{URL: url}
} }
} }
@ -1067,7 +1067,7 @@ func (ui *UI) confirmQuit() {
false, false,
func(buttonIndex int, _ string) { func(buttonIndex int, _ string) {
if buttonIndex == 0 { if buttonIndex == 0 {
ui.commandC <- CommandQuit{} ui.commandC <- domain.CommandQuit{}
} }
}, },
) )