/
visualize.go
66 lines (59 loc) · 1.75 KB
/
visualize.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
package cmd
import (
"fmt"
util "github.com/RyanSiu1995/gcb-visualizer/internal/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
"path/filepath"
"strings"
)
var supportedFormat = []string{".dot", ".jpg", ".jpeg", ".png"}
var output = ""
func init() {
if _, exist := os.LookupEnv("DEBUG"); exist {
log.SetLevel(log.DebugLevel)
}
}
func init() {
var visualize = &cobra.Command{
Use: "visualize [cloud build YAML path]",
Short: "Visualize the given cloud build pipeline",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
filePath := args[0]
if _, err := os.Stat(filePath); err != nil {
fmt.Printf("File %s does not exist!\n", filePath)
os.Exit(1)
}
cloudbuild, err := util.ParseYaml(filePath)
if err != nil {
fmt.Printf("Cannot parse the YAML file %s\n", filePath)
os.Exit(1)
}
graph := util.BuildStepsToDAG(cloudbuild.Steps)
if output == "" {
err = util.Visualize(graph)
if err != nil {
fmt.Println("There is error during rendering the image... Use DEBUG flag to get more information...")
log.Debug(err.Error())
os.Exit(1)
}
} else {
ext := filepath.Ext(output)
if !util.Contains(supportedFormat, strings.ToLower(ext)) {
fmt.Printf("%s is not the supported format. Please specify the correct extension\n", ext)
os.Exit(1)
}
err = util.SaveGraph(graph, output)
if err != nil {
fmt.Println("There is error during rendering the image... Use DEBUG flag to get more information...")
log.Debug(err.Error())
os.Exit(1)
}
}
},
}
visualize.Flags().StringVarP(&output, "output", "o", "", "Specify the output file, a temp png image by default (Support: dot, jpg and png)")
rootCmd.AddCommand(visualize)
}