-
Notifications
You must be signed in to change notification settings - Fork 3
/
factory.go
111 lines (91 loc) · 2.71 KB
/
factory.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright 2018-2020, VMware, Inc. All Rights Reserved.
* Proprietary and Confidential.
* Unauthorized use, copying or distribution of this source code via any medium is
* strictly prohibited without the express written consent of VMware, Inc.
*/
package libbs
import (
"bytes"
"fmt"
"strings"
"github.com/paketo-buildpacks/libpak/sbom"
"github.com/buildpacks/libcnb"
"github.com/paketo-buildpacks/libpak"
"github.com/paketo-buildpacks/libpak/effect"
"github.com/paketo-buildpacks/libpak/sherpa"
)
type ApplicationFactory struct {
Executor effect.Executor
}
func NewApplicationFactory() *ApplicationFactory {
return &ApplicationFactory{Executor: effect.NewExecutor()}
}
func (f *ApplicationFactory) NewApplication(
additionalMetadata map[string]interface{},
arguments []string,
artifactResolver ArtifactResolver,
cache Cache,
command string,
bom *libcnb.BOM,
applicationPath string,
bomScanner sbom.SBOMScanner,
) (Application, error) {
app := Application{
ApplicationPath: applicationPath,
Arguments: arguments,
ArtifactResolver: artifactResolver,
Cache: cache,
Command: command,
Executor: f.Executor,
BOM: bom,
SBOMScanner: bomScanner,
}
expected, err := f.expectedMetadata(additionalMetadata, app)
if err != nil {
return Application{}, fmt.Errorf("failed to generate expected metadata\n%w", err)
}
app.LayerContributor = libpak.NewLayerContributor("Compiled Application", expected, libcnb.LayerTypes{
Cache: true,
})
return app, nil
}
func (f *ApplicationFactory) expectedMetadata(additionalMetadata map[string]interface{}, app Application) (map[string]interface{}, error) {
var err error
metadata := map[string]interface{}{
"arguments": app.Arguments,
"artifact-pattern": app.ArtifactResolver.Pattern(),
}
metadata["files"], err = sherpa.NewFileListing(app.ApplicationPath)
if err != nil {
return nil, fmt.Errorf("unable to create file listing for %s\n%w", app.ApplicationPath, err)
}
metadata["java-version"], err = f.javaVersion()
if err != nil {
return nil, fmt.Errorf("unable to determine java version\n%w", err)
}
for k, v := range additionalMetadata {
metadata[k] = v
}
return metadata, nil
}
func (f *ApplicationFactory) javaVersion() (string, error) {
buf := &bytes.Buffer{}
if err := f.Executor.Execute(effect.Execution{
Command: "javac",
Args: []string{"-version"},
Stdout: buf,
Stderr: buf,
}); err != nil {
return "", fmt.Errorf("error executing 'javac -version':\n Combined Output: %s: \n%w", buf.String(), err)
}
s := strings.Split(strings.TrimSpace(buf.String()), " ")
switch len(s) {
case 2:
return s[1], nil
case 1:
return s[0], nil
default:
return "unknown", nil
}
}