-
Notifications
You must be signed in to change notification settings - Fork 1
/
internal.go
99 lines (81 loc) · 2.37 KB
/
internal.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
package cmd
import (
"encoding/json"
"fmt"
"github.com/bruin-data/bruin/pkg/git"
"github.com/bruin-data/bruin/pkg/path"
"github.com/bruin-data/bruin/pkg/pipeline"
color2 "github.com/fatih/color"
"github.com/urfave/cli/v2"
)
func Internal() *cli.Command {
return &cli.Command{
Name: "internal",
Hidden: true,
Subcommands: []*cli.Command{
ParseAsset(),
},
}
}
func ParseAsset() *cli.Command {
return &cli.Command{
Name: "parse-asset",
Usage: "parse a single Bruin asset",
ArgsUsage: "[path to the asset definition]",
Action: func(c *cli.Context) error {
r := ParseCommand{
builder: DefaultPipelineBuilder,
errorPrinter: errorPrinter,
}
return r.Run(c.Args().Get(0))
},
}
}
type ParseCommand struct {
builder taskCreator
errorPrinter *color2.Color
}
func (r *ParseCommand) Run(assetPath string) error {
defer RecoverFromPanic()
if assetPath == "" {
errorPrinter.Printf("Please give an asset path to parse: bruin render <path to the asset file>)\n")
return cli.Exit("", 1)
}
asset, err := r.builder.CreateAssetFromFile(assetPath)
if err != nil {
errorPrinter.Printf("Failed to build asset: %v\n", err.Error())
return cli.Exit("", 1)
}
if asset == nil {
errorPrinter.Printf("The given file path doesn't seem to be a Bruin asset definition: '%s'\n", assetPath)
return cli.Exit("", 1)
}
pipelinePath, err := path.GetPipelineRootFromTask(assetPath, pipelineDefinitionFile)
if err != nil {
r.errorPrinter.Printf("Failed to find the pipeline this asset belongs to: '%s'\n", assetPath)
return cli.Exit("", 1)
}
foundPipeline, err := DefaultPipelineBuilder.CreatePipelineFromPath(pipelinePath)
if err != nil {
r.errorPrinter.Println("failed to build pipeline, are you sure you have referred the right path?")
r.errorPrinter.Println("\nHint: You need to run this command with a path to the asset file itself directly, and it needs to be inside a pipeline.")
return cli.Exit("", 1)
}
repo, err := git.FindRepoFromPath(pipelinePath)
if err != nil {
r.errorPrinter.Println(err)
return cli.Exit("", 1)
}
foundPipeline.Assets = nil
js, err := json.Marshal(struct {
Asset *pipeline.Asset `json:"asset"`
Pipeline *pipeline.Pipeline `json:"pipeline"`
Repo *git.Repo `json:"repo"`
}{
Asset: asset,
Pipeline: foundPipeline,
Repo: repo,
})
fmt.Println(string(js))
return err
}