Skip to content

Commit

Permalink
feat: CCIE-180 allow running 'fogg init' with flags instead of realti…
Browse files Browse the repository at this point in the history
…me user prompts (#693)
  • Loading branch information
hspitzley-czi authored Aug 1, 2022
1 parent b35740f commit 07010d4
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 14 deletions.
84 changes: 83 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
},
}
25 changes: 12 additions & 13 deletions init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 07010d4

Please sign in to comment.