wsh view with an http url will open web

This commit is contained in:
sawka 2024-08-09 18:54:09 -07:00
parent c192fe2663
commit b057bd2078

View File

@ -7,6 +7,7 @@ import (
"io/fs" "io/fs"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/wavetermdev/thenextwave/pkg/wshrpc" "github.com/wavetermdev/thenextwave/pkg/wshrpc"
@ -29,29 +30,41 @@ func init() {
func viewRun(cmd *cobra.Command, args []string) { func viewRun(cmd *cobra.Command, args []string) {
fileArg := args[0] fileArg := args[0]
absFile, err := filepath.Abs(fileArg) var wshCmd *wshrpc.CommandCreateBlockData
if err != nil { if strings.HasPrefix(fileArg, "http://") || strings.HasPrefix(fileArg, "https://") {
WriteStderr("[error] getting absolute path: %v\n", err) wshCmd = &wshrpc.CommandCreateBlockData{
return BlockDef: &wstore.BlockDef{
} Meta: map[string]interface{}{
_, err = os.Stat(absFile) wstore.MetaKey_View: "web",
if err == fs.ErrNotExist { wstore.MetaKey_Url: fileArg,
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,
}, },
}, }
} 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 { if err != nil {
WriteStderr("[error] running view command: %v\r\n", err) WriteStderr("[error] running view command: %v\r\n", err)
return return