Skip to content

Commit

Permalink
restructure project
Browse files Browse the repository at this point in the history
  • Loading branch information
pomdtr committed Nov 21, 2023
1 parent 4d2c653 commit 8612580
Show file tree
Hide file tree
Showing 59 changed files with 227 additions and 186 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ jobs:
uses: denoland/deployctl@v1
with:
project: sunbeam # the name of the project on Deno Deploy
root: website/backend
root: website/server
entrypoint: main.ts # the entrypoint to deploy
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"public": true,
},
"yaml.validate": false,
"deno.disablePaths": [
"./website/frontend"
"deno.enablePaths": [
"./extensions",
"./website/server"
],
"deno.enable": true,
}
22 changes: 21 additions & 1 deletion internal/cli/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"strings"

"github.com/mattn/go-isatty"
"github.com/pomdtr/sunbeam/internal/config"
"github.com/pomdtr/sunbeam/internal/extensions"
"github.com/pomdtr/sunbeam/internal/history"
"github.com/pomdtr/sunbeam/internal/tui"
"github.com/pomdtr/sunbeam/internal/types"
"github.com/spf13/cobra"
Expand All @@ -33,7 +35,25 @@ func NewCmdCustom(alias string, extension extensions.Extension, extensionConfig
}

if len(inputBytes) == 0 {
return cmd.Usage()
items := extensionListItems(alias, extension, extensionConfig)
if len(items) == 0 {
return cmd.Usage()
}

history, err := history.Load(history.Path)
if err != nil {
return err
}

rootList := tui.NewRootList(extension.Manifest.Title, history, func() (config.Config, []types.ListItem, error) {
cfg := config.Config{
Extensions: map[string]extensions.Config{alias: extensionConfig},
}

return cfg, items, nil
})

return tui.Draw(rootList)
}

var input types.Payload
Expand Down
236 changes: 73 additions & 163 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cli
import (
_ "embed"
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -38,33 +37,10 @@ var configBytes []byte
func NewRootCmd() (*cobra.Command, error) {
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "sunbeam",
Short: "Command Line Launcher",
SilenceUsage: true,
DisableFlagParsing: true,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return nil, cobra.ShellCompDirectiveDefault
}

entrypoint, err := filepath.Abs(args[0])
if err != nil {
return nil, cobra.ShellCompDirectiveDefault
}

extension, err := extensions.ExtractManifest(entrypoint)
if err != nil {
return nil, cobra.ShellCompDirectiveDefault
}

var completions []string
for _, command := range extension.Commands {
completions = append(completions, fmt.Sprintf("%s\t%s", command.Name, command.Title))
}

return completions, cobra.ShellCompDirectiveNoFileComp
},
Args: cobra.ArbitraryArgs,
Use: "sunbeam",
Short: "Command Line Launcher",
SilenceUsage: true,
Args: cobra.NoArgs,
Long: `Sunbeam is a command line launcher for your terminal, inspired by fzf and raycast.
See https://pomdtr.github.io/sunbeam for more information.`,
Expand All @@ -81,6 +57,7 @@ See https://pomdtr.github.io/sunbeam for more information.`,
rootCmd.AddCommand(NewCmdCopy())
rootCmd.AddCommand(NewCmdPaste())
rootCmd.AddCommand(NewCmdOpen())
rootCmd.AddCommand(NewCmdRun())

docCmd := &cobra.Command{
Use: "docs",
Expand Down Expand Up @@ -176,100 +153,34 @@ See https://pomdtr.github.io/sunbeam for more information.`,
}

rootCmd.RunE = func(cmd *cobra.Command, args []string) error {
if len(args) > 0 && (args[0] == "-h" || args[0] == "--help") {
return cmd.Help()
history, err := history.Load(history.Path)
if err != nil {
return err
}

if len(args) == 0 {
history, err := history.Load(history.Path)
rootList := tui.NewRootList("Sunbeam", history, func() (config.Config, []types.ListItem, error) {
cfg, err := config.Load(config.Path)
if err != nil {
return err
return config.Config{}, nil, err
}

rootList := tui.NewRootList("Sunbeam", history, func() (config.Config, []types.ListItem, error) {
cfg, err := config.Load(config.Path)
if err != nil {
return config.Config{}, nil, err
}
var items []types.ListItem
items = append(items, onelinerListItems(cfg.Oneliners)...)

extensionMap := make(map[string]extensions.Extension)
for alias, extensionConfig := range cfg.Extensions {
extension, err := extensions.LoadExtension(extensionConfig.Origin)
if err != nil {
continue
}
extensionMap[alias] = extension
extensionMap := make(map[string]extensions.Extension)
for alias, extensionConfig := range cfg.Extensions {
extension, err := extensions.LoadExtension(extensionConfig.Origin)
if err != nil {
continue
}

return cfg, extractListItems(cfg, extensionMap), nil
})
return tui.Draw(rootList)
}

var entrypoint string
if args[0] == "-" {
tempfile, err := os.CreateTemp("", "entrypoint-*%s")
if err != nil {
return err
}
defer os.Remove(tempfile.Name())

if _, err := io.Copy(tempfile, os.Stdin); err != nil {
return err
}

if err := tempfile.Close(); err != nil {
return err
}

entrypoint = tempfile.Name()
} else if extensions.IsRemote(args[0]) {
tempfile, err := os.CreateTemp("", "entrypoint-*%s")
if err != nil {
return err
}
defer os.Remove(tempfile.Name())

if err := extensions.DownloadEntrypoint(args[0], tempfile.Name()); err != nil {
return err
}

entrypoint = tempfile.Name()
} else {
e, err := filepath.Abs(args[0])
if err != nil {
return err
extensionMap[alias] = extension
items = append(items, extensionListItems(alias, extension, extensionConfig)...)
}

if _, err := os.Stat(e); err != nil {
return fmt.Errorf("error loading extension: %w", err)
}

entrypoint = e
}

if err := os.Chmod(entrypoint, 0755); err != nil {
return err
}

manifest, err := extensions.ExtractManifest(entrypoint)
if err != nil {
return fmt.Errorf("error loading extension: %w", err)
}

rootCmd, err := NewCmdCustom(filepath.Base(entrypoint), extensions.Extension{
Manifest: manifest,
Entrypoint: entrypoint,
}, extensions.Config{
Origin: entrypoint,
return cfg, items, nil
})
if err != nil {
return fmt.Errorf("error loading extension: %w", err)
}
return tui.Draw(rootList)

rootCmd.Use = "extension"
rootCmd.SetArgs(args[1:])
return rootCmd.Execute()
}

return rootCmd, nil
Expand Down Expand Up @@ -310,9 +221,9 @@ func buildDoc(command *cobra.Command) (string, error) {
return out.String(), nil
}

func extractListItems(cfg config.Config, extensionMap map[string]extensions.Extension) []types.ListItem {
func onelinerListItems(oneliners []config.Oneliner) []types.ListItem {
var items []types.ListItem
for _, oneliner := range cfg.Oneliners {
for _, oneliner := range oneliners {
item := types.ListItem{
Id: fmt.Sprintf("oneliner - %s", oneliner.Command),
Title: oneliner.Title,
Expand All @@ -338,62 +249,61 @@ func extractListItems(cfg config.Config, extensionMap map[string]extensions.Exte
items = append(items, item)
}

for alias, extensionConfig := range cfg.Extensions {
extension, ok := extensionMap[alias]
if !ok {
continue
}
return items
}

var rootItems []types.RootItem
rootItems = append(rootItems, extension.Root()...)
rootItems = append(rootItems, extensionConfig.Items...)

for _, rootItem := range rootItems {
item := types.ListItem{
Id: fmt.Sprintf("%s - %s", alias, rootItem.Title),
Title: rootItem.Title,
Subtitle: extension.Manifest.Title,
Accessories: []string{"Command"},
Actions: []types.Action{
{
Title: "Run",
Type: types.ActionTypeRun,
Extension: alias,
Command: rootItem.Command,
Params: rootItem.Params,
Exit: true,
},
},
}
func extensionListItems(alias string, extension extensions.Extension, extensionConfig extensions.Config) []types.ListItem {
var items []types.ListItem

if !extensions.IsRemote(extensionConfig.Origin) {
item.Actions = append(item.Actions, types.Action{
Title: "Edit Extension",
Key: "e",
Type: types.ActionTypeEdit,
Target: extension.Entrypoint,
Reload: true,
})
} else {
item.Actions = append(item.Actions, types.Action{
Title: "View Source",
Key: "c",
Type: types.ActionTypeExec,
Command: fmt.Sprintf("sunbeam fetch %s | %s", extensionConfig.Origin, utils.FindPager()),
})
}
var rootItems []types.RootItem
rootItems = append(rootItems, extension.Root()...)
rootItems = append(rootItems, extensionConfig.Items...)

if len(extensionConfig.Preferences) > 0 {
item.Actions = append(item.Actions, types.Action{
Title: "Configure Extension",
Key: "s",
Type: types.ActionTypeConfig,
for _, rootItem := range rootItems {
item := types.ListItem{
Id: fmt.Sprintf("%s - %s", alias, rootItem.Title),
Title: rootItem.Title,
Subtitle: extension.Manifest.Title,
Accessories: []string{"Command"},
Actions: []types.Action{
{
Title: "Run",
Type: types.ActionTypeRun,
Extension: alias,
})
}
Command: rootItem.Command,
Params: rootItem.Params,
Exit: true,
},
},
}

items = append(items, item)
if !extensions.IsRemote(extensionConfig.Origin) {
item.Actions = append(item.Actions, types.Action{
Title: "Edit Extension",
Key: "e",
Type: types.ActionTypeEdit,
Target: extension.Entrypoint,
Reload: true,
})
} else {
item.Actions = append(item.Actions, types.Action{
Title: "View Source",
Key: "c",
Type: types.ActionTypeExec,
Command: fmt.Sprintf("sunbeam fetch %s | %s", extensionConfig.Origin, utils.FindPager()),
})
}

if len(extensionConfig.Preferences) > 0 {
item.Actions = append(item.Actions, types.Action{
Title: "Configure Extension",
Key: "s",
Type: types.ActionTypeConfig,
Extension: alias,
})
}

items = append(items, item)
}

return items
Expand Down
Loading

0 comments on commit 8612580

Please sign in to comment.