Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 928 #934

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/files/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ func NewSortedFilesFromPaths(paths []string, opts SymlinkAllowOpts) ([]*File, er

relativePath := ""
pathPieces := strings.Split(path, "=")
if strings.HasPrefix(pathPieces[0], "http://") || strings.HasPrefix(pathPieces[0], "https://") {
pathPieces = []string{path}
} else if len(pathPieces) > 1 && (strings.HasPrefix(pathPieces[1], "http://") || strings.HasPrefix(pathPieces[1], "https://")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the scenario where this if statement is triggered?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joaopapereira this is a safetynet for the use case that the file's path is overwritten on the command line; cf. the case 2 part in L60.

Example: want to cover both scenarios of

  • path = "myOverwrittenPath=https://example.com?hello=world"
  • path = "https://example.com?hello=world"

pathPieces[1] = strings.Join(pathPieces[1:], "=")
pathPieces = pathPieces[0:2]
}

switch len(pathPieces) {
case 1:
Expand Down
27 changes: 27 additions & 0 deletions pkg/files/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 The Carvel Authors.
// SPDX-License-Identifier: Apache-2.0

package files_test

import (
"carvel.dev/ytt/pkg/files"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)

func TestNewSortedFilesFromPaths(t *testing.T) {
inputPaths := []string{
"../yamltemplate/filetests/def.tpltest",
"https://example.org/test.yaml?hello=world&foo=bar",
"an/overwritten/path/to/file.yaml=https://example.org/test.yaml?hello=world&foo=bar"}
filesToProcess, err := files.NewSortedFilesFromPaths(inputPaths, files.SymlinkAllowOpts{true, []string{}})

require.NoError(t, err)

assert.Equal(t, filesToProcess[0].RelativePath(), "def.tpltest")
assert.Equal(t, filesToProcess[1].RelativePath(), "test.yaml")
assert.Equal(t, filesToProcess[1].Description(), "HTTP URL 'https://example.org/test.yaml?hello=world&foo=bar'")
assert.Equal(t, filesToProcess[2].RelativePath(), "an/overwritten/path/to/file.yaml")
assert.Equal(t, filesToProcess[2].Description(), "HTTP URL 'https://example.org/test.yaml?hello=world&foo=bar'")
}
4 changes: 3 additions & 1 deletion pkg/files/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ func (s HTTPSource) Description() string {
return fmt.Sprintf("HTTP URL '%s'", s.url)
}

func (s HTTPSource) RelativePath() (string, error) { return path.Base(s.url), nil }
func (s HTTPSource) RelativePath() (string, error) {
return path.Base(strings.Split(s.url, "?")[0]), nil
}

func (s HTTPSource) Bytes() ([]byte, error) {
resp, err := s.Client.Get(s.url)
Expand Down
Loading