Skip to content

Commit 7ebfd85

Browse files
committed
feat: add a --new option to ssh command
This feature adds a -n or --new flag to the wsh ssh command which allows the connection to open in a new block.
1 parent 2b060eb commit 7ebfd85

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

cmd/wsh/cmd/wshcmd-ssh.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import (
1313
"github.com/wavetermdev/waveterm/pkg/wshrpc/wshclient"
1414
)
1515

16-
var identityFiles []string
16+
var (
17+
identityFiles []string
18+
newBlock bool
19+
)
1720

1821
var sshCmd = &cobra.Command{
1922
Use: "ssh",
@@ -25,6 +28,7 @@ var sshCmd = &cobra.Command{
2528

2629
func init() {
2730
sshCmd.Flags().StringArrayVarP(&identityFiles, "identityfile", "i", []string{}, "add an identity file for publickey authentication")
31+
sshCmd.Flags().BoolVarP(&newBlock, "new", "n", false, "create a new terminal block with this connection")
2832
rootCmd.AddCommand(sshCmd)
2933
}
3034

@@ -35,10 +39,11 @@ func sshRun(cmd *cobra.Command, args []string) (rtnErr error) {
3539

3640
sshArg := args[0]
3741
blockId := RpcContext.BlockId
38-
if blockId == "" {
42+
if blockId == "" && !newBlock {
3943
return fmt.Errorf("cannot determine blockid (not in JWT)")
4044
}
41-
// first, make a connection independent of the block
45+
46+
// Create connection request
4247
connOpts := wshrpc.ConnRequest{
4348
Host: sshArg,
4449
LogBlockId: blockId,
@@ -48,7 +53,30 @@ func sshRun(cmd *cobra.Command, args []string) (rtnErr error) {
4853
}
4954
wshclient.ConnConnectCommand(RpcClient, connOpts, &wshrpc.RpcOpts{Timeout: 60000})
5055

51-
// now, with that made, it will be straightforward to connect
56+
if newBlock {
57+
// Create a new block with the SSH connection
58+
createMeta := map[string]any{
59+
waveobj.MetaKey_View: "term",
60+
waveobj.MetaKey_Controller: "shell",
61+
waveobj.MetaKey_Connection: sshArg,
62+
}
63+
if RpcContext.Conn != "" {
64+
createMeta[waveobj.MetaKey_Connection] = RpcContext.Conn
65+
}
66+
createBlockData := wshrpc.CommandCreateBlockData{
67+
BlockDef: &waveobj.BlockDef{
68+
Meta: createMeta,
69+
},
70+
}
71+
oref, err := wshclient.CreateBlockCommand(RpcClient, createBlockData, nil)
72+
if err != nil {
73+
return fmt.Errorf("creating new terminal block: %w", err)
74+
}
75+
WriteStdout("new terminal block created with connection to %q: %s\n", sshArg, oref)
76+
return nil
77+
}
78+
79+
// Update existing block with the new connection
5280
data := wshrpc.CommandSetMetaData{
5381
ORef: waveobj.MakeORef(waveobj.OType_Block, blockId),
5482
Meta: map[string]any{

0 commit comments

Comments
 (0)