-
Notifications
You must be signed in to change notification settings - Fork 364
Fetch remote topo #1622
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
Closed
Closed
Fetch remote topo #1622
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,30 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "strings" | ||
| "errors" | ||
| ) | ||
|
|
||
|
|
||
| func GetRawURL(url string) string { | ||
| if strings.Contains(url, "github.com") { | ||
| raw:=strings.Replace(url, "github.com", "raw.githubusercontent.com", 1) | ||
| return strings.Replace(raw, "/blob", "", 1) | ||
| } | ||
| return url | ||
| } | ||
|
|
||
| func IsGitHubURL(url string) bool { | ||
| return strings.Contains(url, "github") | ||
|
|
||
| } | ||
|
|
||
| // required global variable for tests, otherwise comparison operator fails as error instances were not equal | ||
| var ErrInvalidSuffix = errors.New("valid URL passed in as topology file, but does not end with .yml or .yaml, endpoint must be an actual topology file") | ||
| func CheckSuffix(url string) error { | ||
| // check if topo ends with either .yml or .yaml | ||
| if !strings.HasSuffix(url, ".yml") && !strings.HasSuffix(url, ".yaml") { | ||
| return ErrInvalidSuffix | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or 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,67 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestIsGithubURL(t *testing.T) { | ||
| // tests that github urls are detected | ||
| var tests = []struct { | ||
| input string | ||
| expected bool | ||
| }{ | ||
| {"github.com", true}, | ||
| {"github.com/containers/containerlab/blob/master/README.md", true}, | ||
| {"google.com/containers", false}, | ||
| {"google.com/containers/containerlab/blob/master/README.md", false}, | ||
| {"gitlab.com/containers", false}, | ||
| {"raw.githubusercontent.com/containers", true}, | ||
| } | ||
| for _, test := range tests { | ||
| if output := IsGitHubURL(test.input); output != test.expected { | ||
| t.Error("Test Failed: {} inputted, {} expected, recieved: {}", test.input, test.expected, output) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestGetRawURL(t *testing.T) { | ||
| // tests that github urls are converted to raw urls evething else is left as is | ||
| var tests = []struct { | ||
| input string | ||
| expected string | ||
| }{ | ||
| {"github.com", "raw.githubusercontent.com"}, | ||
| {"github.com/containers/containerlab/blob/master/README.md", "raw.githubusercontent.com/containers/containerlab/master/README.md"}, | ||
| {"google.com/containers", "google.com/containers"}, | ||
| {"google.com/containers/containerlab/blob/master/README.md", "google.com/containers/containerlab/blob/master/README.md"}, | ||
| {"gitlab.com/containers", "gitlab.com/containers"}, | ||
| {"raw.githubusercontent.com/containers", "raw.githubusercontent.com/containers"}, | ||
| } | ||
| for _, test := range tests { | ||
| if output := GetRawURL(test.input); output != test.expected { | ||
| t.Error("Test Failed: {} inputted, {} expected, recieved: {}", test.input, test.expected, output) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestCheckSuffix(t *testing.T) { | ||
| // tests for valid suffix | ||
| var tests = []struct { | ||
| input string | ||
| expected error | ||
| }{ | ||
| {"github.com", ErrInvalidSuffix}, | ||
| {"github.com/containers/containerlab/blob/master/README.md", ErrInvalidSuffix}, | ||
| {"google.com/containers", ErrInvalidSuffix}, | ||
| {"google.com/containers/containerlab/blob/master/README.md", ErrInvalidSuffix}, | ||
| {"gitlab.com/containers", ErrInvalidSuffix}, | ||
| {"raw.githubusercontent.com/containers", ErrInvalidSuffix}, | ||
| {"github.com/containers/containerlab/blob/master/README.yml", nil}, | ||
| {"github.com/containers/containerlab/blob/master/README.yaml", nil}, | ||
| } | ||
| for _, test := range tests { | ||
| if output := CheckSuffix(test.input); output != test.expected { | ||
| t.Error("Test Failed: {} inputted, {} expected, recieved: {}", test.input, test.expected, output) | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This contains in my view is too broad. since also a "raw.githubusercontent.com" url would match.
In cmd/root.go you use this in the switch and consecutively apply the GetRawUrl. Which then does the conversion.
although that would check for "github.com" it should probably be more kind of "consistent".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I'm grasping what you'd like to see from this method.
You mentioned that it would match on raw.githubusercontent.com and you're right. I had planned on it matching on either a generic github.com or the raw.github path to offer flexibility to the end user. It then proceeds to that GetRawURL method where it only converts it if its a generic github.com url else it returns the url without converting anything.
How would you envision this changing?