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

Pull templates from store when not present #1004

Merged
merged 9 commits into from
Jan 23, 2025
Merged

Conversation

alexellis
Copy link
Member

Description

Pull templates from store when not present

Implements a user-experience changes:

faas-cli new - when the templates folder is not present the language will be looked up in the template store using the default URL or an overriden one.

faas-cli publish - when no template folder is available then a pull will be carried out if a configuration section is present with a template given within in

Filtering - whilst there is no way to avoid pulling all the templates wihtin a repository, only the requested/needed ones are expanded by commands like faas-cli new.

Motivation and Context

Improve user experience as discussed on community call Jan 22 2025

How Has This Been Tested?

Tested mainly with existing-unit tests, faas-cli new / publish with and without a templates folder.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Implements a user-experience changes:

faas-cli new - when the templates folder is not present the
language will be looked up in the template store using the
default URL or an overriden one.

faas-cli publish - when no template folder is available then
a pull will be carried out if a configuration section is
present with a template given within in

Filtering - whilst there is no way to avoid pulling all the
templates wihtin a repository, only the requested/needed ones
are expanded by commands like faas-cli new.

Tested mainly with existing-unit tests, faas-cli new / publish
with and without a templates folder.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
log.Println("No templates found in current directory.")
func pullTemplates(templateURL, templateName string) error {

if _, err := os.Stat("./template"); err != nil && os.IsNotExist(err) {
Copy link
Member Author

Choose a reason for hiding this comment

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

More idiomatic way to check for the folder.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree.

Would os.Stat return any errors other than os.IsNotExist?

Copy link
Member Author

Choose a reason for hiding this comment

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

In a rare case there may be a permissions issue

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed in subsequent commit

if len(templateURL) == 0 {
return fmt.Errorf("pass valid templateURL")
}

dir, err := os.MkdirTemp("", "openfaas-templates-*")
if err != nil {
log.Fatal(err)
return err
Copy link
Member Author

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

@@ -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)
Copy link
Member Author

Choose a reason for hiding this comment

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

No need to be using "log" package here in the CLI output

"testing"

"github.com/openfaas/faas-cli/builder"
"github.com/openfaas/faas-cli/stack"
)

func Test_findTemplate(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

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

The findTemplate function was not used in any code path so was deleted

The tests relied on ruby being a long term template in the
templates repository, however we are moving to a model
with a repository per template, and this template has gone
to EOL.

The Dockerfile template makes sense as a replacement and is
still supported.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
@alexellis alexellis requested a review from Copilot January 23, 2025 10:58

Choose a reason for hiding this comment

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

Copilot reviewed 5 out of 10 changed files in this pull request and generated no comments.

Files not reviewed (5)
  • commands/build.go: Evaluated as low risk
  • commands/fetch_templates_test.go: Evaluated as low risk
  • commands/new_function.go: Evaluated as low risk
  • commands/template_pull_stack.go: Evaluated as low risk
  • README.md: Evaluated as low risk
Comments suppressed due to low confidence (3)

commands/new_function_test.go:30

  • Verify the correctness of the updated regex pattern in the LangNotExistsOutput string. Ensure that it matches the expected output format.
LangNotExistsOutput  = `(template: "([0-9A-Za-z-])*" was not found in the templates folder or in the store)`

commands/new_function_test.go:275

  • Ensure that the changes in the test cases are consistent with the new functionality. Verify that using dockerfile instead of ruby is intentional and correct.
const functionLang = "dockerfile"

commands/template_pull.go:47

  • The templateName variable is initialized but not used in the runTemplatePull function. This should be reviewed to ensure it's necessary.
templateName := "" // templateName may not be known at this point
Requested by @welteki, if stack.yml is present then it is
taken to be the default with a --yaml/-f argument being needed.

This change also looks for stack.yaml as a valid alternative
name.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
err could be nil, so it is checked before checking for the
type of error. This may not be needed, but is a better
default to establish since code is often copied by new
contributors when adding features.

Addresses comment by @rgee0 for handling error from stat.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
@alexellis alexellis merged commit 13a967a into master Jan 23, 2025
2 checks passed
@alexellis alexellis deleted the pull-from-store branch January 23, 2025 17:19
@alexellis
Copy link
Member Author

I'm going to merge this so it can get into the hands of a few people, we can make additional changes as we go along.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants