Repository: chanzuckerberg/fogg
Pull request #697 (2022-08-02 18:13:31)

fix: Prevent including table if its an empty string
### Summary
A description of changes or issues addressed by this PR. Provide links to relevant PRs if any.

### Test Plan
Say unittests, or list out steps to verify changes.

### References
(Optional) Additional links to provide more context.



Patch:
diff --git a/cmd/init.go b/cmd/init.go
index 4c58198c..43ad0a96 100644
--- a/cmd/init.go
+++ b/cmd/init.go
@@ -37,55 +37,63 @@ func userPrompt(cmd *cobra.Command) (*FoggProject, error) {
 	foggProject := new(FoggProject)
 	var err error
 
-	foggProject.Project, err = cmd.Flags().GetString("project")
+	project, err := cmd.Flags().GetString("project")
 	if err != nil {
 		return nil, err
 	}
-	if foggProject.Project == "" {
-		foggProject.Project = prompt.StringRequired("project name?")
+	if project == "" {
+		project = prompt.StringRequired("project name?")
 	}
+	foggProject.Project = &project
 
-	foggProject.Region, err = cmd.Flags().GetString("region")
+	region, err := cmd.Flags().GetString("region")
 	if err != nil {
 		return nil, err
 	}
-	if foggProject.Region == "" {
-		foggProject.Region = prompt.StringRequired("aws region?")
+	if region == "" {
+		region = prompt.StringRequired("aws region?")
 	}
+	foggProject.Region = &region
 
-	foggProject.Bucket, err = cmd.Flags().GetString("bucket")
+	bucket, err := cmd.Flags().GetString("bucket")
 	if err != nil {
 		return nil, err
 	}
-	if foggProject.Bucket == "" {
-		foggProject.Bucket = prompt.StringRequired("infra bucket name?")
+	if bucket == "" {
+		bucket = prompt.StringRequired("infra bucket name?")
 	}
+	foggProject.Bucket = &bucket
 
-	foggProject.Table, err = cmd.Flags().GetString("table")
+	table, err := cmd.Flags().GetString("table")
 	if err != nil {
 		return nil, err
 	}
 	// check whether the flag was passed for table because table isn't required
 	// so this allows passing empty string to bypass the user prompt
-	if foggProject.Table == "" && !isFlagPassed(cmd, "table") {
-		foggProject.Table = prompt.String("infra dynamo table name?")
+	if table == "" && !isFlagPassed(cmd, "table") {
+		table = prompt.String("infra dynamo table name?")
+	}
+	if table != "" {
+		foggProject.Table = &table
 	}
 
-	foggProject.Profile, err = cmd.Flags().GetString("profile")
+	profile, err := cmd.Flags().GetString("profile")
 	if err != nil {
 		return nil, err
 	}
-	if foggProject.Profile == "" {
-		foggProject.Profile = prompt.StringRequired("auth profile?")
+	if profile == "" {
+		profile = prompt.StringRequired("auth profile?")
 	}
+	foggProject.Profile = &profile
 
-	foggProject.Owner, err = cmd.Flags().GetString("owner")
+	owner, err := cmd.Flags().GetString("owner")
 	if err != nil {
 		return nil, err
 	}
-	if foggProject.Owner == "" {
-		foggProject.Owner = prompt.StringRequired("owner?")
+	if owner == "" {
+		owner = prompt.StringRequired("owner?")
 	}
+	foggProject.Owner = &owner
 
 	return foggProject, nil
 }
diff --git a/config/config.go b/config/config.go
index 6c3de361..e2bb00ab 100644
--- a/config/config.go
+++ b/config/config.go
@@ -19,22 +19,22 @@ var defaultTerraformVersion = goVersion.Must(goVersion.NewVersion("0.13.5"))
 const DefaultFoggVersion = 2
 
 //InitConfig initializes the config file using user input
-func InitConfig(project, region, bucket, table, awsProfile, owner, awsProviderVersion string) *v2.Config {
+func InitConfig(project, region, bucket, table, awsProfile, owner *string, awsProviderVersion string) *v2.Config {
 	return &v2.Config{
 		Defaults: v2.Defaults{
 			Common: v2.Common{
 				Backend: &v2.Backend{
-					Bucket:      &bucket,
-					Profile:     &awsProfile,
-					Region:      &region,
-					DynamoTable: &table,
+					Bucket:      bucket,
+					Profile:     awsProfile,
+					Region:      region,
+					DynamoTable: table,
 				},
-				Owner:   &owner,
-				Project: &project,
+				Owner:   owner,
+				Project: project,
 				Providers: &v2.Providers{
 					AWS: &v2.AWSProvider{
-						Profile: &awsProfile,
-						Region:  &region,
+						Profile: awsProfile,
+						Region:  region,
 						Version: &awsProviderVersion,
 					},
 				},
diff --git a/init/init.go b/init/init.go
index 9e0bfb4c..2f7e9536 100644
--- a/init/init.go
+++ b/init/init.go
@@ -8,7 +8,7 @@ import (
 const AWSProviderVersion = "2.47.0"
 
 type FoggProject struct {
-	Project, Region, Bucket, Table, Profile, Owner string
+	Project, Region, Bucket, Table, Profile, Owner *string
 }
 
 //Init reads user console input and generates a fogg.yml file


Test patch:
diff --git a/config/config_test.go b/config/config_test.go
index cbccc926..67780ce4 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -8,9 +8,13 @@ import (
 	"github.com/stretchr/testify/require"
 )
 
+func getPtr(val string) *string {
+	return &val
+}
+
 func TestInitConfig(t *testing.T) {
 	r := require.New(t)
-	c := InitConfig("proj", "reg", "buck", "table", "prof", "me@foo.example", "0.99.0")
+	c := InitConfig(getPtr("proj"), getPtr("reg"), getPtr("buck"), getPtr("table"), getPtr("prof"), getPtr("me@foo.example"), "0.99.0")
 	r.Equal("prof", *c.Defaults.Common.Backend.Profile)
 	r.Equal("prof", *c.Defaults.Providers.AWS.Profile)
 	r.Equal("reg", *c.Defaults.Providers.AWS.Region)
diff --git a/init/init_test.go b/init/init_test.go
index 14949890..fa0699af 100644
--- a/init/init_test.go
+++ b/init/init_test.go
@@ -20,7 +20,7 @@ func TestInit(t *testing.T) {
 	fs, _, err := util.TestFs()
 	r.NoError(err)
 
-	conf := config.InitConfig(project, region, bucket, table, profile, owner, AWSProviderVersion)
+	conf := config.InitConfig(&project, &region, &bucket, &table, &profile, &owner, AWSProviderVersion)
 	r.NotNil(conf)
 	r.Equal(config.DefaultFoggVersion, conf.Version)