diff options
author | Ashley Cui <acui@redhat.com> | 2021-09-16 15:17:02 -0400 |
---|---|---|
committer | Ashley Cui <acui@redhat.com> | 2021-09-17 13:33:56 -0400 |
commit | e3e9e5175fdb9cbec8bf0c603b8b336eaad7808c (patch) | |
tree | 1f875d2e91126c08fcbc52f207c14f619920bcf3 /cmd/podman/machine/ssh.go | |
parent | 9119a578e782b92bd344f093f5491c318bc20d69 (diff) | |
download | podman-e3e9e5175fdb9cbec8bf0c603b8b336eaad7808c.tar.gz podman-e3e9e5175fdb9cbec8bf0c603b8b336eaad7808c.tar.bz2 podman-e3e9e5175fdb9cbec8bf0c603b8b336eaad7808c.zip |
Add username flag for machine ssh
allow users to specify what username to use when ssh-ing into the vm.
If the username flag is set, the username will be the flag value. If the
flag is not set and the the vm name is specified, the default user of
the vm will be used. if the flag is not set, and the vm name is not
specified, then the username of the default connection will be used.
Signed-off-by: Ashley Cui <acui@redhat.com>
Diffstat (limited to 'cmd/podman/machine/ssh.go')
-rw-r--r-- | cmd/podman/machine/ssh.go | 29 |
1 files changed, 13 insertions, 16 deletions
diff --git a/cmd/podman/machine/ssh.go b/cmd/podman/machine/ssh.go index 84e9e88ab..da0a09338 100644 --- a/cmd/podman/machine/ssh.go +++ b/cmd/podman/machine/ssh.go @@ -5,6 +5,7 @@ package machine import ( "net/url" + "github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/config" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/pkg/machine" @@ -15,7 +16,7 @@ import ( var ( sshCmd = &cobra.Command{ - Use: "ssh [NAME] [COMMAND [ARG ...]]", + Use: "ssh [options] [NAME] [COMMAND [ARG ...]]", Short: "SSH into an existing machine", Long: "SSH into a managed virtual machine ", RunE: ssh, @@ -35,6 +36,10 @@ func init() { Command: sshCmd, Parent: machineCmd, }) + flags := sshCmd.Flags() + usernameFlagName := "username" + flags.StringVar(&sshOpts.Username, usernameFlagName, "", "Username to use when ssh-ing into the VM.") + _ = sshCmd.RegisterFlagCompletionFunc(usernameFlagName, completion.AutocompleteNone) } func ssh(cmd *cobra.Command, args []string) error { @@ -48,13 +53,6 @@ func ssh(cmd *cobra.Command, args []string) error { // Set the VM to default vmName := defaultMachineName - // If we're not given a VM name, use the remote username from the connection config - if len(args) == 0 { - sshOpts.Username, err = remoteConnectionUsername() - if err != nil { - return err - } - } // If len is greater than 0, it means we may have been // provided the VM name. If so, we check. The VM name, // if provided, must be in args[0]. @@ -68,10 +66,6 @@ func ssh(cmd *cobra.Command, args []string) error { if validVM { vmName = args[0] } else { - sshOpts.Username, err = remoteConnectionUsername() - if err != nil { - return err - } sshOpts.Args = append(sshOpts.Args, args[0]) } } @@ -83,14 +77,17 @@ func ssh(cmd *cobra.Command, args []string) error { if validVM { sshOpts.Args = args[1:] } else { - sshOpts.Username, err = remoteConnectionUsername() - if err != nil { - return err - } sshOpts.Args = args } } + if !validVM && sshOpts.Username == "" { + sshOpts.Username, err = remoteConnectionUsername() + if err != nil { + return err + } + } + switch vmType { default: vm, err = qemu.LoadVMByName(vmName) |