Skip to content

Commit

Permalink
only allow run, open and copy actions
Browse files Browse the repository at this point in the history
  • Loading branch information
pomdtr committed Feb 10, 2025
1 parent e96d1b0 commit 2aff68a
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 192 deletions.
2 changes: 0 additions & 2 deletions internal/cli/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,6 @@ func runExtension(extension extensions.Extension, command sunbeam.Command, param
}

return runExtension(extension, command, action.Run.Params)
case sunbeam.ActionTypeReload:
return nil
case sunbeam.ActionTypeOpen:
return utils.Open(action.Open.Url)
case sunbeam.ActionTypeCopy:
Expand Down
28 changes: 25 additions & 3 deletions internal/cli/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,31 @@ import (
"io"
"net/http"
"net/url"
"os"

"github.com/spf13/cobra"
)

func NewCmdFetch() *cobra.Command {
var flags struct {
token string
}

cmd := &cobra.Command{
Use: "fetch",
Short: "Fetch a remote extension",
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 1 {
resp, err := http.Get(args[0])
req, err := http.NewRequest("GET", args[0], nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}

if flags.token != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", flags.token))
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed to fetch extension: %w", err)
}
Expand All @@ -36,7 +48,15 @@ func NewCmdFetch() *cobra.Command {
return fmt.Errorf("failed to join URL path: %w", err)
}

resp, err := http.Post(target, "application/json", os.Stdin)
req, err := http.NewRequest("POST", target, cmd.InOrStdin())
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
if flags.token != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", flags.token))
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed to fetch extension: %w", err)
}
Expand All @@ -51,5 +71,7 @@ func NewCmdFetch() *cobra.Command {
},
}

cmd.Flags().StringVarP(&flags.token, "token", "t", "", "API token")

return cmd
}
25 changes: 21 additions & 4 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ var (

func NewRootCmd() (*cobra.Command, error) {
var flags struct {
reload bool
reloadExtensions bool
reloadExtension string
}
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand All @@ -37,13 +38,28 @@ func NewRootCmd() (*cobra.Command, error) {
See https://pomdtr.github.io/sunbeam for more information.`,
RunE: func(cmd *cobra.Command, args []string) error {
if flags.reload {
if flags.reloadExtensions {
exts, err := LoadExtensions(utils.ExtensionsDir(), true)
if err != nil {
return fmt.Errorf("failed to reload extensions: %w", err)
}

fmt.Fprintf(os.Stderr, "Reloaded %d extensions\n", len(exts))
fmt.Fprintf(cmd.ErrOrStderr(), "Reloaded %d extensions\n", len(exts))
return nil
}

if flags.reloadExtension != "" {
entrypoint, err := extensions.FindEntrypoint(utils.ExtensionsDir(), flags.reloadExtension)
if err != nil {
return fmt.Errorf("failed to find extension: %w", err)
}

if _, err := extensions.LoadExtension(entrypoint, true); err != nil {
return fmt.Errorf("failed to reload extension: %w", err)
}

fmt.Fprintf(cmd.ErrOrStderr(), "Reloaded extension %s\n", flags.reloadExtension)
return nil
}

if !term.IsTerminal(int(os.Stdout.Fd())) {
Expand Down Expand Up @@ -80,7 +96,8 @@ See https://pomdtr.github.io/sunbeam for more information.`,
},
}

rootCmd.Flags().BoolVar(&flags.reload, "reload", false, "Reload extension manifests")
rootCmd.Flags().BoolVar(&flags.reloadExtensions, "reload-extensions", false, "Reload extension manifests")
rootCmd.Flags().StringVar(&flags.reloadExtension, "reload-extension", "", "Reload a specific extension manifest")
rootCmd.AddCommand(NewCmdValidate())
rootCmd.AddCommand(NewCmdFetch())
rootCmd.AddCommand(NewCmdServe())
Expand Down
65 changes: 2 additions & 63 deletions internal/schemas/action.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
"enum": [
"copy",
"open",
"edit",
"run",
"reload",
"exit"
"run"
]
},
"title": {
Expand Down Expand Up @@ -42,9 +39,6 @@
"properties": {
"text": {
"type": "string"
},
"exit": {
"type": "boolean"
}
}
}
Expand Down Expand Up @@ -72,35 +66,6 @@
}
}
},
{
"if": {
"required": [
"type"
],
"properties": {
"type": {
"const": "edit"
}
}
},
"then": {
"type": "object",
"required": [
"path"
],
"properties": {
"path": {
"type": "string"
},
"reload": {
"type": "boolean"
},
"exit": {
"type": "boolean"
}
}
}
},
{
"if": {
"required": [
Expand All @@ -121,37 +86,11 @@
"command": {
"type": "string"
},
"params": {
"$ref": "./params.schema.json"
},
"exit": {
"type": "boolean"
},
"reload": {
"type": "boolean"
}
}
}
},
{
"if": {
"required": [
"type"
],
"properties": {
"type": {
"const": "reload"
}
}
},
"then": {
"type": "object",
"properties": {
"params": {
"$ref": "./params.schema.json"
}
}
}
}
]
}
}
47 changes: 14 additions & 33 deletions internal/tui/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,24 @@ func (c *RootList) Update(msg tea.Msg) (Page, tea.Cmd) {
case "ctrl+r":
return c, tea.Batch(c.list.SetIsLoading(true), c.Reload())
case "ctrl+e":
return c, func() tea.Msg {
item, ok := c.list.Selection()
if !ok {
return nil
}
item, ok := c.list.Selection()
if !ok {
return c, nil
}

extensionName := item.Actions[0].Run.Extension
entrypoint, err := extensions.FindEntrypoint(utils.ExtensionsDir(), extensionName)
extensionName := item.Actions[0].Run.Extension
entrypoint, err := extensions.FindEntrypoint(utils.ExtensionsDir(), extensionName)
if err != nil {
return c, c.SetError(err)
}

return c, tea.ExecProcess(exec.Command(utils.FindEditor(), entrypoint), func(err error) tea.Msg {
if err != nil {
return fmt.Errorf("extension %s not found", extensionName)
return err
}

return sunbeam.Action{
Type: sunbeam.ActionTypeEdit,
Edit: &sunbeam.EditAction{Path: entrypoint},
}
}
return c.Reload()
})
}
case ReloadMsg:
return c, tea.Batch(c.list.SetIsLoading(true), c.Reload())
Expand Down Expand Up @@ -173,7 +174,6 @@ func (c *RootList) Update(msg tea.Msg) (Page, tea.Cmd) {
Run: &sunbeam.RunAction{
Extension: msg.Run.Extension,
Command: msg.Run.Command,
Reload: msg.Run.Reload,
Params: params,
},
}
Expand Down Expand Up @@ -240,23 +240,6 @@ func (c *RootList) Update(msg tea.Msg) (Page, tea.Cmd) {

return ShowNotificationMsg{"Copied!"}
}
case sunbeam.ActionTypeEdit:
editCmd := exec.Command(utils.FindEditor(), msg.Edit.Path)
return c, tea.ExecProcess(editCmd, func(err error) tea.Msg {
if err != nil {
return err
}

if msg.Edit.Reload {
return c.Reload()
}

if msg.Exit {
return ExitMsg{}
}

return nil
})
case sunbeam.ActionTypeOpen:
return c, func() tea.Msg {
if err := utils.Open(msg.Open.Url); err != nil {
Expand All @@ -269,8 +252,6 @@ func (c *RootList) Update(msg tea.Msg) (Page, tea.Cmd) {

return nil
}
case sunbeam.ActionTypeReload:
return c, tea.Sequence(c.err.SetIsLoading(true), c.Reload())
default:
return c, nil
}
Expand Down
36 changes: 3 additions & 33 deletions internal/tui/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ func (c *Runner) Update(msg tea.Msg) (Page, tea.Cmd) {
Run: &sunbeam.RunAction{
Extension: msg.Run.Extension,
Command: msg.Run.Command,
Reload: msg.Run.Reload,
Params: params,
},
}
Expand All @@ -188,16 +187,15 @@ func (c *Runner) Update(msg tea.Msg) (Page, tea.Cmd) {
case sunbeam.CommandModeSilent:
return c, func() tea.Msg {
_, err := c.extension.Output(context.Background(), command, params)

if err != nil {
return PushPageMsg{NewErrorPage(err)}
}

if msg.Run.Reload {
return ReloadMsg{}
if msg.Exit {
return ExitMsg{}
}

return nil
return ReloadMsg{}
}
case sunbeam.CommandModeAction:
return c, func() tea.Msg {
Expand All @@ -214,24 +212,6 @@ func (c *Runner) Update(msg tea.Msg) (Page, tea.Cmd) {
return action
}
}
case sunbeam.ActionTypeEdit:
editCmd := exec.Command(utils.FindEditor(), msg.Edit.Path)
return c, tea.ExecProcess(editCmd, func(err error) tea.Msg {
if err != nil {
return err
}

if msg.Edit.Reload {
c.embed.Focus()
return c.Reload()
}

if msg.Exit {
return ExitMsg{}
}

return c.embed.Focus()
})
case sunbeam.ActionTypeCopy:
return c, func() tea.Msg {
if err := clipboard.WriteAll(msg.Copy.Text); err != nil {
Expand All @@ -256,16 +236,6 @@ func (c *Runner) Update(msg tea.Msg) (Page, tea.Cmd) {

return nil
}
case sunbeam.ActionTypeReload:
if c.params == nil {
c.params = make(map[string]any)
}

for k, v := range msg.Reload.Params {
c.params[k] = v
}

return c, c.Reload()
}

case error:
Expand Down
8 changes: 3 additions & 5 deletions internal/tui/statusbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,11 @@ func ActionTitle(action sunbeam.Action) string {

switch action.Type {
case sunbeam.ActionTypeRun:
return "Run"
return "Run Command"
case sunbeam.ActionTypeCopy:
return "Copy"
return "Copy Text"
case sunbeam.ActionTypeOpen:
return "Open"
case sunbeam.ActionTypeEdit:
return "Edit"
return "Open URL"
default:
return string(action.Type)
}
Expand Down
Loading

0 comments on commit 2aff68a

Please sign in to comment.