Skip to content

Commit

Permalink
Add phpManager type to enforce OS-specific interface
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Jan 20, 2025
1 parent c44614b commit 1d74939
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 46 deletions.
14 changes: 7 additions & 7 deletions internal/legacy/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@ func (c *CLIWrapper) init() error {
}
return nil
})
g.Go(func() error {
return c.copyPHP(cacheDir)
})

g.Go(newPHPManager(cacheDir).copy)

if err := g.Wait(); err != nil {
return err
Expand Down Expand Up @@ -176,14 +175,15 @@ func (c *CLIWrapper) Exec(ctx context.Context, args ...string) error {

// makeCmd makes a legacy CLI command with the given context and arguments.
func (c *CLIWrapper) makeCmd(ctx context.Context, args []string, cacheDir string) *exec.Cmd {
iniSettings := c.phpSettings(cacheDir)
var cmdArgs = make([]string, 0, len(args)+2+len(iniSettings)*2)
for _, s := range iniSettings {
phpMgr := newPHPManager(cacheDir)
settings := phpMgr.settings()
var cmdArgs = make([]string, 0, len(args)+2+len(settings)*2)
for _, s := range settings {
cmdArgs = append(cmdArgs, "-d", s)
}
cmdArgs = append(cmdArgs, c.pharPath(cacheDir))
cmdArgs = append(cmdArgs, args...)
return exec.CommandContext(ctx, c.phpPath(cacheDir), cmdArgs...) //nolint:gosec
return exec.CommandContext(ctx, phpMgr.binPath(), cmdArgs...) //nolint:gosec
}

// PharPath returns the path to the legacy CLI's Phar file.
Expand Down
20 changes: 20 additions & 0 deletions internal/legacy/php_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package legacy

type phpManager interface {
// copy writes embedded PHP files to temporary files.
copy() error

// binPath returns the path to the temporary PHP binary.
binPath() string

// settings returns PHP INI entries (key=value format).
settings() []string
}

type phpManagerPerOS struct {
cacheDir string
}

func newPHPManager(cacheDir string) phpManager {
return &phpManagerPerOS{cacheDir}
}
16 changes: 16 additions & 0 deletions internal/legacy/php_manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package legacy

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestPHPManager(t *testing.T) {
tempDir := t.TempDir()

pm := newPHPManager(tempDir)
assert.NoError(t, pm.copy())

assert.FileExists(t, pm.binPath())
}
21 changes: 21 additions & 0 deletions internal/legacy/php_manager_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//go:build darwin || linux

package legacy

import (
"path/filepath"

"github.com/platformsh/cli/internal/file"
)

func (m *phpManagerPerOS) copy() error {
return file.WriteIfNeeded(m.binPath(), phpCLI, 0o755)
}

func (m *phpManagerPerOS) binPath() string {
return filepath.Join(m.cacheDir, "php")
}

func (m *phpManagerPerOS) settings() []string {
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ var phpCLI []byte
//go:embed archives/cacert.pem
var caCert []byte

// copyPHP to destination, if it does not exist
func (c *CLIWrapper) copyPHP(cacheDir string) error {
destDir := filepath.Join(cacheDir, "php")
func (m *phpManagerPerOS) copy() error {
destDir := filepath.Join(m.cacheDir, "php")

r, err := zip.NewReader(bytes.NewReader(phpCLI), int64(len(phpCLI)))
if err != nil {
Expand All @@ -49,9 +48,16 @@ func (c *CLIWrapper) copyPHP(cacheDir string) error {
return nil
}

// phpPath returns the path to the temporary PHP-CLI binary
func (c *CLIWrapper) phpPath(cacheDir string) string {
return filepath.Join(cacheDir, "php", "php.exe")
func (m *phpManagerPerOS) binPath() string {
return filepath.Join(m.cacheDir, "php", "php.exe")
}

func (m *phpManagerPerOS) settings() []string {
return []string{
"extension=" + filepath.Join(m.cacheDir, "php", "ext", "php_curl.dll"),
"extension=" + filepath.Join(m.cacheDir, "php", "ext", "php_openssl.dll"),
"openssl.cafile=" + filepath.Join(m.cacheDir, "php", "extras", "cacert.pem"),
}
}

// copyZipFile extracts a file from the Zip to the destination directory.
Expand Down Expand Up @@ -89,16 +95,8 @@ func copyZipFile(f *zip.File, destDir string) error {
}

if err := file.Write(destPath, b, f.Mode()); err != nil {
return fmt.Errorf("could not write extracted file %s: %w", destPath, err)
return fmt.Errorf("could not copy extracted file %s: %w", destPath, err)
}

return nil
}

func (c *CLIWrapper) phpSettings(cacheDir string) []string {
return []string{
"extension=" + filepath.Join(cacheDir, "php", "ext", "php_curl.dll"),
"extension=" + filepath.Join(cacheDir, "php", "ext", "php_openssl.dll"),
"openssl.cafile=" + filepath.Join(cacheDir, "php", "extras", "cacert.pem"),
}
}
24 changes: 0 additions & 24 deletions internal/legacy/php_unix.go

This file was deleted.

0 comments on commit 1d74939

Please sign in to comment.