/
init.go
143 lines (123 loc) · 3.66 KB
/
init.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
package cmd
import (
"fmt"
fs2 "io/fs"
"log"
"os"
path2 "path"
"path/filepath"
"strings"
"github.com/bruin-data/bruin/pkg/config"
"github.com/bruin-data/bruin/templates"
"github.com/spf13/afero"
"github.com/urfave/cli/v2"
)
const (
DefaultTemplate = "default"
DefaultFolderName = "bruin-pipeline"
)
func Init() *cli.Command {
folders, err := templates.Templates.ReadDir(".")
if err != nil {
panic("Error retrieving bruin templates")
}
templateList := make([]string, 0)
for _, entry := range folders {
if entry.IsDir() {
templateList = append(templateList, entry.Name())
}
}
return &cli.Command{
Name: "init",
Usage: "init a Bruin pipeline",
ArgsUsage: fmt.Sprintf(
"[template name to be used: %s] [name of the folder where the pipeline will be created]",
strings.Join(templateList, "|"),
),
Flags: []cli.Flag{},
Action: func(c *cli.Context) error {
defer func() {
if err := recover(); err != nil {
log.Println("=======================================")
log.Println("Bruin encountered an unexpected error, please report the issue to the Bruin team.")
log.Println(err)
log.Println("=======================================")
}
}()
templateName := c.Args().Get(0)
if templateName == "" {
templateName = DefaultTemplate
}
_, err = templates.Templates.ReadDir(templateName)
if err != nil {
errorPrinter.Printf("Template '%s' not found\n", templateName)
return cli.Exit("", 1)
}
inputPath := c.Args().Get(1)
if inputPath == "" {
if templateName == DefaultTemplate {
inputPath = DefaultFolderName
} else {
inputPath = templateName
}
}
// Check if the folder already exists
if _, err := os.Stat(inputPath); !os.IsNotExist(err) {
errorPrinter.Printf("The folder %s already exists, please choose a different name\n", inputPath)
return cli.Exit("", 1)
}
dir, _ := filepath.Split(inputPath)
if dir != "" {
errorPrinter.Printf("Traversing up or down in the folder structure is not allowed, provide base folder name only.\n")
return cli.Exit("", 1)
}
err := os.Mkdir(inputPath, 0o755)
if err != nil {
errorPrinter.Printf("Failed to create the folder %s: %v\n", inputPath, err)
return cli.Exit("", 1)
}
err = fs2.WalkDir(templates.Templates, templateName, func(path string, d fs2.DirEntry, err error) error {
if err != nil {
return err
}
// Walk returns the root as if it was its own content
if path == templateName {
return nil
}
// Walk returns the root as if it was its own content
if d.IsDir() {
return nil
}
fileContents, err := templates.Templates.ReadFile(path)
if err != nil {
return err
}
relativePath, baseName := filepath.Split(path)
relativePath = strings.TrimPrefix(relativePath, templateName)
absolutePath := inputPath + relativePath
// ignore the error
err = os.MkdirAll(absolutePath, os.ModePerm)
if err != nil {
errorPrinter.Printf("Could not create the %s folder: %v\n", absolutePath, err)
return err
}
err = os.WriteFile(filepath.Join(absolutePath, baseName), fileContents, 0o644) //nolint:gosec
if err != nil {
errorPrinter.Printf("Could not write the %s file: %v\n", filepath.Join(absolutePath, baseName), err)
return err
}
_, err = config.LoadOrCreate(afero.NewOsFs(), path2.Join(inputPath, ".bruin.yml"))
if err != nil {
errorPrinter.Printf("Could not write .bruin.yml file: %v\n", err)
return err
}
return nil
})
if err != nil {
errorPrinter.Printf("Could not copy template %s: %s\n", templateName, err)
return cli.Exit("", 1)
}
return nil
},
}
}