diff --git a/main.go b/main.go index e3d1ecfad..272219d8c 100644 --- a/main.go +++ b/main.go @@ -21,9 +21,14 @@ func main() { log.Printf("Running OpenAPI Terraform Provider v%s-%s; Released on: %s", version, commit, date) - providerName, err := getProviderName() + ex, err := os.Executable() if err != nil { - log.Fatalf("[ERROR] There was an error when getting the provider's name: %s", err) + log.Fatalf("[ERROR] There was an error when getting the provider binary name: %s", err) + } + + providerName, err := getProviderName(ex) + if err != nil { + log.Fatalf("[ERROR] There was an error when getting the provider's name fomr the binary '%s': %s", ex, err) } plugin.Serve( &plugin.ServeOpts{ @@ -37,18 +42,15 @@ func main() { }) } -func getProviderName() (string, error) { - ex, err := os.Executable() +func getProviderName(binaryName string) (string, error) { + r, err := regexp.Compile("\\bterraform-provider-([a-zA-Z0-9]+)(?:_v\\d\\.\\d\\.\\d)?\\b") if err != nil { return "", err } - r, err := regexp.Compile("(\\w+)[^-]*$") - if err != nil { - return "", err - } - match := r.FindStringSubmatch(ex) + + match := r.FindStringSubmatch(binaryName) if len(match) != 2 { - return "", fmt.Errorf("provider name (%s) does not match terraform naming convention 'terraform-provider-{name}', please rename the provider binary", ex) + return "", fmt.Errorf("provider binary name (%s) does not match terraform naming convention 'terraform-provider-{name}', please rename the provider binary", binaryName) } - return match[0], nil + return match[1], nil } diff --git a/main_test.go b/main_test.go new file mode 100644 index 000000000..c056b1e9b --- /dev/null +++ b/main_test.go @@ -0,0 +1,70 @@ +package main + +import ( + "testing" + . "github.com/smartystreets/goconvey/convey" +) + +func TestGetProviderName(t *testing.T) { + Convey("Given a valid pluginName with no version", t, func() { + binaryName := "terraform-provider-goa" + Convey("When getProviderName method is called", func() { + providerName, err := getProviderName(binaryName) + Convey("Then the error returned should be nil", func() { + So(err, ShouldBeNil) + }) + Convey("And the error message returned should be", func() { + So(providerName, ShouldEqual, "goa") + }) + }) + }) + Convey("Given a valid pluginName with correct version", t, func() { + binaryName := "terraform-provider-goa_v1.0.0" + Convey("When getProviderName method is called", func() { + providerName, err := getProviderName(binaryName) + Convey("Then the error returned should be nil", func() { + So(err, ShouldBeNil) + }) + Convey("And the error message returned should be", func() { + So(providerName, ShouldEqual, "goa") + }) + }) + }) + Convey("Given a valid pluginName with numbers in the name", t, func() { + binaryName := "terraform-provider-goa1234" + Convey("When getProviderName method is called", func() { + providerName, err := getProviderName(binaryName) + Convey("Then the error returned should be nil", func() { + So(err, ShouldBeNil) + }) + Convey("And the error message returned should be", func() { + So(providerName, ShouldEqual, "goa1234") + }) + }) + }) + Convey("Given a valid pluginName with incorrect version format", t, func() { + binaryName := "terraform-provider-goa_v1" + Convey("When getProviderName method is called", func() { + _, err := getProviderName(binaryName) + Convey("Then the error returned should be nil", func() { + So(err, ShouldNotBeNil) + }) + Convey("And the error message returned should be", func() { + So(err.Error(), ShouldEqual, "provider binary name (terraform-provider-goa_v1) does not match terraform naming convention 'terraform-provider-{name}', please rename the provider binary") + }) + }) + }) + Convey("Given a NON valid pluginName", t, func() { + binaryName := "terraform-providerWrongName-goa" + Convey("When getProviderName method is called", func() { + _, err := getProviderName(binaryName) + Convey("Then the error returned should be nil", func() { + So(err, ShouldNotBeNil) + }) + Convey("And the error message returned should be", func() { + So(err.Error(), ShouldEqual, "provider binary name (terraform-providerWrongName-goa) does not match terraform naming convention 'terraform-provider-{name}', please rename the provider binary") + }) + }) + }) + +} \ No newline at end of file