Calculate localized path for remote file (#4878)

* Calculate localized path for remote file

* Add documentation
This commit is contained in:
Anna Song
2022-11-28 09:36:06 -08:00
committed by GitHub
parent adce67301b
commit 0eff094faf
2 changed files with 70 additions and 4 deletions

View File

@@ -5,6 +5,7 @@ package localizer
import (
"log"
"net/url"
"path/filepath"
"strings"
@@ -120,8 +121,25 @@ func cleanFilePath(fSys filesys.FileSystem, root filesys.ConfirmedDir, file stri
return locPath
}
// locFilePath returns the relative localized path of validated file url fileURL
// TODO(annasong): implement
func locFilePath(_ string) string {
return filepath.Join(LocalizeDir, "")
// locFilePath converts a URL to its localized form, e.g.
// https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/api/krusty/testdata/localize/simple/service.yaml ->
// localized-files/raw.githubusercontent.com/kubernetes-sigs/kustomize/master/api/krusty/testdata/localize/simple/service.yaml.
//
// fileURL must be a validated file URL.
func locFilePath(fileURL string) string {
// File urls must have http or https scheme, so it is safe to use url.Parse.
u, err := url.Parse(fileURL)
if err != nil {
log.Fatalf("cannot parse validated file url %q: %s", fileURL, err.Error())
}
// Percent-encodings should be preserved in case sub-delims have special meaning.
// Extraneous '..' parent directory dot-segments should be removed.
path := filepath.Join(string(filepath.Separator), filepath.FromSlash(u.EscapedPath()))
// The host should not include userinfo or port.
// Raw github urls are the only type of file urls kustomize officially accepts.
// In this case, the path already consists of org, repo, version, and path in repo, in order,
// so we can use it as is.
return filepath.Join(LocalizeDir, u.Hostname(), path)
}