Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ssh username flag #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ var (
var sshCommand string
var targetName string
if exec == "" {
user := strings.TrimSpace(viper.GetString("ssh-user"))
target, err := internal.AskTarget(ctx, *_credential.awsConfig)
if err != nil {
panicRed(err)
}
targetName = target.Name

sshUser, err := internal.AskUser()
sshUser, err := internal.AskUser(user)
if err != nil {
panicRed(err)
}
Expand Down Expand Up @@ -93,6 +93,7 @@ var (
}

// call ssh
sshArgsFromCommand := strings.TrimSpace(viper.GetString("ssh-args"))
proxy := fmt.Sprintf("ProxyCommand=%s '%s' %s %s %s '%s'",
_credential.ssmPluginPath, string(sessJson), _credential.awsConfig.Region,
"StartSession", _credential.awsProfile, string(paramsJson))
Expand All @@ -102,6 +103,11 @@ var (
sshArgs = append(sshArgs, sep)
}
}
for _, sep := range strings.Split(sshArgsFromCommand, " ") {
if sep != "" {
sshArgs = append(sshArgs, sep)
}
}

if err := internal.CallProcess("ssh", sshArgs...); err != nil {
color.Red("%v", err)
Expand All @@ -120,10 +126,14 @@ func init() {
// add sub command
sshCommand.Flags().StringP("exec", "e", "", "[optional] ssh $exec, ex) \"-i ex.pem ubuntu@server\"")
sshCommand.Flags().StringP("identity", "i", "", "[optional] identity file path, ex) $HOME/.ssh/id_rsa")
sshCommand.Flags().StringP("user", "u", "", "[optional] ssh username, ex) ubuntu")
sshCommand.Flags().StringP("args", "a", "", "[optional] ssh $args, ex) \"-o UserKnownHostsFile=/dev/null -A\"")

// mapping viper
viper.BindPFlag("ssh-exec", sshCommand.Flags().Lookup("exec"))
viper.BindPFlag("ssh-identity", sshCommand.Flags().Lookup("identity"))
viper.BindPFlag("ssh-user", sshCommand.Flags().Lookup("user"))
viper.BindPFlag("ssh-args", sshCommand.Flags().Lookup("args"))

rootCmd.AddCommand(sshCommand)
}
5 changes: 4 additions & 1 deletion internal/ssm.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ type (
)

// AskUser asks you which selects a user.
func AskUser() (*User, error) {
func AskUser(username string) (*User, error) {
prompt := &survey.Input{
Message: "Type your connect ssh user (default: root):",
}
if username != "" {
return &User{Name: username}, nil
}
var user string
survey.AskOne(prompt, &user)
user = strings.TrimSpace(user)
Expand Down