diff --git a/cmd/init.go b/cmd/init.go index 4e7280fb5..4c58198c0 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -5,14 +5,91 @@ import ( "github.com/chanzuckerberg/fogg/errs" fogg_init "github.com/chanzuckerberg/fogg/init" + prompt "github.com/segmentio/go-prompt" "github.com/spf13/afero" "github.com/spf13/cobra" + "github.com/spf13/pflag" ) +type FoggProject = fogg_init.FoggProject + func init() { + initCmd.Flags().String("project", "", "Use this to pass the project name via CLI.") + initCmd.Flags().String("region", "", "Use this to pass the aws region via CLI.") + initCmd.Flags().String("bucket", "", "Use this to pass the infra bucket name via CLI.") + initCmd.Flags().String("table", "", "Use this to pass the infra dynamo table name via CLI.") + initCmd.Flags().String("profile", "", "Use this to pass the aws auth profile via CLI.") + initCmd.Flags().String("owner", "", "Use this to pass the owner name via CLI.") rootCmd.AddCommand(initCmd) } +func isFlagPassed(cmd *cobra.Command, name string) bool { + found := false + cmd.Flags().Visit(func(f *pflag.Flag) { + if f.Name == name { + found = true + } + }) + return found +} + +func userPrompt(cmd *cobra.Command) (*FoggProject, error) { + foggProject := new(FoggProject) + var err error + + foggProject.Project, err = cmd.Flags().GetString("project") + if err != nil { + return nil, err + } + if foggProject.Project == "" { + foggProject.Project = prompt.StringRequired("project name?") + } + + foggProject.Region, err = cmd.Flags().GetString("region") + if err != nil { + return nil, err + } + if foggProject.Region == "" { + foggProject.Region = prompt.StringRequired("aws region?") + } + + foggProject.Bucket, err = cmd.Flags().GetString("bucket") + if err != nil { + return nil, err + } + if foggProject.Bucket == "" { + foggProject.Bucket = prompt.StringRequired("infra bucket name?") + } + + foggProject.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?") + } + + foggProject.Profile, err = cmd.Flags().GetString("profile") + if err != nil { + return nil, err + } + if foggProject.Profile == "" { + foggProject.Profile = prompt.StringRequired("auth profile?") + } + + foggProject.Owner, err = cmd.Flags().GetString("owner") + if err != nil { + return nil, err + } + if foggProject.Owner == "" { + foggProject.Owner = prompt.StringRequired("owner?") + } + + return foggProject, nil +} + var initCmd = &cobra.Command{ Use: "init", Short: "Initialize a new repo for use with fogg", @@ -27,6 +104,11 @@ var initCmd = &cobra.Command{ // check that we are at root of initialized git repo openGitOrExit(fs) - return fogg_init.Init(fs) + foggProject, err := userPrompt(cmd) + if err != nil { + return err + } + + return fogg_init.Init(fs, foggProject) }, } diff --git a/init/init.go b/init/init.go index 0e82a9425..9e0bfb4c5 100644 --- a/init/init.go +++ b/init/init.go @@ -2,27 +2,26 @@ package init import ( "github.com/chanzuckerberg/fogg/config" - prompt "github.com/segmentio/go-prompt" "github.com/spf13/afero" ) const AWSProviderVersion = "2.47.0" -func userPrompt() (string, string, string, string, string, string) { - project := prompt.StringRequired("project name?") - region := prompt.StringRequired("aws region?") - bucket := prompt.StringRequired("infra bucket name?") - table := prompt.String("infra dynamo table name?") - profile := prompt.StringRequired("auth profile?") - owner := prompt.StringRequired("owner?") - - return project, region, bucket, table, profile, owner +type FoggProject struct { + Project, Region, Bucket, Table, Profile, Owner string } //Init reads user console input and generates a fogg.yml file -func Init(fs afero.Fs) error { - project, region, bucket, table, profile, owner := userPrompt() - config := config.InitConfig(project, region, bucket, table, profile, owner, AWSProviderVersion) +func Init(fs afero.Fs, foggProject *FoggProject) error { + config := config.InitConfig( + foggProject.Project, + foggProject.Region, + foggProject.Bucket, + foggProject.Table, + foggProject.Profile, + foggProject.Owner, + AWSProviderVersion, + ) e := config.Write(fs, "fogg.yml") return e }