-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (75 loc) · 2.48 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// A generated module for Php functions
//
// This module is used to setup PHP containers with the specified version and extensions installed for Dagger
// pipelines. The PHP version must be a valid PHP version (8.2, 8.3, 8.4) and the extensions must be valid PHP
// extension that can be installed using the php-extension-installer script provided by ghcr.io/mlocati/php-extension-installer:latest
package main
import (
"context"
"dagger/php/internal/dagger"
"errors"
"fmt"
)
const (
ExtensionContainerImage = "ghcr.io/mlocati/php-extension-installer:latest"
ExtensionScriptPath = "/usr/bin/install-php-extensions"
)
type Php struct {
Extensions []string
Version string
}
func New(
// +optional
// +default="8.4"
// The version of PHP that will be installed
version string,
// +optional
// Which extensions to install
extensions []string,
) (*Php, error) {
// version must be a valid PHP version
// related to https://github.com/dagger/dagger/issues/8981
switch version {
case "8.2", "8.3", "8.4":
default:
return nil, fmt.Errorf("invalid PHP version provided: %s - only 8.4, 8.3, and 8.2 are supported", version)
}
return &Php{
Extensions: extensions,
Version: version,
}, nil
}
// Returns a container with the specified PHP version and extensions installed.
func (m *Php) Setup(ctx context.Context) (*dagger.Container, error) {
// grab the php-extension-installer script from the container
f, err := dag.Container().From(ExtensionContainerImage).File(ExtensionScriptPath).Contents(ctx)
if err != nil {
return nil, errors.New("failed to get php-extension-installer script")
}
// create a new container with the PHP version and the php-extension-installer script
container := dag.Container().From("php:"+m.Version).
WithNewFile("/usr/bin/install-php-extensions", f, dagger.ContainerWithNewFileOpts{
Permissions: 0755,
})
// if extensions are provided, add them to the container
if len(m.Extensions) > 0 {
container = container.WithExec(append([]string{"install-php-extensions"}, m.Extensions...))
}
return container, nil
}
// Info returns the PHP info output
func (m *Php) Info(ctx context.Context) (string, error) {
ctr, err := m.Setup(ctx)
if err != nil {
return "", err
}
return ctr.WithExec([]string{"php", "-i"}).Stdout(ctx)
}
// Modules returns the PHP modules output
func (m *Php) Modules(ctx context.Context) (string, error) {
ctr, err := m.Setup(ctx)
if err != nil {
return "", err
}
return ctr.WithExec([]string{"php", "-m"}).Stdout(ctx)
}