Пример #1
0
// Prepare handles the global CLI flags and shared functionality without
// the assumption that a Project has already been initialized.
//
// Precedence is currently:
//
//  - flags such as --profile
//  - env vars such as AWS_PROFILE
//  - files such as ~/.aws/config
//
func Prepare(c *cobra.Command, args []string) error {
	if l, err := log.ParseLevel(logLevel); err == nil {
		log.SetLevel(l)
	}

	// config defaults
	Config = aws.NewConfig()

	// profile from flag, config, env, "default"
	if profile == "" {
		profile, _ = utils.ProfileFromConfig(environment)
		if profile == "" {
			profile = os.Getenv("AWS_PROFILE")
			if profile == "" {
				profile = "default"
			}
		}
	}

	// the default SharedCredentialsProvider checks the env
	os.Setenv("AWS_PROFILE", profile)

	// region from flag, env, file
	if region == "" {
		region = os.Getenv("AWS_REGION")
		if region == "" {
			region, _ = utils.GetRegion(profile)
		}
	}

	if region != "" {
		Config = Config.WithRegion(region)
	}

	Session = session.New(Config)

	Project = &project.Project{
		Environment:      environment,
		InfraEnvironment: environment,
		Log:              log.Log,
		Path:             ".",
	}

	if dryRun {
		log.SetLevel(log.WarnLevel)
		Project.Service = dryrun.New(Session)
		Project.Concurrency = 1
	} else {
		Project.Service = lambda.New(Session)
	}

	if chdir != "" {
		if err := os.Chdir(chdir); err != nil {
			return err
		}
	}

	return nil
}
Пример #2
0
// PreRun sets up global tasks used for most commands, some use PreRunNoop
// to remove this default behaviour.
func preRun(c *cobra.Command, args []string) error {
	if l, err := log.ParseLevel(logLevel); err == nil {
		log.SetLevel(l)
	}

	// credential defaults
	config := aws.NewConfig()

	// explicit profile
	if profile != "" {
		config = config.WithCredentials(credentials.NewSharedCredentials("", profile))
	}

	// support region from ~/.aws/config for AWS_PROFILE
	if profile == "" {
		profile = os.Getenv("AWS_PROFILE")
	}

	// region from ~/.aws/config
	if region, _ := utils.GetRegion(profile); region != "" {
		config = config.WithRegion(region)
	}

	Session = session.New(config)

	Project = &project.Project{
		Log:  log.Log,
		Path: ".",
	}

	if dryRun {
		log.SetLevel(log.WarnLevel)
		Project.Service = dryrun.New(Session)
		Project.Concurrency = 1
	} else {
		Project.Service = lambda.New(Session)
	}

	if chdir != "" {
		if err := os.Chdir(chdir); err != nil {
			return err
		}
	}

	return Project.Open()
}