build.go 2.1 KB
Newer Older
1
2
3
4
package function

import (
	"gitlab.com/gitlab-org/gitlabktl/app/commands"
5
	"gitlab.com/gitlab-org/gitlabktl/builder"
6
	"gitlab.com/gitlab-org/gitlabktl/runtime"
7
8
)

9
var newBuilder = func(action *BuildAction) (builder.Builder, error) {
10
11
12
	details := action.toRuntimeDetails()
	runtime := runtime.New(details)

13
	return builder.NewFromRuntime(runtime)
14
15
}

16
type BuildAction struct {
17
18
	Name      string `short:"n" long:"name" env:"GITLAB_FUNCTION_NAME" description:"Function name"`
	File      string `short:"f" long:"file" env:"GITLAB_FUNCTION_FILE" description:"Function file"`
19
20
21
22
	Handler   string `short:"c" long:"handler" env:"GITLAB_FUNCTION_HANDLER" description:"Function handler"`
	Runtime   string `short:"r" long:"runtime" env:"GITLAB_FUNCTION_RUNTIME" description:"Function runtime"`
	Directory string `short:"d" long:"directory" env:"GITLAB_FUNCTION_DIRECTORY" description:"Directory with code"`
	Image     string `short:"i" long:"image" env:"GITLAB_FUNCTION_IMAGE" description:"Resulting image name"`
23
	DryRun    bool   `short:"t" long:"dry-run" env:"GITLAB_FUNCTION_DRYRUN" description:"Dry run only"`
24
25
}

26
func (action *BuildAction) Execute(context *commands.Context) error {
27
28
29
30
	executor, err := newBuilder(action)
	if err != nil {
		return err
	}
31

32
	if action.DryRun {
33
		_, err := executor.BuildDryRun()
34
35

		return err
36
	}
37

38
	return executor.Build(context.Ctx)
39
40
}

41
func (action *BuildAction) toRuntimeDetails() runtime.Details {
42
	return runtime.Details{
43
44
45
		FunctionName:    action.Name,
		FunctionFile:    action.File,
		FunctionHandler: action.Handler,
46
		FunctionRuntime: action.Runtime,
47
48
		CodeDirectory:   action.Directory,
		ResultingImage:  action.Image,
Grzegorz Bizon's avatar
Grzegorz Bizon committed
49
50
51
	}
}

52
53
func newBuildCommand() commands.Command {
	return commands.Command{
54
		Registrable: commands.Registrable{
55
			Path: "function/build",
56
			Config: commands.Config{
57
58
59
60
61
62
				Name:        "build",
				Aliases:     []string{},
				Usage:       "Build your serverless function",
				Description: "This commands builds an image with a function and a runtime",
			},
		},
63
		Handler: new(BuildAction),
64
65
66
67
68
69
	}
}

func RegisterBuildCommand(register *commands.Register) {
	register.RegisterCommand(newBuildCommand())
}