github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/pkg/packager/composer/pathfixer.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // SPDX-FileCopyrightText: 2021-Present The Jackal Authors 3 4 // Package composer contains functions for composing components within Jackal packages. 5 package composer 6 7 import ( 8 "path/filepath" 9 10 "github.com/Racer159/jackal/src/types" 11 "github.com/defenseunicorns/pkg/helpers" 12 ) 13 14 func makePathRelativeTo(path, relativeTo string) string { 15 if helpers.IsURL(path) { 16 return path 17 } 18 19 return filepath.Join(relativeTo, path) 20 } 21 22 func fixPaths(child *types.JackalComponent, relativeToHead string) { 23 for fileIdx, file := range child.Files { 24 composed := makePathRelativeTo(file.Source, relativeToHead) 25 child.Files[fileIdx].Source = composed 26 } 27 28 for chartIdx, chart := range child.Charts { 29 for valuesIdx, valuesFile := range chart.ValuesFiles { 30 composed := makePathRelativeTo(valuesFile, relativeToHead) 31 child.Charts[chartIdx].ValuesFiles[valuesIdx] = composed 32 } 33 if child.Charts[chartIdx].LocalPath != "" { 34 composed := makePathRelativeTo(chart.LocalPath, relativeToHead) 35 child.Charts[chartIdx].LocalPath = composed 36 } 37 } 38 39 for manifestIdx, manifest := range child.Manifests { 40 for fileIdx, file := range manifest.Files { 41 composed := makePathRelativeTo(file, relativeToHead) 42 child.Manifests[manifestIdx].Files[fileIdx] = composed 43 } 44 for kustomizeIdx, kustomization := range manifest.Kustomizations { 45 composed := makePathRelativeTo(kustomization, relativeToHead) 46 // kustomizations can use non-standard urls, so we need to check if the composed path exists on the local filesystem 47 abs, _ := filepath.Abs(composed) 48 invalid := helpers.InvalidPath(abs) 49 if !invalid { 50 child.Manifests[manifestIdx].Kustomizations[kustomizeIdx] = composed 51 } 52 } 53 } 54 55 for dataInjectionsIdx, dataInjection := range child.DataInjections { 56 composed := makePathRelativeTo(dataInjection.Source, relativeToHead) 57 child.DataInjections[dataInjectionsIdx].Source = composed 58 } 59 60 defaultDir := child.Actions.OnCreate.Defaults.Dir 61 child.Actions.OnCreate.Before = fixActionPaths(child.Actions.OnCreate.Before, defaultDir, relativeToHead) 62 child.Actions.OnCreate.After = fixActionPaths(child.Actions.OnCreate.After, defaultDir, relativeToHead) 63 child.Actions.OnCreate.OnFailure = fixActionPaths(child.Actions.OnCreate.OnFailure, defaultDir, relativeToHead) 64 child.Actions.OnCreate.OnSuccess = fixActionPaths(child.Actions.OnCreate.OnSuccess, defaultDir, relativeToHead) 65 66 // deprecated 67 if child.DeprecatedCosignKeyPath != "" { 68 composed := makePathRelativeTo(child.DeprecatedCosignKeyPath, relativeToHead) 69 child.DeprecatedCosignKeyPath = composed 70 } 71 } 72 73 // fixActionPaths takes a slice of actions and mutates the Dir to be relative to the head node 74 func fixActionPaths(actions []types.JackalComponentAction, defaultDir, relativeToHead string) []types.JackalComponentAction { 75 for actionIdx, action := range actions { 76 var composed string 77 if action.Dir != nil { 78 composed = makePathRelativeTo(*action.Dir, relativeToHead) 79 } else { 80 composed = makePathRelativeTo(defaultDir, relativeToHead) 81 } 82 actions[actionIdx].Dir = &composed 83 } 84 return actions 85 }