diff --git a/cmd/wsh/cmd/wshcmd-term.go b/cmd/wsh/cmd/wshcmd-term.go new file mode 100644 index 000000000..c3b83d2b5 --- /dev/null +++ b/cmd/wsh/cmd/wshcmd-term.go @@ -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) +} diff --git a/pkg/blockcontroller/blockcontroller.go b/pkg/blockcontroller/blockcontroller.go index a940931b9..36693bdfe 100644 --- a/pkg/blockcontroller/blockcontroller.go +++ b/pkg/blockcontroller/blockcontroller.go @@ -414,6 +414,17 @@ func getBoolFromMeta(meta map[string]any, key string, def bool) bool { 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) { defer func() { 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) if runOnStart { go func() { - err := bc.DoRunShellCommand(&RunShellOpts{TermSize: bdata.RuntimeOpts.TermSize}, bdata.Meta) + err := bc.DoRunShellCommand(&RunShellOpts{TermSize: getTermSize(bdata)}, bdata.Meta) if err != nil { log.Printf("error running shell: %v\n", err) } @@ -469,7 +480,7 @@ func (bc *BlockController) RestartController() error { if err != nil { 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 { log.Printf("error running shell command: %v\n", err) }