-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
82 lines (63 loc) · 1.41 KB
/
set.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
package value
import (
"github.com/rjeczalik/bigstruct/cmd/bigstruct/command"
"github.com/rjeczalik/bigstruct/storage"
"github.com/rjeczalik/bigstruct/storage/model"
"github.com/spf13/cobra"
)
func NewSetCommand(app *command.App) *cobra.Command {
m := &setCmd{
App: app,
Builder: new(command.Builder),
Meta: new(command.Meta),
}
cmd := &cobra.Command{
Use: "set",
Short: "Creates or updates values",
Args: cobra.NoArgs,
RunE: m.run,
SilenceUsage: true,
}
m.register(cmd)
return cmd
}
type setCmd struct {
*command.App
*command.Builder
*command.Meta
overlay string
schema bool
}
func (m *setCmd) register(cmd *cobra.Command) {
m.Builder.Register(cmd)
m.Meta.Register(cmd)
f := cmd.Flags()
f.StringVarP(&m.overlay, "overlay", "L", "", "")
f.BoolVarP(&m.schema, "schema", "x", false, "")
cmd.MarkFlagRequired("overlay")
}
func (m *setCmd) run(*cobra.Command, []string) error {
return m.Storage.Transaction(m.txRun)
}
func (m *setCmd) txRun(g storage.Gorm) error {
f, err := m.Builder.Build(m.Context)
if err != nil {
return err
}
ns, err := g.Overlay(m.overlay)
if err != nil {
return err
}
if m.schema {
s := model.MakeSchemas(ns, f)
if err := g.UpsertSchemas(s); err != nil {
return err
}
}
v := model.MakeValues(ns, f)
v.SetMeta(m.Meta.Object())
if err := g.UpsertValues(v); err != nil {
return err
}
return m.Render(v)
}