Skip to content

Commit

Permalink
add the "unknown command" mentionned in the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
pomdtr committed Sep 28, 2023
1 parent c9db0e7 commit 98c7509
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
12 changes: 12 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "sunbeam",
"type": "go",
"request": "launch",
"console": "integratedTerminal",
"program": "${workspaceFolder}"
},
],
}
5 changes: 3 additions & 2 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ func NewCmdList() *cobra.Command {
}{}

cmd := &cobra.Command{
Use: "list",
Short: "List Kakoune sessions and clients.",
Use: "list",
Aliases: []string{"ls"},
Short: "List Kakoune sessions and clients.",
RunE: func(cmd *cobra.Command, args []string) error {
kakSessions, err := kak.Sessions()
if err != nil {
Expand Down
80 changes: 77 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,48 @@ package cmd

import (
_ "embed"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/spf13/cobra"
)

func NewRootCmd(version string) *cobra.Command {
cmd := &cobra.Command{
Use: "kks [-s <session>] [-c <client>] [<args>]",
Version: version,
Short: "Handy Kakoune companion.",
Use: "kks [-s <session>] [-c <client>] [<args>]",
Version: version,
DisableFlagParsing: true,
Args: cobra.ArbitraryArgs,
Short: "Handy Kakoune companion.",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return cmd.Usage()
}

if args[0] == "--help" || args[0] == "-h" {
return cmd.Help()
}

extensions, err := FindExtensions()
if err != nil {
return err
}

scriptPath, ok := extensions[args[0]]
if !ok {
return cmd.Help()
}

execCmd := exec.Command(scriptPath, args[1:]...)

execCmd.Stdin = os.Stdin
execCmd.Stdout = os.Stdout
execCmd.Stderr = os.Stderr

return execCmd.Run()
},
}

cmd.AddCommand(NewCmdAttach())
Expand All @@ -26,3 +59,44 @@ func NewRootCmd(version string) *cobra.Command {

return cmd
}

func FindExtensions() (map[string]string, error) {
path := os.Getenv("PATH")

extensions := make(map[string]string)
for _, dir := range filepath.SplitList(path) {
if dir == "" {
// Unix shell semantics: path element "" means "."
dir = "."
}

dir, err := filepath.Abs(dir)
if err != nil {
continue
}

entries, err := os.ReadDir(dir)
if err != nil {
continue
}

for _, entry := range entries {
if entry.IsDir() {
continue
}

if !strings.HasPrefix(entry.Name(), "kks-") {
continue
}

alias := strings.TrimPrefix(entry.Name(), "kks-")
if _, ok := extensions[alias]; ok {
continue
}

extensions[alias] = filepath.Join(dir, entry.Name())
}
}

return extensions, nil
}

0 comments on commit 98c7509

Please sign in to comment.