diff --git a/commands/build.go b/commands/build.go index 4a182d2c0..96cd75675 100644 --- a/commands/build.go +++ b/commands/build.go @@ -21,18 +21,19 @@ import ( // Flags that are to be added to commands. var ( - nocache bool - squash bool - parallel int - shrinkwrap bool - buildArgs []string - buildArgMap map[string]string - buildOptions []string - tagFormat schema.BuildFormat - buildLabels []string - buildLabelMap map[string]string - envsubst bool - quietBuild bool + nocache bool + squash bool + parallel int + shrinkwrap bool + buildArgs []string + buildArgMap map[string]string + buildOptions []string + tagFormat schema.BuildFormat + buildLabels []string + buildLabelMap map[string]string + envsubst bool + quietBuild bool + disableStackPull bool ) func init() { @@ -56,6 +57,8 @@ func init() { buildCmd.Flags().BoolVar(&quietBuild, "quiet", false, "Perform a quiet build, without showing output from Docker") + buildCmd.Flags().BoolVar(&disableStackPull, "disable-stack-pull", false, "Disables the template configuration in the stack.yml") + // Set bash-completion. _ = buildCmd.Flags().SetAnnotation("handler", cobra.BashCompSubdirsInDir, []string{}) @@ -181,6 +184,13 @@ func runBuild(cmd *cobra.Command, args []string) error { return nil } + if len(services.StackConfiguration.TemplateConfigs) != 0 && !disableStackPull { + err := pullStackTemplates(services.StackConfiguration.TemplateConfigs, cmd) + if err != nil { + return fmt.Errorf("could not pull templates from function yaml file: %s", err.Error()) + } + } + errors := build(&services, parallel, shrinkwrap, quietBuild) if len(errors) > 0 { errorSummary := "Errors received during build:\n" diff --git a/commands/template_pull_stack.go b/commands/template_pull_stack.go index 8463c5d7a..4fbea7882 100644 --- a/commands/template_pull_stack.go +++ b/commands/template_pull_stack.go @@ -18,7 +18,6 @@ var ( func init() { templatePullStackCmd.Flags().BoolVar(&overwrite, "overwrite", false, "Overwrite existing templates?") templatePullStackCmd.Flags().BoolVar(&pullDebug, "debug", false, "Enable debug output") - templatePullStackCmd.PersistentFlags().StringVarP(&customRepoName, "repo", "r", "", "The custom name of the template repo") templatePullCmd.AddCommand(templatePullStackCmd) } @@ -41,10 +40,7 @@ func runTemplatePullStack(cmd *cobra.Command, args []string) error { if err != nil { return err } - if len(customRepoName) > 0 { - return pullSpecificTemplate(templatesConfig, customRepoName, cmd) - } - return pullAllTemplates(templatesConfig, cmd) + return pullStackTemplates(templatesConfig, cmd) } func loadTemplateConfig() ([]stack.TemplateSource, error) { @@ -72,7 +68,7 @@ func readStackConfig() (stack.Configuration, error) { return configField, nil } -func pullAllTemplates(templateInfo []stack.TemplateSource, cmd *cobra.Command) error { +func pullStackTemplates(templateInfo []stack.TemplateSource, cmd *cobra.Command) error { for _, val := range templateInfo { fmt.Printf("Pulling template: %s from configuration file: %s\n", val.Name, yamlFile) if len(val.Source) == 0 { @@ -98,14 +94,3 @@ func findTemplate(templateInfo []stack.TemplateSource, customName string) (speci } return nil } - -func pullSpecificTemplate(templateInfo []stack.TemplateSource, customName string, cmd *cobra.Command) error { - desiredTemplate := findTemplate(templateInfo, customName) - if desiredTemplate == nil { - return fmt.Errorf("Unable to find template repo with name: `%s`", customName) - } - if len(desiredTemplate.Source) == 0 { - return runTemplateStorePull(cmd, []string{desiredTemplate.Name}) - } - return pullTemplate(desiredTemplate.Source) -} diff --git a/commands/template_pull_stack_test.go b/commands/template_pull_stack_test.go index f1eb8fdef..ce30c0c78 100644 --- a/commands/template_pull_stack_test.go +++ b/commands/template_pull_stack_test.go @@ -98,68 +98,7 @@ func Test_pullAllTemplates(t *testing.T) { } for _, test := range tests { t.Run(test.title, func(t *testing.T) { - actualError := pullAllTemplates(test.existingTemplates, templatePullStackCmd) - if actualError != nil && test.expectedError == false { - t.Errorf("Unexpected error: %s", actualError.Error()) - } - }) - } -} - -func Test_pullSpecificTemplate(t *testing.T) { - tests := []struct { - title string - desiredTemplate string - existingTemplates []stack.TemplateSource - expectedError bool - }{ - { - title: "Pull custom named template", - desiredTemplate: "my_powershell", - existingTemplates: []stack.TemplateSource{ - {Name: "my_powershell", Source: "https://github.com/openfaas-incubator/powershell-http-template"}, - {Name: "my_rust", Source: "https://github.com/openfaas-incubator/openfaas-rust-template"}, - }, - expectedError: false, - }, - { - title: "Pull missing template", - desiredTemplate: "my_perl", - existingTemplates: []stack.TemplateSource{ - {Name: "my_powershell", Source: "https://github.com/openfaas-incubator/powershell-http-template"}, - {Name: "my_rust", Source: "https://github.com/openfaas-incubator/openfaas-rust-template"}, - }, - expectedError: true, - }, - { - title: "Pull custom template from store", - desiredTemplate: "perl-alpine", - existingTemplates: []stack.TemplateSource{ - {Name: "perl-alpine"}, - {Name: "my_rust", Source: "https://github.com/openfaas-incubator/openfaas-rust-template"}, - }, - expectedError: false, - }, - { - title: "Pull specific template with invalid URL", - desiredTemplate: "my_powershell", - existingTemplates: []stack.TemplateSource{ - {Name: "my_powershell", Source: "invalidURL"}, - }, - expectedError: true, - }, - { - title: "Pull template missing from store", - desiredTemplate: "my_powershell", - existingTemplates: []stack.TemplateSource{ - {Name: "my_powershell"}, - }, - expectedError: true, - }, - } - for _, test := range tests { - t.Run(test.title, func(t *testing.T) { - actualError := pullSpecificTemplate(test.existingTemplates, test.desiredTemplate, templatePullStackCmd) + actualError := pullStackTemplates(test.existingTemplates, templatePullStackCmd) if actualError != nil && test.expectedError == false { t.Errorf("Unexpected error: %s", actualError.Error()) } diff --git a/stack/schema.go b/stack/schema.go index 9b89f913b..953681135 100644 --- a/stack/schema.go +++ b/stack/schema.go @@ -87,9 +87,10 @@ type EnvironmentFile struct { // Services root level YAML file to define FaaS function-set type Services struct { - Version string `yaml:"version,omitempty"` - Functions map[string]Function `yaml:"functions,omitempty"` - Provider Provider `yaml:"provider,omitempty"` + Version string `yaml:"version,omitempty"` + Functions map[string]Function `yaml:"functions,omitempty"` + Provider Provider `yaml:"provider,omitempty"` + StackConfiguration StackConfiguration `yaml:"configuration,omitempty"` } // LanguageTemplate read from template.yml within root of a language template folder