forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply.go
More file actions
142 lines (124 loc) · 3.68 KB
/
Copy pathapply.go
File metadata and controls
142 lines (124 loc) · 3.68 KB
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
// Copyright 2018 The Feast Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"context"
"errors"
"fmt"
"io/ioutil"
"path/filepath"
"github.com/gojek/feast/cli/feast/pkg/parse"
"github.com/gojek/feast/protos/generated/go/feast/core"
"github.com/spf13/cobra"
)
// applyCmd represents the apply command
var applyCmd = &cobra.Command{
Use: "apply [resource] [filepaths...]",
Short: "Apply a resource given one or many yaml files.",
Long: `Apply a resource from one or multiple yamls.
Valid resources include:
- entity
- feature
- featureGroup
- storage
Examples:
- feast apply entity entity.yml
- feast apply storage storage1.yml storage2.yml
- feast apply feature *-feature.yml`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return cmd.Help()
}
if len(args) < 2 {
fmt.Println(args)
return errors.New("invalid number of arguments for apply command")
}
initConn()
ctx := context.Background()
coreCli := core.NewCoreServiceClient(coreConn)
resource := args[0]
paths := args[1:]
for _, fp := range paths {
if isYaml(fp) {
fmt.Printf("Applying %s at %s\n", resource, fp)
regID, err := apply(ctx, coreCli, resource, fp)
if err != nil {
return fmt.Errorf("failed to apply %s at path %s: %v", resource, fp, err)
}
fmt.Printf("Successfully applied %s %s\n", resource, regID)
}
}
return nil
},
}
func init() {
rootCmd.AddCommand(applyCmd)
}
func apply(ctx context.Context, coreCli core.CoreServiceClient, resource string, fileLocation string) (string, error) {
yml, err := ioutil.ReadFile(fileLocation)
if err != nil {
return "", fmt.Errorf("error reading file at %s: %v", fileLocation, err)
}
switch resource {
case "feature":
return applyFeature(ctx, coreCli, yml)
case "featureGroup":
return applyFeatureGroup(ctx, coreCli, yml)
case "entity":
return applyEntity(ctx, coreCli, yml)
case "storage":
return applyStorage(ctx, coreCli, yml)
default:
return "", fmt.Errorf("invalid resource %s: please choose one of [feature, featureGroup, entity, storage]", resource)
}
}
func applyFeature(ctx context.Context, coreCli core.CoreServiceClient, yml []byte) (string, error) {
fs, err := parse.YamlToFeatureSpec(yml)
if err != nil {
return "", err
}
_, err = coreCli.ApplyFeature(ctx, fs)
return fs.GetId(), err
}
func applyFeatureGroup(ctx context.Context, coreCli core.CoreServiceClient, yml []byte) (string, error) {
fgs, err := parse.YamlToFeatureGroupSpec(yml)
if err != nil {
return "", err
}
_, err = coreCli.ApplyFeatureGroup(ctx, fgs)
return fgs.GetId(), err
}
func applyEntity(ctx context.Context, coreCli core.CoreServiceClient, yml []byte) (string, error) {
es, err := parse.YamlToEntitySpec(yml)
if err != nil {
return "", err
}
_, err = coreCli.ApplyEntity(ctx, es)
return es.GetName(), err
}
func applyStorage(ctx context.Context, coreCli core.CoreServiceClient, yml []byte) (string, error) {
ss, err := parse.YamlToStorageSpec(yml)
if err != nil {
return "", err
}
_, err = coreCli.ApplyStorage(ctx, ss)
return ss.GetId(), err
}
func isYaml(path string) bool {
ext := filepath.Ext(path)
if ext == ".yaml" || ext == ".yml" {
return true
}
return false
}