Skip to content

Commit

Permalink
refactor(tmux): Replace CurrentServerOrDefault with explicit handling…
Browse files Browse the repository at this point in the history
… of what to do if there is no current server.
  • Loading branch information
JeffFaer committed Apr 1, 2024
1 parent fb48ad4 commit b36f515
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
5 changes: 4 additions & 1 deletion cmd/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ var cleanupCommand = &cobra.Command{
}

func cleanup(ctx context.Context) error {
srv, _ := tmux.CurrentServerOrDefault()
srv := tmux.MaybeCurrentServer()
if srv == nil {
srv = tmux.DefaultServer()
}
st, err := state.New(ctx, srv, api.Registered())
if err != nil {
return err
Expand Down
5 changes: 4 additions & 1 deletion cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ func newWorkUnit(ctx context.Context, workUnitName string, ctor workUnitCtor) er
if err != nil {
return err
}
srv, _ := tmux.CurrentServerOrDefault()
srv := tmux.MaybeCurrentServer()
if srv == nil {
srv = tmux.DefaultServer()
}
state, err := state.New(ctx, srv, vcs)
if err != nil {
return err
Expand Down
8 changes: 6 additions & 2 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func update(ctx context.Context) error {
curSesh := tmux.MaybeCurrentSession()
if curSesh == nil {
// Executed outside of tmux. Attach to the proper tmux session.
srv, _ := tmux.CurrentServerOrDefault()
srv := tmux.DefaultServer()
state, err := state.New(ctx, srv, vcs)
if err != nil {
return err
Expand Down Expand Up @@ -126,7 +126,11 @@ func updateTmux(ctx context.Context, st *state.State, repo api.Repository, workU

func updateTo(ctx context.Context, workUnitName string) error {
vcs := api.Registered()
srv, hasCurrentServer := tmux.CurrentServerOrDefault()
srv := tmux.MaybeCurrentServer()
hasCurrentServer := srv != nil
if !hasCurrentServer {
srv = tmux.DefaultServer()
}
st, err := state.New(ctx, srv, vcs)
if err != nil {
return err
Expand Down
9 changes: 2 additions & 7 deletions tmux/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,8 @@ func MaybeCurrentServer() Server {
return srv
}

// CurrentServerOrDefault either returns the CurrentServer, or the default server.
func CurrentServerOrDefault() (Server, bool) {
srv := MaybeCurrentServer()
if srv == nil {
return &server{}, false
}
return srv, true
func DefaultServer() Server {
return &server{}
}

func (srv *server) LogValue() slog.Value {
Expand Down

0 comments on commit b36f515

Please sign in to comment.