Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
github.com/stretchr/testify v1.11.1
golang.org/x/sync v0.20.0
golang.org/x/term v0.41.0
k8s.io/api v0.26.2
k8s.io/apimachinery v0.27.4
k8s.io/client-go v0.26.2
Expand Down Expand Up @@ -83,7 +84,6 @@ require (
golang.org/x/net v0.52.0 // indirect
golang.org/x/oauth2 v0.35.0 // indirect
golang.org/x/sys v0.42.0 // indirect
golang.org/x/term v0.41.0 // indirect
golang.org/x/text v0.35.0 // indirect
golang.org/x/time v0.12.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20260401024825-9d38bb4040a9 // indirect
Expand Down
9 changes: 9 additions & 0 deletions internal/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func RunSubgraph(ctx context.Context, cancel context.CancelFunc, port int, openB
return nil
}

setTerminalTitle(workflowTitle(name, subgraph.Nodes))

// create logs directory
if err := os.MkdirAll("logs", 0755); err != nil && !errors.Is(err, os.ErrExist) {
return fmt.Errorf("failed to create logs directory: %w", err)
Expand Down Expand Up @@ -230,6 +232,8 @@ func RunSubgraph(ctx context.Context, cancel context.CancelFunc, port int, openB
}

if len(failures) > 0 {
setTerminalTitle(workflowTitle(name, subgraph.Nodes))
ringTerminalBell()
return fmt.Errorf("failed tasks: %v", failures)
}

Expand Down Expand Up @@ -275,6 +279,8 @@ func RunSubgraph(ctx context.Context, cancel context.CancelFunc, port int, openB

if len(pendingTasks) == 0 {
logger.Println("✅ exiting because all requested tasks completed and none should be restarted")
setTerminalTitle(workflowTitle(name, subgraph.Nodes))
ringTerminalBell()
cancel()
} else if len(remainingTasks) == 0 {
if !allRunning {
Expand All @@ -288,6 +294,8 @@ func RunSubgraph(ctx context.Context, cancel context.CancelFunc, port int, openB
}
}
}
setTerminalTitle(workflowTitle(name, subgraph.Nodes))
ringTerminalBell()
}
} else {
allRunning = false
Expand Down Expand Up @@ -350,6 +358,7 @@ func RunSubgraph(ctx context.Context, cancel context.CancelFunc, port int, openB
node.Phase = phase
node.Message = message
stallTimers[node.Name].Reset(node.Task.GetStalledTimeout())
setTerminalTitle(workflowTitle(name, subgraph.Nodes))
logger.Println(node.Message)
statusEvents <- node
events <- poisonPill
Expand Down
72 changes: 72 additions & 0 deletions internal/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"bytes"
"context"
"io"
"log"
"os"
"strings"
Expand All @@ -28,6 +29,19 @@ func TestRunSubgraph(t *testing.T) {

}

setupTerminal := func(t *testing.T) *bytes.Buffer {
buffer := &bytes.Buffer{}
previousWriter := terminalWriter
previousIsTerminal := isTerminalWriter
terminalWriter = buffer
isTerminalWriter = func(io.Writer) bool { return true }
t.Cleanup(func() {
terminalWriter = previousWriter
isTerminalWriter = previousIsTerminal
})
return buffer
}

t.Run("No tasks", func(t *testing.T) {
ctx, cancel, logger, _ := setup(t)
defer cancel()
Expand Down Expand Up @@ -438,6 +452,64 @@ sleep 30
assert.NoError(t, err)
})

t.Run("Successful job updates terminal title and rings bell", func(t *testing.T) {
ctx, cancel, logger, _ := setup(t)
defer cancel()
terminal := setupTerminal(t)

wf := &types.Workflow{
Tasks: map[string]types.Task{
"job": {Command: []string{"true"}},
},
}
err := RunSubgraph(ctx, cancel, 0, false, logger, wf, []string{"job"}, nil)
assert.NoError(t, err)
assert.Contains(t, terminal.String(), ": done\033\\")
assert.Contains(t, terminal.String(), "\a")
})

t.Run("Ready service updates terminal title and rings bell", func(t *testing.T) {
ctx, cancel, logger, _ := setup(t)
defer cancel()
terminal := setupTerminal(t)

wf := &types.Workflow{
Tasks: map[string]types.Task{
"service": {Command: []string{"sleep", "30"}, Type: types.TaskTypeService},
},
}
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
err := RunSubgraph(ctx, cancel, 0, false, logger, wf, []string{"service"}, nil)
assert.NoError(t, err)
}()

sleep(t)
cancel()
wg.Wait()

assert.Contains(t, terminal.String(), ": ready\033\\")
assert.Contains(t, terminal.String(), "\a")
})

t.Run("Failed job updates terminal title and rings bell", func(t *testing.T) {
ctx, cancel, logger, _ := setup(t)
defer cancel()
terminal := setupTerminal(t)

wf := &types.Workflow{
Tasks: map[string]types.Task{
"job": {Command: []string{"false"}},
},
}
err := RunSubgraph(ctx, cancel, 0, false, logger, wf, []string{"job"}, nil)
assert.EqualError(t, err, "failed tasks: [job]")
assert.Contains(t, terminal.String(), ": failed (job)\033\\")
assert.Contains(t, terminal.String(), "\a")
})

t.Run("Ready message printed only once when service stays running", func(t *testing.T) {
ctx, cancel, logger, buffer := setup(t)
defer cancel()
Expand Down
77 changes: 77 additions & 0 deletions internal/terminal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package internal

import (
"fmt"
"io"
"os"
"strings"
"sync"

"golang.org/x/term"
)
Comment on lines +3 to +11

var terminalWriter io.Writer = os.Stdout

var isTerminalWriter = func(w io.Writer) bool {
file, ok := w.(*os.File)
return ok && term.IsTerminal(int(file.Fd()))
}
Comment on lines +13 to +18

var terminalMu sync.Mutex

var titleReplacer = strings.NewReplacer("\a", "", "\x1b", "", "\r", " ", "\n", " ")

func setTerminalTitle(title string) {
terminalMu.Lock()
defer terminalMu.Unlock()
if terminalWriter == nil || !isTerminalWriter(terminalWriter) {
return
}
title = titleReplacer.Replace(title)
_, _ = fmt.Fprintf(terminalWriter, "\033]0;%s\033\\", title)
}

func ringTerminalBell() {
terminalMu.Lock()
defer terminalMu.Unlock()
if terminalWriter == nil || !isTerminalWriter(terminalWriter) {
return
}
_, _ = io.WriteString(terminalWriter, "\a")
}

Comment on lines +34 to +42
func workflowTitle(name string, nodes map[string]*TaskNode) string {
if len(nodes) == 0 {
return fmt.Sprintf("kit %s", name)
}

complete := 0
running := 0
failures := []string{}
for _, node := range nodes {
switch node.Phase {
case "failed":
failures = append(failures, node.Name)
case "running", "stalled":
if node.Task.GetType() == "Service" {
running++
complete++
}
case "succeeded", "skipped":
complete++
}
}

switch {
case len(failures) == 1:
return fmt.Sprintf("kit %s: failed (%s)", name, failures[0])
case len(failures) > 1:
return fmt.Sprintf("kit %s: failed (%d)", name, len(failures))
case complete == len(nodes) && running > 0:
return fmt.Sprintf("kit %s: ready", name)
case complete == len(nodes):
return fmt.Sprintf("kit %s: done", name)
default:
return fmt.Sprintf("kit %s: starting (%d/%d)", name, complete, len(nodes))
}
}
56 changes: 56 additions & 0 deletions internal/terminal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package internal

import (
"bytes"
"io"
"testing"

"github.com/kitproj/kit/internal/types"
"github.com/stretchr/testify/assert"
)
Comment thread
Copilot marked this conversation as resolved.

func TestSetTerminalTitle(t *testing.T) {
var buffer bytes.Buffer

previousWriter := terminalWriter
previousIsTerminal := isTerminalWriter
terminalWriter = &buffer
isTerminalWriter = func(io.Writer) bool { return true }
t.Cleanup(func() {
terminalWriter = previousWriter
isTerminalWriter = previousIsTerminal
})

setTerminalTitle("hello\nworld\a")
assert.Equal(t, "\033]0;hello world\033\\", buffer.String())
}

func TestWorkflowTitle(t *testing.T) {
t.Run("ready", func(t *testing.T) {
title := workflowTitle("kit", map[string]*TaskNode{
"api": {Name: "api", Phase: "running", Task: types.Task{Ports: types.Ports{{ContainerPort: 8080}}}},
})
assert.Equal(t, "kit kit: ready", title)
})

t.Run("starting when job running", func(t *testing.T) {
title := workflowTitle("kit", map[string]*TaskNode{
"job": {Name: "job", Phase: "running"},
})
assert.Equal(t, "kit kit: starting (0/1)", title)
})

t.Run("done", func(t *testing.T) {
title := workflowTitle("kit", map[string]*TaskNode{
"job": {Name: "job", Phase: "succeeded"},
})
assert.Equal(t, "kit kit: done", title)
})

t.Run("failed", func(t *testing.T) {
title := workflowTitle("kit", map[string]*TaskNode{
"job": {Name: "job", Phase: "failed"},
})
assert.Equal(t, "kit kit: failed (job)", title)
})
}
Loading