From d4f3b8bcc6885252b949ab563826829e53bdba00 Mon Sep 17 00:00:00 2001 From: Aidan Steele Date: Fri, 28 Jun 2019 21:11:49 +1000 Subject: [PATCH] gofmt and nicer errors --- cmd/connect.go | 32 ++++++++++++++++++++++++++++---- cmd/install.go | 4 ++-- cmd/root.go | 8 ++++---- cmd/setup.go | 2 +- cmd/version.go | 1 - pkg/ec2connect/authorize.go | 4 ++-- 6 files changed, 37 insertions(+), 14 deletions(-) diff --git a/cmd/connect.go b/cmd/connect.go index aa572d2..7cbe6f3 100644 --- a/cmd/connect.go +++ b/cmd/connect.go @@ -2,7 +2,10 @@ package cmd import ( "context" + "fmt" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/credentials/stscreds" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" @@ -15,6 +18,7 @@ import ( "io/ioutil" "net" "os" + "strings" ) func init() { @@ -29,7 +33,27 @@ func init() { info, err := authorize(instanceId, region, user, sshKeyPath) if err != nil { - panic(err) + if awsErr, ok := errors.Cause(err).(awserr.Error); ok { + if awsErr.Code() == credentials.ErrNoValidProvidersFoundInChain.Code() { + fmt.Fprintln(os.Stderr, ` +No AWS credentials found. + +* You can specify one of the profiles from ~/.aws/config by setting the + AWS_PROFILE environment variable. + +* You can set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and optionally + AWS_SESSION_TOKEN.`) + } else if strings.HasPrefix(awsErr.Code(), "InvalidInstanceID.") { + fmt.Fprintf(os.Stderr, ` +No instance found with ID %s. Try specifying an explicit region using the +AWS_REGION environment variable. + +`, instanceId) + } + return + } else { + panic(err) + } } err = connect(info.Address + ":22") @@ -38,7 +62,7 @@ func init() { } }, } - + cmd.PersistentFlags().String("instance-id", "", "") cmd.PersistentFlags().String("region", "", "") cmd.PersistentFlags().String("user", "ec2-user", "") @@ -59,9 +83,9 @@ func authorize(instanceId, region, user, sshKeyPath string) (*ec2connect.Connect } sess, err := session.NewSessionWithOptions(session.Options{ - SharedConfigState: session.SharedConfigEnable, + SharedConfigState: session.SharedConfigEnable, AssumeRoleTokenProvider: stscreds.StdinTokenProvider, - Config: *aws.NewConfig().WithRegion(region),//.WithLogLevel(aws.LogDebugWithHTTPBody), + Config: *aws.NewConfig().WithRegion(region), //.WithLogLevel(aws.LogDebugWithHTTPBody), }) if err != nil { return nil, err diff --git a/cmd/install.go b/cmd/install.go index 4239779..e752678 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -8,7 +8,7 @@ func init() { cmd := &cobra.Command{ Use: "install", Short: "Remotely install EC2 Instance Connect on EC2 instance", - Long: ``, + Long: ``, Run: func(cmd *cobra.Command, args []string) { }, } @@ -18,4 +18,4 @@ func init() { func install() error { return nil -} \ No newline at end of file +} diff --git a/cmd/root.go b/cmd/root.go index bd53fd9..9980700 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -25,7 +25,7 @@ import ( var RootCmd = &cobra.Command{ Use: "ec2connect", Short: "", - Long: ``, + Long: ``, // Uncomment the following line if your bare application // has an action associated with it: // Run: func(cmd *cobra.Command, args []string) { }, @@ -47,9 +47,9 @@ func init() { // initConfig reads in config file and ENV variables if set. func initConfig() { - viper.AddConfigPath(".") // adding home directory as first search path - viper.AddConfigPath("$HOME") // adding home directory as first search path - viper.AutomaticEnv() // read in environment variables that match + viper.AddConfigPath(".") // adding home directory as first search path + viper.AddConfigPath("$HOME") // adding home directory as first search path + viper.AutomaticEnv() // read in environment variables that match // If a config file is found, read it in. if err := viper.ReadInConfig(); err == nil { diff --git a/cmd/setup.go b/cmd/setup.go index 72f040c..68a9b4e 100644 --- a/cmd/setup.go +++ b/cmd/setup.go @@ -85,4 +85,4 @@ Match exec "ec2connect match --host %n --user %r" Include %s `, myConfPath)) return err -} \ No newline at end of file +} diff --git a/cmd/version.go b/cmd/version.go index 5769e8f..37e13d5 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -23,4 +23,3 @@ Date: %s RootCmd.AddCommand(cmd) } - diff --git a/pkg/ec2connect/authorize.go b/pkg/ec2connect/authorize.go index 2285aa9..ccbace8 100644 --- a/pkg/ec2connect/authorize.go +++ b/pkg/ec2connect/authorize.go @@ -53,7 +53,7 @@ func (c *Authorizer) Authorize(ctx context.Context, instanceId, user, sshKey str } return &ConnectionInfo{ - Address: ip, + Address: ip, RequestId: *r2.RequestId, }, nil } @@ -65,4 +65,4 @@ func NormalizeKey(input string) (string, error) { } return string(ssh.MarshalAuthorizedKey(s.PublicKey())), nil -} \ No newline at end of file +}