-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
main.go
69 lines (53 loc) · 1.37 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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package main
import (
"context"
"fmt"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"go.uber.org/fx"
"github.com/cilium/cilium/pkg/hive"
)
type ExampleConfig struct {
Foo string
Bar float32
DashedFieldName int
}
func (ExampleConfig) CellFlags(flags *pflag.FlagSet) {
flags.String("foo", "", "foo flag")
flags.Float32("bar", 0.25, "bar flag")
flags.Int("dashed-field-name", 10, "dashed")
}
type ExampleObject struct {
cfg ExampleConfig
}
func (o *ExampleObject) onStart(context.Context) error {
fmt.Printf(">>> ExampleObject.OnStart: Config: %#v\n", o.cfg)
return nil
}
func (o *ExampleObject) onStop(context.Context) error {
fmt.Printf(">>> ExampleObject.OnStop\n")
return nil
}
func newExampleObject(lc fx.Lifecycle, cfg ExampleConfig) *ExampleObject {
obj := &ExampleObject{cfg}
lc.Append(fx.Hook{OnStart: obj.onStart, OnStop: obj.onStop})
return obj
}
var exampleCell = hive.NewCellWithConfig[ExampleConfig](
"example",
fx.Provide(newExampleObject),
)
func main() {
// Construct the hive. This registers all command-line flags
// from the cells to the given FlagSet.
hive := hive.New(
viper.New(), pflag.CommandLine,
exampleCell,
hive.Require[*ExampleObject](),
)
pflag.Parse()
// Now that flags have been parsed the hive can be started.
hive.Run()
}