-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (60 loc) · 1.81 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
// Dagger Composer Module
//
// This module provides a way to install composer dependencies in a directory for a
// PHP project. It uses the composer image to install the dependencies and returns the
// vendor directory with the dependencies installed.
package main
import (
"context"
"dagger/dagger-composer/internal/dagger"
"errors"
"fmt"
)
type Composer struct {
Version string
}
func New(
// +optional
// +default="latest"
// The version of the composer image to use.
version string,
) *Composer {
return &Composer{
Version: version,
}
}
// Takes a directory and installs composer dependencies.
func (m *Composer) Install(
ctx context.Context,
// +optional
// +defaultPath="."
// The directory that contains the composer.json/composer.lock files.
dir *dagger.Directory,
// +optional
// +default=["--prefer-dist", "--optimize-autoloader", "--ignore-platform-reqs"]
// Additional arguments to pass to the composer install command.
args []string) (*dagger.Directory, error) {
// make sure there is a composer.json file
if !exists(ctx, dir, "composer.json") {
return nil, errors.New("missing composer.json file in path")
}
// if there is no composer.lock, log it but continue
if !exists(ctx, dir, "composer.lock") {
// TODO(jasonmccallister) log to stdout in the dagger way
fmt.Println("composer.lock file not found in path")
}
exec := append([]string{"composer", "install"}, args...)
return dag.Container().
From("composer:"+m.Version).
WithMountedDirectory("/app", dir).
WithWorkdir("/app").
WithExec(exec).
Directory("/app/vendor"), nil
}
// can be simplified when this is completed: https://github.com/dagger/dagger/issues/6713
func exists(ctx context.Context, src *dagger.Directory, path string) bool {
if _, err := src.File(path).Sync(ctx); err != nil {
return false
}
return true
}