|
| 1 | +// Copyright 2025, Command Line Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package cmd |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/wavetermdev/waveterm/pkg/wshrpc" |
| 11 | + "github.com/wavetermdev/waveterm/pkg/wshrpc/wshclient" |
| 12 | +) |
| 13 | + |
| 14 | +var magnifyBlock bool |
| 15 | + |
| 16 | +var launchCmd = &cobra.Command{ |
| 17 | + Use: "launch", |
| 18 | + Short: "launch a widget by its ID", |
| 19 | + Args: cobra.ExactArgs(1), |
| 20 | + RunE: launchRun, |
| 21 | + PreRunE: preRunSetupRpcClient, |
| 22 | +} |
| 23 | + |
| 24 | +func init() { |
| 25 | + launchCmd.Flags().BoolVarP(&magnifyBlock, "magnify", "m", false, "start the widget in magnified mode") |
| 26 | + rootCmd.AddCommand(launchCmd) |
| 27 | +} |
| 28 | + |
| 29 | +func launchRun(cmd *cobra.Command, args []string) (rtnErr error) { |
| 30 | + defer func() { |
| 31 | + sendActivity("launch", rtnErr == nil) |
| 32 | + }() |
| 33 | + |
| 34 | + widgetId := args[0] |
| 35 | + |
| 36 | + // Get the full configuration |
| 37 | + config, err := wshclient.GetFullConfigCommand(RpcClient, &wshrpc.RpcOpts{Timeout: 2000}) |
| 38 | + if err != nil { |
| 39 | + return fmt.Errorf("getting configuration: %w", err) |
| 40 | + } |
| 41 | + |
| 42 | + // Look for widget in both widgets and defaultwidgets |
| 43 | + widget, ok := config.Widgets[widgetId] |
| 44 | + if !ok { |
| 45 | + widget, ok = config.DefaultWidgets[widgetId] |
| 46 | + if !ok { |
| 47 | + return fmt.Errorf("widget %q not found in configuration", widgetId) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Create block data from widget config |
| 52 | + createBlockData := wshrpc.CommandCreateBlockData{ |
| 53 | + BlockDef: &widget.BlockDef, |
| 54 | + Magnified: magnifyBlock || widget.Magnified, |
| 55 | + } |
| 56 | + |
| 57 | + // Create the block |
| 58 | + oref, err := wshclient.CreateBlockCommand(RpcClient, createBlockData, nil) |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("creating widget block: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + WriteStdout("launched widget %q: %s\n", widgetId, oref) |
| 64 | + return nil |
| 65 | +} |
0 commit comments