github.com/hazelops/ize@v1.1.12-0.20230915191306-97d7c0e48f11/internal/commands/configure.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"reflect"
     7  	"strings"
     8  
     9  	"github.com/AlecAivazis/survey/v2"
    10  	"github.com/spf13/cobra"
    11  	"github.com/spf13/viper"
    12  )
    13  
    14  type ConfigureOptions struct {
    15  }
    16  
    17  func NewConfigFlags() *ConfigureOptions {
    18  	return &ConfigureOptions{}
    19  }
    20  
    21  func NewCmdConfig() *cobra.Command {
    22  	o := NewConfigFlags()
    23  
    24  	cmd := &cobra.Command{
    25  		Use:   "configure",
    26  		Short: "Generate global configuration file",
    27  		RunE: func(cmd *cobra.Command, args []string) error {
    28  			err := o.Run()
    29  			if err != nil {
    30  				return err
    31  			}
    32  
    33  			return nil
    34  		},
    35  	}
    36  
    37  	return cmd
    38  }
    39  
    40  func (o *ConfigureOptions) Run() error {
    41  	home, err := os.UserHomeDir()
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	gc := map[string]map[string]map[string]string{}
    47  
    48  	viper.SetConfigFile(fmt.Sprintf("%s/.ize/config.toml", home))
    49  	err = viper.ReadInConfig()
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	err = viper.Unmarshal(&gc)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	env, exist := os.LookupEnv("IZE_ENV")
    60  	if !exist {
    61  		env = os.Getenv("ENV")
    62  	}
    63  	region, exist := os.LookupEnv("IZE_AWS_REGION")
    64  	if !exist {
    65  		region = os.Getenv("AWS_REGION")
    66  	}
    67  	profile, exist := os.LookupEnv("IZE_AWS_PROFILE")
    68  	if !exist {
    69  		profile = os.Getenv("AWS_PROFILE")
    70  	}
    71  	namespace, exist := os.LookupEnv("IZE_NAMESPACE")
    72  	if !exist {
    73  		namespace = os.Getenv("NAMESPACE")
    74  	}
    75  
    76  	err = survey.AskOne(&survey.Input{
    77  		Message: " namespace:",
    78  		Default: namespace,
    79  	}, &namespace, survey.WithIcons(func(is *survey.IconSet) {
    80  		is.Question.Text = " ??"
    81  		is.Question.Format = "black:green"
    82  		is.Error.Format = "black:red"
    83  	}))
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	err = survey.AskOne(&survey.Input{
    89  		Message: " env:",
    90  		Default: env,
    91  	}, &env, survey.WithIcons(func(is *survey.IconSet) {
    92  		is.Question.Text = " ??"
    93  		is.Question.Format = "black:green"
    94  		is.Error.Format = "black:red"
    95  	}))
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	var qs = []*survey.Question{
   101  		{
   102  			Prompt: &survey.Input{
   103  				Message: " aws region:",
   104  				Default: region,
   105  			},
   106  			Validate: survey.Required,
   107  			Name:     "aws_region",
   108  		},
   109  		{
   110  			Prompt: &survey.Input{
   111  				Message: " aws profile:",
   112  				Default: profile,
   113  			},
   114  			Validate: survey.Required,
   115  			Name:     "aws_profile",
   116  		},
   117  		{
   118  			Prompt: &survey.Input{
   119  				Message: " terraform version:",
   120  			},
   121  			Validate: survey.Required,
   122  			Name:     "terraform_version",
   123  		},
   124  	}
   125  
   126  	opts := Config{}
   127  
   128  	err = survey.Ask(qs, &opts, survey.WithIcons(func(is *survey.IconSet) {
   129  		is.Question.Text = " ??"
   130  		is.Question.Format = "black:green"
   131  		is.Error.Format = "black:red"
   132  	}))
   133  	if err != nil {
   134  		return err
   135  	}
   136  
   137  	v := reflect.ValueOf(opts)
   138  	typeOfOpts := v.Type()
   139  
   140  	viper.Reset()
   141  
   142  	if gc[namespace] == nil {
   143  		gc[namespace] = make(map[string]map[string]string)
   144  	}
   145  
   146  	if gc[namespace][env] == nil {
   147  		gc[namespace][env] = make(map[string]string)
   148  	}
   149  
   150  	for i := 0; i < v.NumField(); i++ {
   151  		gc[namespace][env][strings.ToLower(typeOfOpts.Field(i).Name)] = v.Field(i).String()
   152  	}
   153  
   154  	raw := make(map[string]interface{}, len(gc))
   155  	for k, v := range gc {
   156  		raw[k] = v
   157  	}
   158  
   159  	err = viper.MergeConfigMap(raw)
   160  	if err != nil {
   161  		return err
   162  	}
   163  
   164  	err = os.MkdirAll(fmt.Sprintf("%s/.ize", home), 0755)
   165  	if err != nil {
   166  		return err
   167  	}
   168  
   169  	err = viper.WriteConfigAs(fmt.Sprintf("%s/.ize/config.toml", home))
   170  	if err != nil {
   171  		return fmt.Errorf("can't write config: %w", err)
   172  	}
   173  
   174  	return nil
   175  }
   176  
   177  type Namespace struct {
   178  	Env map[string]Config
   179  }
   180  
   181  type Config struct {
   182  	AwsProfile       string
   183  	AwsRegion        string
   184  	TerraformVersion string
   185  }