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

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"runtime"
     7  
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/iam"
    10  	"github.com/aws/aws-sdk-go/service/sts"
    11  	"github.com/hazelops/ize/internal/config"
    12  
    13  	"github.com/hazelops/ize/internal/version"
    14  	"github.com/pterm/pterm"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  func NewDebugCmd(project *config.Project) *cobra.Command {
    19  	cmd := &cobra.Command{
    20  		Use:   "status",
    21  		Short: "Show debug information",
    22  		RunE: func(cmd *cobra.Command, args []string) error {
    23  			cmd.SilenceUsage = true
    24  
    25  			cwd, err := os.Getwd()
    26  			if err != nil {
    27  				return fmt.Errorf("can't load options for a command: %w", err)
    28  			}
    29  
    30  			dt := pterm.DefaultTable
    31  
    32  			pterm.DefaultSection.Println("IZE Info")
    33  
    34  			_ = dt.WithData(pterm.TableData{
    35  				{"ENV", project.Env},
    36  				{"NAMESPACE", project.Namespace},
    37  				{"TAG", project.Tag},
    38  				{"INFRA DIR", project.InfraDir},
    39  				{"PWD", cwd},
    40  				{"IZE VERSION", version.FullVersionNumber()},
    41  				{"GIT REVISION", version.GitCommit},
    42  				{"ENV DIR", project.EnvDir},
    43  				{"PREFER_RUNTIME", project.PreferRuntime},
    44  			}).WithLeftAlignment().Render()
    45  
    46  			v := project.TerraformVersion
    47  			if project.Terraform != nil {
    48  				if i, ok := project.Terraform["infra"]; ok {
    49  					if len(i.Version) != 0 {
    50  						v = i.Version
    51  					}
    52  				}
    53  			}
    54  
    55  			pterm.DefaultSection.Println("Terraform Info")
    56  			_ = dt.WithData(pterm.TableData{
    57  				{"TERRAFORM_VERSION", v},
    58  			}).WithLeftAlignment().Render()
    59  
    60  			pterm.DefaultSection.Println("System Info")
    61  
    62  			_ = dt.WithData(pterm.TableData{
    63  				{"OS", runtime.GOOS},
    64  				{"ARCH", runtime.GOARCH},
    65  			}).WithLeftAlignment().Render()
    66  
    67  			pterm.DefaultSection.Println("AWS Environment Info")
    68  
    69  			if len(project.AwsProfile) > 0 {
    70  				resp, err := project.AWSClient.STSClient.GetCallerIdentity(
    71  					&sts.GetCallerIdentityInput{},
    72  				)
    73  				if err != nil {
    74  					return err
    75  				}
    76  
    77  				guo, err := project.AWSClient.IAMClient.GetUser(&iam.GetUserInput{})
    78  				if aerr, ok := err.(awserr.Error); ok {
    79  					switch aerr.Code() {
    80  					case "NoSuchEntity":
    81  						return fmt.Errorf("error obtaining AWS user with aws_profile=%s: username %s is not found in account %s", project.AwsProfile, *guo.User.UserName, *resp.Account)
    82  					default:
    83  						return err
    84  					}
    85  				}
    86  
    87  				tags, err := project.AWSClient.IAMClient.ListUserTags(&iam.ListUserTagsInput{
    88  					UserName: guo.User.UserName,
    89  				})
    90  				if aerr, ok := err.(awserr.Error); ok {
    91  					switch aerr.Code() {
    92  					case "NoSuchEntity":
    93  						return fmt.Errorf("error obtaining AWS user with aws_profile=%s: username %s is not found in account %s", project.AwsProfile, *guo.User.UserName, *resp.Account)
    94  					default:
    95  						return err
    96  					}
    97  				}
    98  
    99  				devEnvName := ""
   100  
   101  				for _, k := range tags.Tags {
   102  					if *k.Key == "devEnvironmentName" {
   103  						devEnvName = *k.Value
   104  					}
   105  				}
   106  
   107  				_ = dt.WithData(pterm.TableData{
   108  					{"AWS PROFILE", project.AwsProfile},
   109  					{"AWS USER", *guo.User.UserName},
   110  					{"AWS ACCOUNT", *resp.Account},
   111  				}).WithLeftAlignment().Render()
   112  
   113  				if len(devEnvName) > 0 {
   114  					_ = dt.WithData(pterm.TableData{
   115  						{"AWS_DEV_ENV_NAME", devEnvName},
   116  					}).WithLeftAlignment().Render()
   117  				}
   118  			} else {
   119  				pterm.Println("No AWS profile credentials detected. Parameters used:")
   120  				_ = dt.WithData(pterm.TableData{
   121  					{"AWS PROFILE", project.AwsProfile},
   122  				}).WithLeftAlignment().Render()
   123  			}
   124  
   125  			return nil
   126  		},
   127  	}
   128  
   129  	return cmd
   130  }