From c2267b96c5ea3262ecd1878328dac9c22b7dc261 Mon Sep 17 00:00:00 2001 From: tarunsunny3 Date: Tue, 5 Nov 2024 20:41:00 -0700 Subject: [PATCH 1/2] Add Validation check to see if func is already initialized --- cmd/build.go | 7 +++++++ pkg/functions/client.go | 3 +++ 2 files changed, 10 insertions(+) diff --git a/cmd/build.go b/cmd/build.go index aab47aa27c..1617305ff9 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -303,6 +303,13 @@ func (c buildConfig) Prompt() (buildConfig, error) { // value and will always use the value from the config (flag or env variable). // This is not strictly correct and will be fixed when Global Config: Function // Context is available (PR#1416) + hasFunc, err := fn.IsFunctionInitialized(c.Path) + if err != nil { + return c, err + } + if !hasFunc { + return c, fmt.Errorf("no function has been initialized in the current directory. Please initialize a function by running either:\n- func init --language \n- func create --language ") + } f, err := fn.NewFunction(c.Path) if err != nil { return c, err diff --git a/pkg/functions/client.go b/pkg/functions/client.go index 2542739448..a3b696d5a5 100644 --- a/pkg/functions/client.go +++ b/pkg/functions/client.go @@ -1276,6 +1276,9 @@ func isEffectivelyEmpty(path string) (bool, error) { } return true, nil } +func IsFunctionInitialized(path string) (bool, error) { + return hasInitializedFunction(path) +} // returns true if the given path contains an initialized function. func hasInitializedFunction(path string) (bool, error) { From d9a91595118bacc7eb05fd19945751e83287cb89 Mon Sep 17 00:00:00 2001 From: tarunsunny3 Date: Thu, 14 Nov 2024 21:27:06 -0700 Subject: [PATCH 2/2] Refactor func init check in build.go --- cmd/build.go | 10 +++------- pkg/functions/client.go | 3 --- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/cmd/build.go b/cmd/build.go index 1617305ff9..4917997675 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -303,17 +303,13 @@ func (c buildConfig) Prompt() (buildConfig, error) { // value and will always use the value from the config (flag or env variable). // This is not strictly correct and will be fixed when Global Config: Function // Context is available (PR#1416) - hasFunc, err := fn.IsFunctionInitialized(c.Path) - if err != nil { - return c, err - } - if !hasFunc { - return c, fmt.Errorf("no function has been initialized in the current directory. Please initialize a function by running either:\n- func init --language \n- func create --language ") - } f, err := fn.NewFunction(c.Path) if err != nil { return c, err } + if !f.Initialized() { + return c, fmt.Errorf("no function has been initialized in %q. Please initialize a function by running:\n- func init --language ", c.Path) + } if (f.Registry == "" && c.Registry == "" && c.Image == "") || c.Confirm { fmt.Println("A registry for function images is required. For example, 'docker.io/tigerteam'.") err := survey.AskOne( diff --git a/pkg/functions/client.go b/pkg/functions/client.go index a3b696d5a5..2542739448 100644 --- a/pkg/functions/client.go +++ b/pkg/functions/client.go @@ -1276,9 +1276,6 @@ func isEffectivelyEmpty(path string) (bool, error) { } return true, nil } -func IsFunctionInitialized(path string) (bool, error) { - return hasInitializedFunction(path) -} // returns true if the given path contains an initialized function. func hasInitializedFunction(path string) (bool, error) {