Skip to content

Commit

Permalink
fix: Prevent including table if its an empty string (chanzuckerberg#697)
Browse files Browse the repository at this point in the history
  • Loading branch information
hspitzley-czi authored Aug 2, 2022
1 parent c97e9c0 commit 14cdabf
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 30 deletions.
44 changes: 26 additions & 18 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 9 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
Expand Down
6 changes: 5 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "[email protected]", "0.99.0")
c := InitConfig(getPtr("proj"), getPtr("reg"), getPtr("buck"), getPtr("table"), getPtr("prof"), getPtr("[email protected]"), "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)
Expand Down
2 changes: 1 addition & 1 deletion init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 14cdabf

Please sign in to comment.