-
Notifications
You must be signed in to change notification settings - Fork 223
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
Pull templates from store when not present #1004
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2ad875c
Pull templates from store when not present
alexellis 3831ce7
Switch out ruby for dockerfile in tests
alexellis e3c149a
Make test more accurate by fixing URLs
alexellis 04a590c
Accept stack.yaml as a default file if present
alexellis 5761ec8
Update CI test to use python3-http instead of EOL template
alexellis bd0300f
Name template from python3 to python3-http
alexellis 2a9b309
Fail integration test early through set mechanisms
alexellis 452db1e
Better error handling and Handle potential permissions issue
alexellis 8fc6ee0
Fix bash test
alexellis 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 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ package commands | |
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
|
@@ -20,21 +19,22 @@ const DefaultTemplateRepository = "https://github.com/openfaas/templates.git" | |
const templateDirectory = "./template/" | ||
|
||
// fetchTemplates fetch code templates using git clone. | ||
func fetchTemplates(templateURL string, refName string, overwrite bool) error { | ||
func fetchTemplates(templateURL, refName, templateName string, overwrite bool) error { | ||
if len(templateURL) == 0 { | ||
return fmt.Errorf("pass valid templateURL") | ||
} | ||
|
||
dir, err := os.MkdirTemp("", "openfaas-templates-*") | ||
if err != nil { | ||
log.Fatal(err) | ||
return err | ||
} | ||
|
||
if !pullDebug { | ||
defer os.RemoveAll(dir) // clean up | ||
defer os.RemoveAll(dir) | ||
} | ||
|
||
log.Printf("Attempting to expand templates from %s\n", templateURL) | ||
pullDebugPrint(fmt.Sprintf("Temp files in %s", dir)) | ||
|
||
args := map[string]string{"dir": dir, "repo": templateURL} | ||
cmd := versioncontrol.GitCloneDefault | ||
|
||
|
@@ -47,7 +47,7 @@ func fetchTemplates(templateURL string, refName string, overwrite bool) error { | |
return err | ||
} | ||
|
||
preExistingLanguages, fetchedLanguages, err := moveTemplates(dir, overwrite) | ||
preExistingLanguages, fetchedLanguages, err := moveTemplates(dir, templateName, overwrite) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -56,7 +56,7 @@ func fetchTemplates(templateURL string, refName string, overwrite bool) error { | |
log.Printf("Cannot overwrite the following %d template(s): %v\n", len(preExistingLanguages), preExistingLanguages) | ||
} | ||
|
||
log.Printf("Fetched %d template(s) : %v from %s\n", len(fetchedLanguages), fetchedLanguages, templateURL) | ||
fmt.Printf("Wrote %d template(s) : %v from %s\n", len(fetchedLanguages), fetchedLanguages, templateURL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to be using "log" package here in the CLI output |
||
|
||
return err | ||
} | ||
|
@@ -87,7 +87,7 @@ func templateFolderExists(language string, overwrite bool) bool { | |
return true | ||
} | ||
|
||
func moveTemplates(repoPath string, overwrite bool) ([]string, []string, error) { | ||
func moveTemplates(repoPath, templateName string, overwrite bool) ([]string, []string, error) { | ||
var ( | ||
existingLanguages []string | ||
fetchedLanguages []string | ||
|
@@ -97,7 +97,7 @@ func moveTemplates(repoPath string, overwrite bool) ([]string, []string, error) | |
availableLanguages := make(map[string]bool) | ||
|
||
templateDir := filepath.Join(repoPath, templateDirectory) | ||
templates, err := ioutil.ReadDir(templateDir) | ||
templates, err := os.ReadDir(templateDir) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("can't find templates in: %s", repoPath) | ||
} | ||
|
@@ -108,23 +108,35 @@ func moveTemplates(repoPath string, overwrite bool) ([]string, []string, error) | |
} | ||
language := file.Name() | ||
|
||
canWrite := canWriteLanguage(availableLanguages, language, overwrite) | ||
if canWrite { | ||
fetchedLanguages = append(fetchedLanguages, language) | ||
// Do cp here | ||
languageSrc := filepath.Join(templateDir, language) | ||
languageDest := filepath.Join(templateDirectory, language) | ||
builder.CopyFiles(languageSrc, languageDest) | ||
} else { | ||
existingLanguages = append(existingLanguages, language) | ||
continue | ||
if len(templateName) == 0 { | ||
|
||
canWrite := canWriteLanguage(availableLanguages, language, overwrite) | ||
if canWrite { | ||
fetchedLanguages = append(fetchedLanguages, language) | ||
// Do cp here | ||
languageSrc := filepath.Join(templateDir, language) | ||
languageDest := filepath.Join(templateDirectory, language) | ||
builder.CopyFiles(languageSrc, languageDest) | ||
} else { | ||
existingLanguages = append(existingLanguages, language) | ||
continue | ||
} | ||
} else if language == templateName { | ||
|
||
if canWriteLanguage(availableLanguages, language, overwrite) { | ||
fetchedLanguages = append(fetchedLanguages, language) | ||
// Do cp here | ||
languageSrc := filepath.Join(templateDir, language) | ||
languageDest := filepath.Join(templateDirectory, language) | ||
builder.CopyFiles(languageSrc, languageDest) | ||
} | ||
} | ||
} | ||
|
||
return existingLanguages, fetchedLanguages, nil | ||
} | ||
|
||
func pullTemplate(repository string) error { | ||
func pullTemplate(repository, templateName string) error { | ||
if _, err := os.Stat(repository); err != nil { | ||
if !versioncontrol.IsGitRemote(repository) && !versioncontrol.IsPinnedGitRemote(repository) { | ||
return fmt.Errorf("the repository URL must be a valid git repo uri") | ||
|
@@ -143,8 +155,12 @@ func pullTemplate(repository string) error { | |
} | ||
} | ||
|
||
fmt.Printf("Fetch templates from repository: %s at %s\n", repository, refName) | ||
if err := fetchTemplates(repository, refName, overwrite); err != nil { | ||
refStr := "" | ||
if len(refName) > 0 { | ||
refStr = fmt.Sprintf(" @ %s", refName) | ||
} | ||
fmt.Printf("Fetch templates from repository: %s%s\n", repository, refStr) | ||
if err := fetchTemplates(repository, refName, templateName, overwrite); err != nil { | ||
return fmt.Errorf("error while fetching templates: %s", err) | ||
} | ||
|
||
|
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
Oops, something went wrong.
Oops, something went wrong.
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.
Better to return an error than use Fatal