add wsh term to open a new terminal in directory

This commit is contained in:
sawka 2024-08-12 16:14:19 -07:00
parent f464223aab
commit a2aef5b0ce
2 changed files with 75 additions and 2 deletions

View File

@ -0,0 +1,62 @@
// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/wavetermdev/thenextwave/pkg/wavebase"
"github.com/wavetermdev/thenextwave/pkg/wshrpc"
"github.com/wavetermdev/thenextwave/pkg/wshrpc/wshclient"
"github.com/wavetermdev/thenextwave/pkg/wstore"
)
var termCmd = &cobra.Command{
Use: "term",
Short: "open a terminal in directory",
Args: cobra.RangeArgs(0, 1),
Run: termRun,
}
func init() {
rootCmd.AddCommand(termCmd)
}
func termRun(cmd *cobra.Command, args []string) {
var cwd string
if len(args) > 0 {
cwd = args[0]
cwd = wavebase.ExpandHomeDir(cwd)
} else {
var err error
cwd, err = os.Getwd()
if err != nil {
WriteStderr("[error] getting current directory: %v\n", err)
return
}
}
var err error
cwd, err = filepath.Abs(cwd)
if err != nil {
WriteStderr("[error] getting absolute path: %v\n", err)
return
}
createBlockData := wshrpc.CommandCreateBlockData{
BlockDef: &wstore.BlockDef{
Meta: map[string]interface{}{
wstore.MetaKey_View: "term",
wstore.MetaKey_CmdCwd: cwd,
wstore.MetaKey_Controller: "shell",
},
},
}
oref, err := wshclient.CreateBlockCommand(RpcClient, createBlockData, nil)
if err != nil {
WriteStderr("[error] creating new terminal block: %v\n", err)
return
}
WriteStdout("terminal block created: %s\n", oref)
}

View File

@ -414,6 +414,17 @@ func getBoolFromMeta(meta map[string]any, key string, def bool) bool {
return def return def
} }
func getTermSize(bdata *wstore.Block) shellexec.TermSize {
if bdata.RuntimeOpts != nil {
return bdata.RuntimeOpts.TermSize
} else {
return shellexec.TermSize{
Rows: 25,
Cols: 80,
}
}
}
func (bc *BlockController) run(bdata *wstore.Block, blockMeta map[string]any) { func (bc *BlockController) run(bdata *wstore.Block, blockMeta map[string]any) {
defer func() { defer func() {
bc.UpdateControllerAndSendUpdate(func() bool { bc.UpdateControllerAndSendUpdate(func() bool {
@ -445,7 +456,7 @@ func (bc *BlockController) run(bdata *wstore.Block, blockMeta map[string]any) {
runOnStart := getBoolFromMeta(blockMeta, wstore.MetaKey_CmdRunOnStart, true) runOnStart := getBoolFromMeta(blockMeta, wstore.MetaKey_CmdRunOnStart, true)
if runOnStart { if runOnStart {
go func() { go func() {
err := bc.DoRunShellCommand(&RunShellOpts{TermSize: bdata.RuntimeOpts.TermSize}, bdata.Meta) err := bc.DoRunShellCommand(&RunShellOpts{TermSize: getTermSize(bdata)}, bdata.Meta)
if err != nil { if err != nil {
log.Printf("error running shell: %v\n", err) log.Printf("error running shell: %v\n", err)
} }
@ -469,7 +480,7 @@ func (bc *BlockController) RestartController() error {
if err != nil { if err != nil {
return fmt.Errorf("error getting block: %w", err) return fmt.Errorf("error getting block: %w", err)
} }
err = bc.DoRunShellCommand(&RunShellOpts{TermSize: bdata.RuntimeOpts.TermSize}, bdata.Meta) err = bc.DoRunShellCommand(&RunShellOpts{TermSize: getTermSize(bdata)}, bdata.Meta)
if err != nil { if err != nil {
log.Printf("error running shell command: %v\n", err) log.Printf("error running shell command: %v\n", err)
} }