-
Notifications
You must be signed in to change notification settings - Fork 15
/
export.go
178 lines (156 loc) · 5.52 KB
/
export.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package sync
import (
"fmt"
"os"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/kaskada-ai/kaskada/clients/cli/api"
"github.com/kaskada-ai/kaskada/clients/cli/utils"
apiv1alpha "github.com/kaskada-ai/kaskada/gen/proto/go/kaskada/kaskada/v1alpha"
)
// exportCmd represents the export command
var exportCmd = &cobra.Command{
Use: "export",
Short: "takes a set of resources and returns a yaml file",
Long: `Used to generate resources-as-code for kaskada tables,
views, and materializations. The outputted yaml file
can be used with the 'plan' and 'apply' commands
to update your infrastructure.`,
Run: func(cmd *cobra.Command, args []string) {
log.Info().Msg("starting export")
var (
err error
materializations []*apiv1alpha.Materialization
tables []*apiv1alpha.Table
views []*apiv1alpha.View
)
apiClient := api.NewApiClient()
if !getAll && len(materializationNames)+len(tableNames)+len(viewNames) == 0 {
utils.LogAndQuitIfErrorExists(fmt.Errorf("either `all`, `materialization`, `table`, or `view` flag needs to be set at least once"))
}
if getAll {
materializations, err = exportMaterializations(apiClient)
utils.LogAndQuitIfErrorExists(err)
tables, err = exportTables(apiClient)
utils.LogAndQuitIfErrorExists(err)
views, err = exportViews(apiClient)
utils.LogAndQuitIfErrorExists(err)
} else {
for _, materializationName := range materializationNames {
materialization, err := exportMaterialization(apiClient, materializationName)
utils.LogAndQuitIfErrorExists(err)
materializations = append(materializations, materialization)
}
for _, tableName := range tableNames {
table, err := exportTable(apiClient, tableName)
utils.LogAndQuitIfErrorExists(err)
tables = append(tables, table)
}
for _, viewName := range viewNames {
view, err := exportView(apiClient, viewName)
utils.LogAndQuitIfErrorExists(err)
views = append(views, view)
}
}
spec := &apiv1alpha.Spec{
Tables: tables,
Views: views,
Materializations: materializations,
}
yamlData, err := utils.ProtoToYaml(spec)
utils.LogAndQuitIfErrorExists(err)
if filePath == "" {
fmt.Printf("%s\n", yamlData)
} else {
utils.LogAndQuitIfErrorExists(os.WriteFile(filePath, yamlData, 0666))
}
log.Info().Msg("Success!")
},
}
func exportMaterializations(apiClient api.ApiClient) ([]*apiv1alpha.Materialization, error) {
protos, err := apiClient.List(&apiv1alpha.Materialization{}, "", 100, "")
if err != nil {
return nil, err
}
api.ClearOutputOnlyFieldsList(protos)
materializations := make([]*apiv1alpha.Materialization, 0)
for _, p := range protos {
m, err := api.ProtoToMaterialization(p)
if err != nil {
return nil, err
}
materializations = append(materializations, m)
}
return materializations, nil
}
func exportTables(apiClient api.ApiClient) ([]*apiv1alpha.Table, error) {
protos, err := apiClient.List(&apiv1alpha.Table{}, "", 100, "")
if err != nil {
return nil, err
}
api.ClearOutputOnlyFieldsList(protos)
tables := make([]*apiv1alpha.Table, 0)
for _, p := range protos {
t, err := api.ProtoToTable(p)
if err != nil {
return nil, err
}
tables = append(tables, t)
}
return tables, nil
}
func exportViews(apiClient api.ApiClient) ([]*apiv1alpha.View, error) {
protos, err := apiClient.List(&apiv1alpha.View{}, "", 100, "")
if err != nil {
return nil, err
}
api.ClearOutputOnlyFieldsList(protos)
views := make([]*apiv1alpha.View, 0)
for _, p := range protos {
v, err := api.ProtoToView(p)
if err != nil {
return nil, err
}
views = append(views, v)
}
return views, nil
}
func exportMaterialization(apiClient api.ApiClient, materializationName string) (*apiv1alpha.Materialization, error) {
proto, err := apiClient.Get(&apiv1alpha.Materialization{
MaterializationName: materializationName},
)
if err != nil {
return nil, err
}
return api.ProtoToMaterialization(api.ClearOutputOnlyFields(proto))
}
func exportTable(apiClient api.ApiClient, tableName string) (*apiv1alpha.Table, error) {
proto, err := apiClient.Get(&apiv1alpha.Table{
TableName: tableName},
)
if err != nil {
return nil, err
}
return api.ProtoToTable(api.ClearOutputOnlyFields(proto))
}
func exportView(apiClient api.ApiClient, viewName string) (*apiv1alpha.View, error) {
proto, err := apiClient.Get(&apiv1alpha.View{
ViewName: viewName},
)
if err != nil {
return nil, err
}
return api.ProtoToView(api.ClearOutputOnlyFields(proto))
}
var (
getAll bool
filePath string
materializationNames, tableNames, viewNames []string
)
func init() {
exportCmd.Flags().BoolVar(&getAll, "all", false, "set this flag to export all tables, views, and materializations in the system")
exportCmd.Flags().StringVarP(&filePath, "file-path", "f", "", "specify a file path to export the resources to, instead of exporting to std-out")
exportCmd.Flags().StringArrayVarP(&materializationNames, "materialization", "m", []string{}, "the name of the `materialization` resource to export. This flag can be set multiple times to specify multiple materialization.")
exportCmd.Flags().StringArrayVarP(&tableNames, "table", "t", []string{}, "the name of the `table` resource to export. This flag can be set multiple times to specify multiple tables.")
exportCmd.Flags().StringArrayVarP(&viewNames, "view", "v", []string{}, "the name of the `view` resource to export. This flag can be set multiple times to specify multiple views.")
}