Skip to content

Commit

Permalink
Add ability to custom handle redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
abogdanov37 committed Apr 24, 2024
1 parent 2e6171a commit de1f38e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*.DS_store

.bin
# VSCode
.vscode/

# Hugo site
publishedSite/
Expand Down
9 changes: 9 additions & 0 deletions api/internal/loader/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ var (
ErrHTTP = errors.Errorf("HTTP Error")
ErrRtNotDir = errors.Errorf("must build at directory")
)

type RedirectionError struct {
NewPath string
Msg string
}

func (e *RedirectionError) Error() string {
return e.Msg
}
16 changes: 12 additions & 4 deletions api/internal/loader/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,19 @@ func (fl *FileLoader) httpClientGetContent(path string) ([]byte, error) {
defer resp.Body.Close()
// response unsuccessful
if resp.StatusCode < 200 || resp.StatusCode > 299 {
_, err = git.NewRepoSpecFromURL(path)
if err == nil {
return nil, errors.Errorf("URL is a git repository")
if resp.StatusCode == 300 {

Check failure on line 321 in api/internal/loader/fileloader.go

View workflow job for this annotation

GitHub Actions / Lint

mnd: Magic number: 300, in <condition> detected (gomnd)
var newPath string = resp.Header.Get("Location")
return nil, &RedirectionError{
Msg: "Response is redirect",
NewPath: newPath,
}
} else {
_, err = git.NewRepoSpecFromURL(path)
if err == nil {
return nil, errors.Errorf("URL is a git repository")
}
return nil, fmt.Errorf("%w: status code %d (%s)", ErrHTTP, resp.StatusCode, http.StatusText(resp.StatusCode))
}
return nil, fmt.Errorf("%w: status code %d (%s)", ErrHTTP, resp.StatusCode, http.StatusText(resp.StatusCode))
}
content, err := io.ReadAll(resp.Body)
return content, errors.Wrap(err)
Expand Down
9 changes: 8 additions & 1 deletion api/internal/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,14 @@ func (kt *KustTarget) accumulateResources(
if errors.Is(errF, load.ErrHTTP) {
return nil, errF
}
ldr, err := kt.ldr.New(path)
var redErr *load.RedirectionError
var ldr ifc.Loader
var err error
if errors.As(errF, &redErr) {
ldr, err = kt.ldr.New(redErr.NewPath)
} else {
ldr, err = kt.ldr.New(path)
}
if err != nil {
// If accumulateFile found malformed YAML and there was a failure
// loading the resource as a base, then the resource is likely a
Expand Down

0 comments on commit de1f38e

Please sign in to comment.