From 5d662e66d7f235979ef70f0e98641a782a0d5016 Mon Sep 17 00:00:00 2001 From: Edgar Costa Date: Sat, 16 Sep 2023 02:24:40 -0300 Subject: [PATCH] fix: breaking the client initialization --- cmd/local/cmd.go | 49 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/cmd/local/cmd.go b/cmd/local/cmd.go index b2a1806..d495489 100644 --- a/cmd/local/cmd.go +++ b/cmd/local/cmd.go @@ -79,24 +79,18 @@ var Cmd = &cobra.Command{ func Run(cmd *cobra.Command, argv []string) { // client initialization with AFT Credentials - log.Info("initializing AWS Client using AFT Account credentials") - awsClient := aws.NewClient("") - ssmClient := awsClient.GetSSMClient() - - // defining the S3 key for the local execution - var tfS3Key string - - // Validate input account ID - _, err := validate.CheckAWSAccountID(args.targetAccount) + awsClient, ssmClient, err := initializeAWSandSSMClients() if err != nil { - log.Fatalf("invalid AWS Account ID: %v", err) + log.Errorf("error initializing AWS and SSM Clients: %v", err) return } - // Validate input terraform command - _, err = validate.CheckTerraformCommand(args.terraformCommand) - if err != nil { - log.Fatalf("invalid Terraform command: %v", err) + // defining the S3 key for the local execution + var tfS3Key string + + // Validate input + if err := validateInput(); err != nil { + log.Errorf("Validation failed: %v", err) return } @@ -334,3 +328,30 @@ func processJinjaFiles( return nil } + +func validateInput() error { + _, err := validate.CheckAWSAccountID(args.targetAccount) + if err != nil { + return fmt.Errorf("invalid AWS Account ID: %w", err) + } + _, err = validate.CheckTerraformCommand(args.terraformCommand) + if err != nil { + return fmt.Errorf("invalid Terraform Command: %w", err) + } + return nil +} + +func initializeAWSandSSMClients() (*aws.Client, aws.SSMClient, error) { + log.Info("initializing AWS Client using AFT Account credentials") + awsClient := aws.NewClient("") + if awsClient == nil { + return nil, nil, fmt.Errorf("failed to initialize AWS client") + } + + ssmClient := awsClient.GetSSMClient() + if ssmClient == nil { + return nil, nil, fmt.Errorf("failed to initialize SSM client") + } + + return awsClient, ssmClient, nil +}