Skip to content

Commit

Permalink
Improve file name matching when determining parser for value of --pro…
Browse files Browse the repository at this point in the history
…cfile flag

Fixes #6
  • Loading branch information
fgrosse committed Jul 12, 2024
1 parent e1fbd36 commit 490b7cc
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 25 deletions.
3 changes: 3 additions & 0 deletions cmd/prox/_testdata/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
redis: bin/redis-server conf/redis.conf
server: php -S localhost:8080 app/web/index.php
selenium: java -jar /usr/local/bin/selenium-server-standalone.jar
1 change: 1 addition & 0 deletions cmd/prox/_testdata/Procfile.dev
4 changes: 4 additions & 0 deletions cmd/prox/_testdata/Proxfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
processes:
redis: bin/redis-server conf/redis.conf
server: "php -S localhost:8080 app/web/index.php"
selenium: "java -jar /usr/local/bin/selenium-server-standalone.jar"
1 change: 1 addition & 0 deletions cmd/prox/_testdata/default-to-Procfile/Procfile
1 change: 1 addition & 0 deletions cmd/prox/_testdata/default-to-Proxfile/Proxfile
54 changes: 29 additions & 25 deletions cmd/prox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"

"github.com/fgrosse/prox"
Expand Down Expand Up @@ -92,37 +94,39 @@ func environment(path string) (prox.Environment, error) {
return env, err
}

func processes(env prox.Environment, procFileFlag string) ([]prox.Process, error) {
var (
f *os.File
err error
parse = prox.ParseProxFile
)

if procFileFlag != "" {
// user has specified a path
logger.Debug("Reading processes from file specified via --procfile", zap.String("path", procFileFlag))
f, err = os.Open(procFileFlag)
if filepath.Base(procFileFlag) == "Procfile" {
parse = prox.ParseProcFile
}
} else {
// default to "Proxfile"
f, err = os.Open("Proxfile")
if os.IsNotExist(err) {
// no "Proxfile" but maybe we can open a "Procfile"
logger.Debug("Reading processes from Procfile")
parse = prox.ParseProcFile
f, err = os.Open("Procfile")
func processes(env prox.Environment, path string) ([]prox.Process, error) {
var parser func(io.Reader, prox.Environment) ([]prox.Process, error)

switch {
case path == "" && fileExists("Proxfile"):
logger.Debug("Reading processes from Proxfile")
path = "Proxfile"
parser = prox.ParseProxFile
case path == "" && fileExists("Procfile"):
logger.Debug("Reading processes from Procfile")
path = "Procfile"
parser = prox.ParseProcFile
case path != "":
logger.Debug("Reading processes from file specified via --procfile", zap.String("path", path))
if strings.HasPrefix(filepath.Base(path), "Procfile") {
parser = prox.ParseProcFile
} else {
logger.Debug("Reading processes from Proxfile")
parser = prox.ParseProxFile
}
default:
return nil, errors.New("no Proxfile or Procfile file found. Please specify a path with --procfile")
}

f, err := os.Open(path)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to open process file: %w", err)
}

defer f.Close()
return parse(f, env)
return parser(f, env)
}

func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
55 changes: 55 additions & 0 deletions cmd/prox/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"github.com/fgrosse/prox"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
"os"
"testing"
)

func TestParseProcessesFile(t *testing.T) {
cases := map[string]struct {
dir string
path string
}{
"default Procfile": {dir: "_testdata/default-to-Procfile"},
"default Proxfile": {dir: "_testdata/default-to-Proxfile"},
"Proxfile": {dir: "_testdata", path: "Proxfile"},
"Procfile": {path: "_testdata/Procfile"},
"Procfile.dev": {path: "_testdata/Procfile.dev"},
}

for name, c := range cases {
t.Run(name, func(t *testing.T) {
logger = zaptest.NewLogger(t)
env := prox.Environment{}

if c.dir != "" {
changeDirectory(t, c.dir)
}

pp, err := processes(env, c.path)
require.NoError(t, err)
assert.Len(t, pp, 3)
})
}
}

func changeDirectory(t *testing.T, dir string) {
t.Helper()

originalDir, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get current directory: %v", err)
}

err = os.Chdir(dir)
require.NoError(t, err)

t.Cleanup(func() {
err := os.Chdir(originalDir)
require.NoError(t, err)
})
}

0 comments on commit 490b7cc

Please sign in to comment.