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

feat: CCIE-180 allow running 'fogg init' with flags instead of realtime user prompts #693

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
81 changes: 80 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,88 @@ 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"
)

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) (string, string, string, string, string, string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after 3 output vars, I usually revert to making a struct because I forget the ordering of the variables, especially when they are all of the same type. It might make it a little cleaner to do something like this:

Suggested change
func userPrompt(cmd *cobra.Command) (string, string, string, string, string, string, error) {
type FoggProject struct {
Project, Region, Bucket, Table, Profile, Owner string
}
func userPrompt(cmd *cobra.Command) (*FoggProject, error) {

and then in error cases you'd do something like

if err != nil {
  return nil, errors.Wrap(err, "a summary of the error")
}

project, region, bucket, table, profile, owner := "", "", "", "", "", ""

project, e := cmd.Flags().GetString("project")
hspitzley-czi marked this conversation as resolved.
Show resolved Hide resolved
if e != nil {
return project, region, bucket, table, profile, owner, e
}
if project == "" {
project = prompt.StringRequired("project name?")
}

region, e = cmd.Flags().GetString("region")
if e != nil {
return project, region, bucket, table, profile, owner, e
}
if region == "" {
region = prompt.StringRequired("aws region?")
}

bucket, e = cmd.Flags().GetString("bucket")
if e != nil {
return project, region, bucket, table, profile, owner, e
}
if bucket == "" {
bucket = prompt.StringRequired("infra bucket name?")
}

table, e = cmd.Flags().GetString("table")
if e != nil {
return project, region, bucket, table, profile, owner, e
}
// 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 table == "" && !isFlagPassed(cmd, "table") {
table = prompt.String("infra dynamo table name?")
}

profile, e = cmd.Flags().GetString("profile")
if e != nil {
return project, region, bucket, table, profile, owner, e
}
if profile == "" {
profile = prompt.StringRequired("auth profile?")
}

owner, e = cmd.Flags().GetString("owner")
if e != nil {
return project, region, bucket, table, profile, owner, e
}
if owner == "" {
owner = prompt.StringRequired("owner?")
}

return project, region, bucket, table, profile, owner, nil
}

var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize a new repo for use with fogg",
Expand All @@ -27,6 +101,11 @@ var initCmd = &cobra.Command{
// check that we are at root of initialized git repo
openGitOrExit(fs)

return fogg_init.Init(fs)
project, region, bucket, table, profile, owner, err := userPrompt(cmd)
if err != nil {
return err
}

return fogg_init.Init(fs, project, region, bucket, table, profile, owner)
},
}
17 changes: 2 additions & 15 deletions init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,14 @@ 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
}

//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, project, region, bucket, table, awsProfile, owner string) error {
config := config.InitConfig(project, region, bucket, table, awsProfile, owner, AWSProviderVersion)
e := config.Write(fs, "fogg.yml")
return e
}