-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathexample.go
108 lines (81 loc) · 2.27 KB
/
example.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
package main
import (
"fmt"
"io/ioutil"
"github.com/mandelsoft/goutils/errors"
"ocm.software/ocm/api/config"
"ocm.software/ocm/api/config/cpi"
"ocm.software/ocm/api/utils/runtime"
)
const CFGFILE = "examples/lib/config3/config.yaml"
type Target struct {
updater cpi.Updater
value string
}
func NewTarget(ctx cpi.Context) *Target {
t := &Target{}
t.updater = cpi.NewUpdater(ctx, t)
return t
}
func (t *Target) SetValue(v string) {
t.value = v
}
func (t *Target) GetValue() string {
t.updater.Update()
return t.value
}
////////////////////////////////////////////////////////////////////////////////
const TYPE = "mytype.config.mandelsoft.org"
func init() {
cpi.RegisterConfigType(cpi.NewConfigType[*Config](TYPE, "just provide a value for Target objects"))
}
type Config struct {
runtime.ObjectVersionedType `json:",inline""`
Value string `json:"value"`
}
func (c *Config) ApplyTo(context cpi.Context, i interface{}) error {
if i == nil {
return nil
}
t, ok := i.(*Target)
if !ok {
return cpi.ErrNoContext(TYPE)
}
t.SetValue(c.Value)
return nil
}
var _ cpi.Config = (*Config)(nil)
func NewConfig(v string) *Config {
return &Config{
ObjectVersionedType: runtime.NewVersionedTypedObject(TYPE),
Value: v,
}
}
////////////////////////////////////////////////////////////////////////////////
func UsingConfigs() error {
ctx := config.DefaultContext()
target := NewTarget(ctx)
err := ctx.ApplyConfig(NewConfig("hello world"), "explicit1")
if err != nil {
return errors.Wrapf(err, "cannot apply config 1")
}
fmt.Printf("value is %q\n", target.GetValue())
err = ctx.ApplyConfig(NewConfig("hello universe"), "explicit2")
if err != nil {
return errors.Wrapf(err, "cannot apply config 2")
}
fmt.Printf("value is %q\n", target.GetValue())
newtarget := NewTarget(ctx)
fmt.Printf("value is %q\n", newtarget.GetValue())
// now reading config from a central generic configuration file
data, err := ioutil.ReadFile(CFGFILE)
if err != nil {
return errors.Wrapf(err, "cannot read configuration file %s", CFGFILE)
}
_, err = ctx.ApplyData(data, runtime.DefaultYAMLEncoding, CFGFILE)
if err != nil {
return errors.Wrapf(err, "cannot apply config data")
}
fmt.Printf("value is %q\n", newtarget.GetValue())
return nil
}