/
resource.go
77 lines (70 loc) · 1.75 KB
/
resource.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
package cmd
import (
"fmt"
"github.com/gojek/optimus-extension-valor/recipe"
"github.com/spf13/cobra"
)
type resourceArg struct {
Name string
Format string
Type string
Path string
}
func getResourceCmd() *cobra.Command {
var (
name string
format string
_type string
path string
)
resourceCmd := &cobra.Command{
Use: "resource",
Short: "Execute pipeline for a specific resource",
RunE: func(cmd *cobra.Command, args []string) error {
enrich := func(rcp *recipe.Recipe) error {
if err := enrichWithBatchSize(rcp); err != nil {
return err
}
return enrichWithArg(rcp, &resourceArg{
Name: name,
Format: format,
Type: _type,
Path: path,
})
}
return executePipeline(recipePath, progressType, enrich)
},
}
resourceCmd.Flags().StringVarP(&name, "name", "n", "", "name of the resource recipe to be used")
resourceCmd.Flags().StringVarP(&format, "format", "f", "", "format of the resource")
resourceCmd.Flags().StringVarP(&_type, "type", "t", "", "type of the resource")
resourceCmd.Flags().StringVarP(&path, "path", "p", "", "path of the resource")
resourceCmd.MarkFlagRequired("name")
return resourceCmd
}
func enrichWithArg(rcp *recipe.Recipe, arg *resourceArg) error {
if arg.Name == "" {
return nil
}
var resourceRcp *recipe.Resource
for _, rsc := range rcp.Resources {
if rsc.Name == arg.Name {
resourceRcp = rsc
if arg.Path != "" {
resourceRcp.Path = arg.Path
}
if arg.Format != "" {
resourceRcp.Format = arg.Format
}
if arg.Type != "" {
resourceRcp.Type = arg.Type
}
break
}
}
if resourceRcp == nil {
return fmt.Errorf("resource recipe [%s] is not found", arg.Name)
}
rcp.Resources = []*recipe.Resource{resourceRcp}
return nil
}