Skip to content

Commit

Permalink
Fix #9: Add an ability to execute a single command via SSH
Browse files Browse the repository at this point in the history
  • Loading branch information
sibprogrammer committed Dec 26, 2022
1 parent e915457 commit 6c4e84d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
6 changes: 4 additions & 2 deletions cmd/servers/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ var SSHCmd = &cobra.Command{
Use: "ssh [SERVER]",
Short: locales.L.Get("server.ssh.description"),
RunE: func(cmd *cobra.Command, args []string) error {
additionalCommand, _ := cmd.Flags().GetString("command")
cmd.SilenceUsage = true

server, err := config.GetServerByArgs(args)
if err != nil {
return err
}

return actions.ServerSSH(*server)
return actions.ServerSSH(*server, additionalCommand)
},
Args: cobra.MaximumNArgs(1),
}

func init() {
SSHCmd.Flags().StringP("command", "c", "", locales.L.Get("server.ssh.flag.command"))

ServersCmd.AddCommand(SSHCmd)
}
5 changes: 4 additions & 1 deletion cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ var sshCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
return serversCmd.SSHCmd.RunE(cmd, args)
},
Args: cobra.MaximumNArgs(1),
}

func init() {
sshCmd.Flags().StringP("command", "c", "", locales.L.Get("server.ssh.flag.command"))
}
10 changes: 7 additions & 3 deletions internal/actions/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,14 @@ func ServerLogin(host types.Server, generateOnly bool) error {
return nil
}

func ServerSSH(host types.Server) error {
fmt.Printf("Login to %s using SSH...\n", host.Host)
func ServerSSH(host types.Server, additionalCommand string) error {
if additionalCommand == "" {
fmt.Printf("Login to %s using SSH...\n", host.Host)
} else {
fmt.Printf("Attempt to execute '%s' at %s via SSH...\n", additionalCommand, host.Host)
}

cmd := exec.Command("ssh", host.Host)
cmd := exec.Command("ssh", host.Host, additionalCommand)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down
1 change: 1 addition & 0 deletions internal/locales/locales.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func init() {
"server.login.success": "Generated login link: %s",
"server.login.generate.flag": "Only generate the link (without autologin attempt)",
"server.ssh.description": "Login to server using SSH",
"server.ssh.flag.command": "Command to execute",

"errors.server.remove.failure": "Could not remove server %s: %s",
"errors.feature.not.supported": "Feature %s is not supported on Windows",
Expand Down

0 comments on commit 6c4e84d

Please sign in to comment.