-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: local replace package dependency (#1521)
local replace package dependency YES This is part of the [Forked cleanup package project](https://www.notion.so/kurtosistech/Forked-Package-Cleanup-16d86c4e274547b28496f17154bf3d62)
- Loading branch information
Showing
7 changed files
with
170 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...ainer/server/startosis_engine/startosis_packages/git_package_content_provider/locators.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package git_package_content_provider | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
"strings" | ||
) | ||
|
||
const ( | ||
dotRelativePathIndicatorString = "." | ||
subStrNotPresentIndicator = -1 | ||
) | ||
|
||
func isLocalLocator(locator string) bool { | ||
if strings.HasPrefix(locator, osPathSeparatorString) || strings.HasPrefix(locator, dotRelativePathIndicatorString) { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func isSamePackageLocalAbsoluteLocator(locator string, parentPackageId string) bool { | ||
return strings.HasPrefix(locator, parentPackageId) | ||
} | ||
|
||
func replaceAbsoluteLocator(absoluteLocator string, packageReplaceOptions map[string]string) string { | ||
if absoluteLocator == "" { | ||
return absoluteLocator | ||
} | ||
|
||
found, packageToBeReplaced, replaceWithPackage := findPackageReplace(absoluteLocator, packageReplaceOptions) | ||
|
||
if found { | ||
// we skip if it's a local replace because we will use the same absolute locator | ||
// due the file was already uploaded in the enclave's package cache | ||
if isLocalLocator(replaceWithPackage) { | ||
return absoluteLocator | ||
} | ||
replacedAbsoluteLocator := strings.Replace(absoluteLocator, packageToBeReplaced, replaceWithPackage, onlyOneReplace) | ||
logrus.Debugf("absoluteLocator '%s' replaced with '%s'", absoluteLocator, replacedAbsoluteLocator) | ||
return replacedAbsoluteLocator | ||
} | ||
|
||
return absoluteLocator | ||
} | ||
|
||
func findPackageReplace(absoluteLocator string, packageReplaceOptions map[string]string) (bool, string, string) { | ||
if len(packageReplaceOptions) == 0 { | ||
return false, "", "" | ||
} | ||
|
||
pathToAnalyze := absoluteLocator | ||
for { | ||
numberSlashes := strings.Count(pathToAnalyze, urlPathSeparator) | ||
|
||
// check for the minimal path e.g.: github.com/org/package | ||
if numberSlashes < minimumSubPathsForValidGitURL { | ||
break | ||
} | ||
lastIndex := strings.LastIndex(pathToAnalyze, urlPathSeparator) | ||
|
||
packageToBeReplaced := pathToAnalyze[:lastIndex] | ||
replaceWithPackage, ok := packageReplaceOptions[packageToBeReplaced] | ||
if ok { | ||
logrus.Debugf("dependency replace found for '%s', package '%s' will be replaced with '%s'", absoluteLocator, packageToBeReplaced, replaceWithPackage) | ||
return true, packageToBeReplaced, replaceWithPackage | ||
} | ||
|
||
pathToAnalyze = packageToBeReplaced | ||
} | ||
|
||
return false, "", "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters