-
Notifications
You must be signed in to change notification settings - Fork 9
/
shellscript.go
58 lines (47 loc) · 2.04 KB
/
shellscript.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package shellscript
import (
"context"
"io/fs"
"path/filepath"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/parsing/integration/api"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
func Register() {
api.RegisterIntegration[*schema.ShellScriptIntegration, *schema.ShellScriptIntegration](impl{})
}
type impl struct {
api.DefaultBinaryTestIntegration[*schema.ShellScriptIntegration]
}
func (impl) ApplyToServer(ctx context.Context, env *schema.Environment, pl pkggraph.PackageLoader, pkg *pkggraph.Package, data *schema.ShellScriptIntegration) error {
return fnerrors.NewWithLocation(pkg.Location, "shellscript integration is not supported on the server")
}
func (impl) CreateBinary(ctx context.Context, env *schema.Environment, pl pkggraph.PackageLoader, loc pkggraph.Location, data *schema.ShellScriptIntegration) (*schema.Binary, error) {
if data.Entrypoint == "" {
return nil, fnerrors.NewWithLocation(loc, "missing required field `entrypoint`")
}
entrypoint := filepath.Clean(data.Entrypoint)
if _, err := fs.Stat(loc.Module.ReadOnlyFS(), filepath.Join(loc.Rel(), entrypoint)); err != nil {
return nil, fnerrors.NewWithLocation(loc, "could not find %q file, please verify that the specified script path is correct: %w", data.Entrypoint, err)
}
required := []string{"bash", "curl"}
required = append(required, data.RequiredPackages...)
return &schema.Binary{
BuildPlan: &schema.LayeredImageBuildPlan{
LayerBuildPlan: []*schema.ImageBuildPlan{{
AlpineBuild: &schema.ImageBuildPlan_AlpineBuild{Package: required},
}, {
SnapshotFiles: []string{entrypoint},
}},
},
Config: &schema.BinaryConfig{
WorkingDir: "/",
// "./" is important to resolve the entrypoint script relative to the working directory.
Command: []string{"./" + entrypoint},
},
}, nil
}