From b057bd20784893f0691f9bf97a2251f4baa51f9c Mon Sep 17 00:00:00 2001 From: sawka Date: Fri, 9 Aug 2024 18:54:09 -0700 Subject: [PATCH] wsh view with an http url will open web --- cmd/wsh/cmd/wshcmd-view.go | 55 +++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/cmd/wsh/cmd/wshcmd-view.go b/cmd/wsh/cmd/wshcmd-view.go index 0faf2855d..bad220168 100644 --- a/cmd/wsh/cmd/wshcmd-view.go +++ b/cmd/wsh/cmd/wshcmd-view.go @@ -7,6 +7,7 @@ import ( "io/fs" "os" "path/filepath" + "strings" "github.com/spf13/cobra" "github.com/wavetermdev/thenextwave/pkg/wshrpc" @@ -29,29 +30,41 @@ func init() { func viewRun(cmd *cobra.Command, args []string) { fileArg := args[0] - absFile, err := filepath.Abs(fileArg) - if err != nil { - WriteStderr("[error] getting absolute path: %v\n", err) - return - } - _, err = os.Stat(absFile) - if err == fs.ErrNotExist { - WriteStderr("[error] file does not exist: %q\n", absFile) - return - } - if err != nil { - WriteStderr("[error] getting file info: %v\n", err) - return - } - viewWshCmd := &wshrpc.CommandCreateBlockData{ - BlockDef: &wstore.BlockDef{ - Meta: map[string]interface{}{ - wstore.MetaKey_View: "preview", - wstore.MetaKey_File: absFile, + var wshCmd *wshrpc.CommandCreateBlockData + if strings.HasPrefix(fileArg, "http://") || strings.HasPrefix(fileArg, "https://") { + wshCmd = &wshrpc.CommandCreateBlockData{ + BlockDef: &wstore.BlockDef{ + Meta: map[string]interface{}{ + wstore.MetaKey_View: "web", + wstore.MetaKey_Url: fileArg, + }, }, - }, + } + } else { + absFile, err := filepath.Abs(fileArg) + if err != nil { + WriteStderr("[error] getting absolute path: %v\n", err) + return + } + _, err = os.Stat(absFile) + if err == fs.ErrNotExist { + WriteStderr("[error] file does not exist: %q\n", absFile) + return + } + if err != nil { + WriteStderr("[error] getting file info: %v\n", err) + return + } + wshCmd = &wshrpc.CommandCreateBlockData{ + BlockDef: &wstore.BlockDef{ + Meta: map[string]interface{}{ + wstore.MetaKey_View: "preview", + wstore.MetaKey_File: absFile, + }, + }, + } } - _, err = RpcClient.SendRpcRequest(wshrpc.Command_CreateBlock, viewWshCmd, 2000) + _, err := RpcClient.SendRpcRequest(wshrpc.Command_CreateBlock, wshCmd, 2000) if err != nil { WriteStderr("[error] running view command: %v\r\n", err) return