-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
83 lines (66 loc) · 3.1 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
/*
Copyright 2019 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package main is the reference implementation of Composition Functions.
package main
import (
"fmt"
"github.com/alecthomas/kong"
"github.com/google/go-containerregistry/pkg/name"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/function-runtime-oci/cmd/function-runtime-oci/run"
"github.com/crossplane/function-runtime-oci/cmd/function-runtime-oci/spark"
"github.com/crossplane/function-runtime-oci/cmd/function-runtime-oci/start"
"github.com/crossplane/function-runtime-oci/internal/version"
)
type debugFlag bool
type versionFlag bool
// KongVars represent the kong variables associated with the CLI parser
// required for the Registry default variable interpolation.
var KongVars = kong.Vars{
"default_registry": name.DefaultRegistry,
}
var cli struct {
Debug debugFlag `short:"d" help:"Print verbose logging statements."`
Version versionFlag `short:"v" help:"Print version and quit."`
Registry string `short:"r" help:"Default registry used to fetch containers when not specified in tag." default:"${default_registry}" env:"REGISTRY"`
Start start.Command `cmd:"" help:"Start listening for Composition Function runs over gRPC." default:"1"`
Run run.Command `cmd:"" help:"Run a Composition Function."`
Spark spark.Command `cmd:"" help:"function-runtime-oci executes Spark inside a user namespace to run a Composition Function. You shouldn't run it directly." hidden:""`
}
// BeforeApply binds the dev mode logger to the kong context when debugFlag is
// passed.
func (d debugFlag) BeforeApply(ctx *kong.Context) error { //nolint:unparam // BeforeApply requires this signature.
zl := zap.New(zap.UseDevMode(true)).WithName("function-runtime-oci")
// BindTo uses reflect.TypeOf to get reflection type of used interface
// A *logging.Logger value here is used to find the reflection type here.
// Please refer: https://golang.org/pkg/reflect/#TypeOf
ctx.BindTo(logging.NewLogrLogger(zl), (*logging.Logger)(nil))
return nil
}
func (v versionFlag) BeforeApply(app *kong.Kong) error { //nolint:unparam // BeforeApply requires this signature.
fmt.Fprintln(app.Stdout, version.New().GetVersionString())
app.Exit(0)
return nil
}
func main() {
zl := zap.New().WithName("function-runtime-oci")
ctx := kong.Parse(&cli,
kong.Name("function-runtime-oci"),
kong.Description("Crossplane Composition Functions."),
kong.BindTo(logging.NewLogrLogger(zl), (*logging.Logger)(nil)),
kong.UsageOnError(),
KongVars,
)
ctx.FatalIfErrorf(ctx.Run(&start.Args{Registry: cli.Registry}))
}