/
data-source.go
204 lines (203 loc) · 5.93 KB
/
data-source.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package apbr
//
//import (
// "context"
// "github.com/apibrew/apibrew/pkg/apbr/flags"
// "github.com/apibrew/apibrew/pkg/formats/yamlformat"
// "github.com/apibrew/apibrew/pkg/model"
// "github.com/apibrew/apibrew/pkg/service/annotations"
// "github.com/apibrew/apibrew/pkg/stub"
// "github.com/olekukonko/tablewriter"
// log "github.com/sirupsen/logrus"
// "github.com/spf13/cobra"
// "os"
// "strconv"
// "strings"
//)
//
//var dataSourceId *string
//var dataSourceName *string
//var dataSourcePrepareEntityNames *string
//var dataSourcePrepareCatalogs *string
//
//func prepareResourcesFromDataSource(ctx context.Context, dataSource *resource_model.DataSource) <-chan *model.Resource {
// ch := make(chan *model.Resource)
//
// go func() {
// defer func() {
// close(ch)
// }()
// catalogs := check2(GetClient().GetDataSourceClient().ListEntities(ctx, &stub.ListEntitiesRequest{
// Token: GetClient().GetToken(),
// Id: dataSource.Id,
// })).Catalogs
//
// for _, catalog := range catalogs {
//
// if *dataSourcePrepareCatalogs != "*" {
// catalogs := strings.Split(*dataSourcePrepareCatalogs, ",")
//
// found := false
//
// for _, cc := range catalogs {
// if cc == catalog.Name {
// found = true
// break
// }
// }
//
// if !found {
// continue
// }
// }
//
// for _, entity := range catalog.Entities {
// if *dataSourcePrepareEntityNames != "*" {
// entities := strings.Split(*dataSourcePrepareEntityNames, ",")
//
// found := false
//
// for _, ec := range entities {
// if ec == entity.Name {
// found = true
// break
// }
// }
//
// if !found {
// continue
// }
// }
//
// res := check2(GetClient().GetDataSourceClient().PrepareResourceFromEntity(ctx, &stub.PrepareResourceFromEntityRequest{
// Token: GetClient().GetToken(),
// Id: dataSource.Id,
// Catalog: catalog.Name,
// Entity: entity.Name,
// }))
//
// resource := res.Resource
//
// if entity.ReadOnly {
// annotations.Enable(resource, annotations.DisableBackup)
// }
//
// ch <- resource
// }
// }
// }()
//
// return ch
//}
//
//var dataSourceCmd = &cobra.Command{
// Use: "data-source",
// Short: "data-source - Data source related operations",
//}
//
//var dataSourceStatusCmd = &cobra.Command{
// Use: "status",
// Short: "status - Data source status",
// Run: func(cmd *cobra.Command, args []string) {
// dataSource := loadDataSourceByNameOrId(cmd.Context(), *dataSourceId, *dataSourceName)
//
// resp := check2(GetClient().GetDataSourceClient().Status(cmd.Context(), &stub.StatusRequest{
// Token: GetClient().GetToken(),
// Id: dataSource.Id,
// }))
//
// log.Println("DataSource name: " + dataSource.Name)
// log.Println("ConnectionAlreadyInitiated: " + strconv.FormatBool(resp.ConnectionAlreadyInitiated))
// log.Println("TestConnection: " + strconv.FormatBool(resp.TestConnection))
// },
//}
//
//var dataSourceListEntitiesCmd = &cobra.Command{
// Use: "list-entities",
// Short: "list-entities - List existing entities in data source",
// Run: func(cmd *cobra.Command, args []string) {
// dataSource := loadDataSourceByNameOrId(cmd.Context(), *dataSourceId, *dataSourceName)
//
// catalogs := check2(GetClient().GetDataSourceClient().ListEntities(cmd.Context(), &stub.ListEntitiesRequest{
// Token: GetClient().GetToken(),
// Id: dataSource.Id,
// })).Catalogs
//
// table := tablewriter.NewWriter(os.Stdout)
// table.SetHeader([]string{"Catalog", "Entity", "Editable"})
// table.SetAutoWrapText(false)
// table.SetAutoFormatHeaders(true)
// table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
// table.SetAlignment(tablewriter.ALIGN_LEFT)
// table.SetCenterSeparator("")
// table.SetColumnSeparator("")
// table.SetRowSeparator("")
// table.SetHeaderLine(false)
// table.SetBorder(false)
// table.SetTablePadding("\t") // pad with tabs
// table.SetNoWhiteSpace(true)
//
// for _, catalog := range catalogs {
// for _, entity := range catalog.Entities {
// status := "editable"
//
// if entity.ReadOnly {
// status = "read-only"
// }
//
// table.Append([]string{
// catalog.Name, entity.Name, status,
// })
// }
// }
//
// table.Render() // Send output
// },
//}
//
//var dataSourcePrepareCmd = &cobra.Command{
// Use: "prepare",
// Short: "prepare - Prepare resources from existing data source entities",
// RunE: func(cmd *cobra.Command, args []string) error {
// dataSource := loadDataSourceByNameOrId(cmd.Context(), *dataSourceId, *dataSourceName)
//
// ch := prepareResourcesFromDataSource(cmd.Context(), dataSource)
//
// var yamlWriter = yamlformat.NewWriter(os.Stdout, nil)
//
// var overrideConfig = new(flags.OverrideConfig)
// overrideFlags.Parse(overrideConfig, cmd, args)
//
// for item := range ch {
// if overrideConfig.Namespace != "" {
// item.Namespace = overrideConfig.Namespace
// }
//
// if overrideConfig.DataSource != "" {
// item.SourceConfig.DataSource = overrideConfig.DataSource
// }
//
// err := yamlWriter.WriteResource(item)
//
// if err != nil {
// return err
// }
// }
//
// return nil
// },
//}
//
//func init() {
// dataSourceName = dataSourceCmd.PersistentFlags().String("name", "", "Data Source name")
// dataSourceId = dataSourceCmd.PersistentFlags().String("id", "", "Data Source Id")
//
// dataSourcePrepareEntityNames = dataSourcePrepareCmd.PersistentFlags().StringP("entity", "e", "*", "Select entities for resource preparation, default value is * for selection of all entities, you can use comma to select multiple entities")
// dataSourcePrepareCatalogs = dataSourcePrepareCmd.PersistentFlags().StringP("catalog", "c", "*", "Select catalogs for resource preparation, default value is * for selection of all catalogs, you can use comma to select multiple catalogs")
//
// dataSourceCmd.AddCommand(dataSourceStatusCmd)
// dataSourceCmd.AddCommand(dataSourcePrepareCmd)
// dataSourceCmd.AddCommand(dataSourceListEntitiesCmd)
//
// overrideFlags.Declare(dataSourcePrepareCmd)
//}