|
| 1 | +/* |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +contributor license agreements. See the NOTICE file distributed with |
| 4 | +this work for additional information regarding copyright ownership. |
| 5 | +The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +(the "License"); you may not use this file except in compliance with |
| 7 | +the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package tasks |
| 19 | + |
| 20 | +import ( |
| 21 | + "net/url" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/apache/incubator-devlake/core/errors" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + DbtProjectBaseDirConfigKey = "DBT_PROJECTS_DIR" |
| 31 | + dbtProjectBaseDirName = "devlake-dbt-projects" |
| 32 | +) |
| 33 | + |
| 34 | +func PrepareOptions(op *DbtOptions, configuredBaseDir string) errors.Error { |
| 35 | + if op == nil { |
| 36 | + return errors.Default.New("dbt options are required") |
| 37 | + } |
| 38 | + |
| 39 | + baseDir, err := normalizeBaseDir(configuredBaseDir) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + op.ProjectBaseDir = baseDir |
| 44 | + op.ProjectPath = strings.TrimSpace(op.ProjectPath) |
| 45 | + op.ProjectGitURL = strings.TrimSpace(op.ProjectGitURL) |
| 46 | + |
| 47 | + if op.ProjectGitURL != "" { |
| 48 | + if err := validateProjectGitURL(op.ProjectGitURL); err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + op.ProjectPath = "" |
| 52 | + op.ManagedProjectDir = true |
| 53 | + return nil |
| 54 | + } |
| 55 | + |
| 56 | + if op.ProjectPath == "" { |
| 57 | + return errors.Default.New("projectPath is required for local dbt projects") |
| 58 | + } |
| 59 | + |
| 60 | + projectPath, err := normalizePathWithinBase(baseDir, op.ProjectPath) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + op.ProjectPath = projectPath |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +func normalizeBaseDir(configuredBaseDir string) (string, errors.Error) { |
| 69 | + baseDir := strings.TrimSpace(configuredBaseDir) |
| 70 | + if baseDir == "" { |
| 71 | + baseDir = filepath.Join(os.TempDir(), dbtProjectBaseDirName) |
| 72 | + } |
| 73 | + baseDir, err := filepath.Abs(filepath.Clean(baseDir)) |
| 74 | + if err != nil { |
| 75 | + return "", errors.Convert(err) |
| 76 | + } |
| 77 | + return baseDir, nil |
| 78 | +} |
| 79 | + |
| 80 | +func normalizePathWithinBase(baseDir string, candidate string) (string, errors.Error) { |
| 81 | + normalizedPath, err := filepath.Abs(filepath.Clean(candidate)) |
| 82 | + if err != nil { |
| 83 | + return "", errors.Convert(err) |
| 84 | + } |
| 85 | + rel, err := filepath.Rel(baseDir, normalizedPath) |
| 86 | + if err != nil { |
| 87 | + return "", errors.Convert(err) |
| 88 | + } |
| 89 | + if rel == ".." || strings.HasPrefix(rel, ".."+string(os.PathSeparator)) { |
| 90 | + return "", errors.Default.New("projectPath must stay within " + baseDir) |
| 91 | + } |
| 92 | + return normalizedPath, nil |
| 93 | +} |
| 94 | + |
| 95 | +func validateProjectGitURL(rawURL string) errors.Error { |
| 96 | + u, err := url.Parse(rawURL) |
| 97 | + if err != nil { |
| 98 | + return errors.Convert(err) |
| 99 | + } |
| 100 | + if u.Scheme != "https" && u.Scheme != "ssh" { |
| 101 | + return errors.Default.New("projectGitURL must use https:// or ssh://") |
| 102 | + } |
| 103 | + if u.Host == "" { |
| 104 | + return errors.Default.New("projectGitURL must include a hostname") |
| 105 | + } |
| 106 | + if u.Path == "" || u.Path == "/" { |
| 107 | + return errors.Default.New("projectGitURL must include a repository path") |
| 108 | + } |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +func ensureProjectBaseDir(baseDir string) (string, errors.Error) { |
| 113 | + baseDir, err := normalizeBaseDir(baseDir) |
| 114 | + if err != nil { |
| 115 | + return "", err |
| 116 | + } |
| 117 | + if err := os.MkdirAll(baseDir, 0o755); err != nil { |
| 118 | + return "", errors.Convert(err) |
| 119 | + } |
| 120 | + return baseDir, nil |
| 121 | +} |
| 122 | + |
| 123 | +func CleanupManagedProjectDir(op *DbtOptions) errors.Error { |
| 124 | + if op == nil || !op.ManagedProjectDir || op.ProjectPath == "" { |
| 125 | + return nil |
| 126 | + } |
| 127 | + projectPath, err := normalizePathWithinBase(op.ProjectBaseDir, op.ProjectPath) |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + return errors.Convert(os.RemoveAll(projectPath)) |
| 132 | +} |
0 commit comments