Skip to content

Commit

Permalink
feat: options flag in daytona ssh (#1275)
Browse files Browse the repository at this point in the history
Signed-off-by: bryans-go <[email protected]>
  • Loading branch information
bryans-go authored Oct 22, 2024
1 parent 0d6d122 commit ede3673
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
3 changes: 2 additions & 1 deletion docs/daytona_ssh.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ daytona ssh [WORKSPACE] [PROJECT] [CMD...] [flags]
### Options

```
-y, --yes Automatically confirm any prompts
-o, --option stringArray Specify SSH options in KEY=VALUE format.
-y, --yes Automatically confirm any prompts
```

### Options inherited from parent commands
Expand Down
4 changes: 4 additions & 0 deletions hack/docs/daytona_ssh.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ name: daytona ssh
synopsis: SSH into a project using the terminal
usage: daytona ssh [WORKSPACE] [PROJECT] [CMD...] [flags]
options:
- name: option
shorthand: o
default_value: '[]'
usage: Specify SSH options in KEY=VALUE format.
- name: "yes"
shorthand: "y"
default_value: "false"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/workspace/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func openIDE(ideId string, activeProfile config.Profile, workspaceId string, pro
case "vscode":
return ide.OpenVSCode(activeProfile, workspaceId, projectName, projectProviderMetadata, gpgKey)
case "ssh":
return ide.OpenTerminalSsh(activeProfile, workspaceId, projectName, gpgKey)
return ide.OpenTerminalSsh(activeProfile, workspaceId, projectName, gpgKey, nil)
case "browser":
return ide.OpenBrowserIDE(activeProfile, workspaceId, projectName, projectProviderMetadata, gpgKey)
case "cursor":
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/workspace/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"github.com/spf13/cobra"
)

var sshOptions []string

var SshCmd = &cobra.Command{
Use: "ssh [WORKSPACE] [PROJECT] [CMD...]",
Short: "SSH into a project using the terminal",
Expand Down Expand Up @@ -103,7 +105,7 @@ var SshCmd = &cobra.Command{
log.Warn(err)
}

return ide.OpenTerminalSsh(activeProfile, workspace.Id, projectName, gpgKey, sshArgs...)
return ide.OpenTerminalSsh(activeProfile, workspace.Id, projectName, gpgKey, sshOptions, sshArgs...)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) >= 2 {
Expand All @@ -119,4 +121,5 @@ var SshCmd = &cobra.Command{

func init() {
SshCmd.Flags().BoolVarP(&yesFlag, "yes", "y", false, "Automatically confirm any prompts")
SshCmd.Flags().StringArrayVarP(&sshOptions, "option", "o", []string{}, "Specify SSH options in KEY=VALUE format.")
}
39 changes: 34 additions & 5 deletions pkg/ide/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@
package ide

import (
"fmt"
"os"
"os/exec"
"strings"

"github.com/daytonaio/daytona/cmd/daytona/config"
)

func OpenTerminalSsh(activeProfile config.Profile, workspaceId string, projectName string, gpgKey string, args ...string) error {
err := config.EnsureSshConfigEntryAdded(activeProfile.Id, workspaceId, projectName, gpgKey)
func OpenTerminalSsh(activeProfile config.Profile, workspaceId string, projectName string, gpgKey string, sshOptions []string, args ...string) error {
if err := config.EnsureSshConfigEntryAdded(activeProfile.Id, workspaceId, projectName, gpgKey); err != nil {
return err
}

// Parse SSH options
parsedOptions, err := parseSshOptions(sshOptions)
if err != nil {
return err
}

projectHostname := config.GetProjectHostname(activeProfile.Id, workspaceId, projectName)

cmdArgs := []string{projectHostname}
cmdArgs = append(cmdArgs, args...)
cmdArgs := buildCommandArgs(projectHostname, parsedOptions, args...)

sshCommand := exec.Command("ssh", cmdArgs...)
sshCommand.Stdin = os.Stdin
Expand All @@ -28,3 +33,27 @@ func OpenTerminalSsh(activeProfile config.Profile, workspaceId string, projectNa

return sshCommand.Run()
}

// parseSshOptions validates and parses the SSH options.
func parseSshOptions(sshOptions []string) (map[string]string, error) {
parsedOptions := make(map[string]string)
for _, option := range sshOptions {
parts := strings.SplitN(option, "=", 2)
if len(parts) == 1 {
return nil, fmt.Errorf("no argument after keyword %q", parts[0])
}
if len(parts) != 2 || strings.Count(option, "=") > 1 {
return nil, fmt.Errorf("bad configuration option: %s", option)
}
parsedOptions[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
}
return parsedOptions, nil
}

func buildCommandArgs(projectHostname string, parsedOptions map[string]string, args ...string) []string {
cmdArgs := []string{projectHostname}
for key, value := range parsedOptions {
cmdArgs = append(cmdArgs, "-o", fmt.Sprintf("%s=%s", key, value))
}
return append(cmdArgs, args...)
}

0 comments on commit ede3673

Please sign in to comment.