Skip to content

Commit

Permalink
Merge pull request #1 from leapkit/task-read-procfile-with-comments
Browse files Browse the repository at this point in the history
task: allowing Procfile with comments
  • Loading branch information
paganotoni authored Feb 24, 2025
2 parents 583ef65 + 3a351b2 commit 35b2220
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 21 deletions.
16 changes: 14 additions & 2 deletions dev/internal/rebuilder/procfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ func readProcfile(path string) ([]entry, error) {
continue
}

for i := range parts {
parts[i] = strings.TrimSpace(parts[i])
}

// Skipping comments in Procfile
if strings.HasPrefix(parts[0], "#") {
continue
}

exists := slices.ContainsFunc(entries, func(e entry) bool {
return e.Name == parts[0]
})
Expand All @@ -38,11 +47,14 @@ func readProcfile(path string) ([]entry, error) {
continue
}

entries = append(entries, entry{
e := entry{
ID: len(entries),
Name: strings.TrimSpace(parts[0]),
Command: strings.TrimSpace(parts[1]),
})
}

entries = append(entries, e)
maxServiceNameLen = max(maxServiceNameLen, len(e.Name))
}

if err := scanner.Err(); err != nil {
Expand Down
5 changes: 0 additions & 5 deletions dev/internal/rebuilder/rebuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rebuilder

import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
Expand All @@ -14,8 +13,6 @@ func Serve(ctx context.Context) error {
return err
}

fmt.Println("[kit] Starting app")

ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
defer cancel()

Expand All @@ -36,7 +33,5 @@ func Serve(ctx context.Context) error {
<-exitCh
}

fmt.Println("[kit] Shutting down...")

return nil
}
102 changes: 90 additions & 12 deletions dev/internal/rebuilder/rebuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,8 @@ func TestServe(t *testing.T) {
var buf bytes.Buffer
io.Copy(&buf, r)

if !strings.Contains(buf.String(), "[kit] Starting app") {
t.Errorf("Expected '[kit] Starting app' to be in the output, got '%v'", buf.String())
}

if !strings.Contains(buf.String(), "[kit] Shutting down...") {
t.Errorf("Expected '[kit] Shutting down...' to be in the output, got '%v'", buf.String())
if !strings.Contains(buf.String(), "web |") {
t.Errorf("Expected 'web |' to be in the output, got '%v'", buf.String())
}
})

Expand Down Expand Up @@ -118,17 +114,13 @@ func TestServe(t *testing.T) {
var buf bytes.Buffer
io.Copy(&buf, r)

if !strings.Contains(buf.String(), "[kit] Starting app") {
t.Errorf("Expected '[kit] Starting app' to be in the output, got '%v'", buf.String())
if !strings.Contains(buf.String(), "web |") {
t.Errorf("Expected 'web |' to be in the output, got '%v'", buf.String())
}

if !strings.Contains(buf.String(), "Restarted...") {
t.Errorf("Expected 'Restarted...' to be in the output, got '%v'", buf.String())
}

if !strings.Contains(buf.String(), "[kit] Shutting down...") {
t.Errorf("Expected '[kit] Shutting down...' to be in the output, got '%v'", buf.String())
}
})

t.Run("Correct - Skip invalid commands in Procfile", func(t *testing.T) {
Expand Down Expand Up @@ -347,4 +339,90 @@ func TestServe(t *testing.T) {
t.Errorf("Expected an error, got nil")
}
})

t.Run("Validate log trailing spaces", func(t *testing.T) {
r, w, _ := os.Pipe()

stdOut := os.Stdout
stdErr := os.Stderr

os.Stdout = w
os.Stderr = w
t.Cleanup(func() {
os.Stdout = stdOut
os.Stderr = stdErr
})

procfile()
os.WriteFile("Procfile", []byte("123: echo '123'\n1234: echo '1234'\n12345: echo '12345'"), 0o644)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()

if err := rebuilder.Serve(ctx); err != nil {
t.Errorf("Expected nil, got '%v'", err)
}

w.Close()
var buf bytes.Buffer
io.Copy(&buf, r)

if !strings.Contains(buf.String(), "123 |") {
t.Errorf("Expected '123 |' to be in the output, got '%v'", buf.String())
}

if !strings.Contains(buf.String(), "1234 |") {
t.Errorf("Expected '1234 |' to be in the output, got '%v'", buf.String())
}

if !strings.Contains(buf.String(), "12345 |") {
t.Errorf("Expected '12345 |' to be in the output, got '%v'", buf.String())
}
})

t.Run("Read Procfile with comments", func(t *testing.T) {
r, w, _ := os.Pipe()

stdOut := os.Stdout
stdErr := os.Stderr

os.Stdout = w
os.Stderr = w
t.Cleanup(func() {
os.Stdout = stdOut
os.Stderr = stdErr
})

procfile()

content := `
# This is my awesome comment for my first command.
# This command 'hello' will print a friendly 'Hello, world!'
hello: echo 'Hello, world!'
# This command is commented because won't be used
# bye: echo 'Bye!'`

os.WriteFile("Procfile", []byte(content), 0o644)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()

if err := rebuilder.Serve(ctx); err != nil {
t.Errorf("Expected nil, got '%v'", err)
}

w.Close()
var buf bytes.Buffer
io.Copy(&buf, r)

if strings.Count(buf.String(), "hello |") != 3 {
t.Errorf("Expected 'hello |' to be in the output, got '%v'", buf.String())
}

if strings.Contains(buf.String(), "bye") {
t.Errorf("Expected 'bye' to be commented, got '%v'", buf.String())
}
})
}
10 changes: 8 additions & 2 deletions dev/internal/rebuilder/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"time"
)

var maxServiceNameLen = 4
// MaxServiceNameLen will receive the max length of the command name.
// This will be used to calculate the trailing spaces.
var maxServiceNameLen int

var colors = []string{
"\033[92m", // Green
Expand Down Expand Up @@ -38,7 +40,7 @@ func (cw *customWriter) Write(p []byte) (int, error) {
defer cw.mu.Unlock()

timestamp := time.Now().Format(time.DateTime)
trailingSpaces := strings.Repeat(" ", max(maxServiceNameLen-len(cw.prefix), 0))
trailingSpaces := strings.Repeat(" ", max(maxServiceNameLen-len(cw.prefix), 0)+1)

scanner := bufio.NewScanner(bytes.NewReader(p))
for scanner.Scan() {
Expand All @@ -56,6 +58,10 @@ func (cw *customWriter) Write(p []byte) (int, error) {
}
}

if err := scanner.Err(); err != nil {
return 0, err
}

return len(p), nil
}

Expand Down

0 comments on commit 35b2220

Please sign in to comment.