/
airflow.go
110 lines (89 loc) · 2.81 KB
/
airflow.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
package airflow
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/pkg/errors"
"github.com/iancoleman/strcase"
"github.com/astronomer/astro-cli/airflow/include"
"github.com/astronomer/astro-cli/messages"
"github.com/astronomer/astro-cli/pkg/fileutil"
"github.com/astronomer/astro-cli/version"
)
func initDirs(root string, dirs []string) error {
// Create the dirs
for _, dir := range dirs {
// Create full path to directory
fullpath := filepath.Join(root, dir)
// Move on if already exists
_, err := fileutil.Exists(fullpath)
if err != nil {
return errors.Wrapf(err, "failed to check existence of '%s'", fullpath)
}
// Create directory
if err := os.MkdirAll(dir, 0777); err != nil {
return errors.Wrapf(err, "failed to create dir '%s'", dir)
}
}
return nil
}
func initFiles(root string, files map[string]string) error {
// Create the files
for file, content := range files {
// Create full path to file
fullpath := filepath.Join(root, file)
// Move on if already exists
fileExist, err := fileutil.Exists(fullpath)
if err != nil {
return errors.Wrapf(err, "failed to check existence of '%s'", fullpath)
}
if fileExist {
continue
}
// Write files out
if err := fileutil.WriteStringToFile(fullpath, content); err != nil {
return errors.Wrapf(err, "failed to create file '%s'", fullpath)
}
}
return nil
}
// Init will scaffold out a new airflow project
func Init(path string, airflowVersion string) error {
// List of directories to create
dirs := []string{"dags", "plugins", "include"}
// Map of files to create
files := map[string]string{
".dockerignore": include.Dockerignore,
"Dockerfile": fmt.Sprintf(include.Dockerfile, version.GetTagFromVersion(airflowVersion)),
".gitignore": include.Gitignore,
"packages.txt": "",
"requirements.txt": "",
".env": "",
"airflow_settings.yaml": include.Settingsyml,
"dags/example-dag.py": include.Exampledag,
"plugins/example-plugin.py": include.ExamplePlugin,
}
// Initailize directories
if err := initDirs(path, dirs); err != nil {
return errors.Wrap(err, "failed to create project directories")
}
// Initialize files
if err := initFiles(path, files); err != nil {
return errors.Wrap(err, "failed to create project files")
}
return nil
}
func validateOrCreateProjectName(path, projectName string) (string, error) {
if len(projectName) != 0 {
projectNameValid := regexp.MustCompile(`^[A-Za-z0-9]([A-Za-z0-9_-]*[A-Za-z0-9])?$`).MatchString
if !projectNameValid(projectName) {
return "", errors.New(messages.CONFIG_PROJECT_NAME_ERROR)
}
} else {
projectDirectory := filepath.Base(path)
return strings.Replace(strcase.ToSnake(projectDirectory), "_", "-", -1), nil
}
return projectName, nil
}