Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task: allowing Procfile with comments #1

Merged
merged 3 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
86 changes: 86 additions & 0 deletions dev/internal/rebuilder/rebuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,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
Loading