Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent including table if its an empty string #697

Merged
merged 2 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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