Skip to content

Commit

Permalink
nix: added package actions
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Nov 6, 2022
1 parent c52acd9 commit 8bb74bc
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions pkg/actions/tools/nix/package.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package nix

import (
"fmt"
"os"
"strings"

"github.com/rsteube/carapace"
)

// ActionChannelPackages completes local channels and their packages
//
// nixos.g++
// nixos.gacutil
func ActionChannelPackages() carapace.Action {
return carapace.ActionMultiParts(".", func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
return ActionLocalChannels().Invoke(c).Suffix(".").ToA()

case 1:
return ActionPackages(c.Parts[0])

default:
return carapace.ActionValues()
}
})
}

// ActionPackages completes packages for given channel
//
// g++
// gacutil
func ActionPackages(channel string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if channel == "" {
channel = "nixos"
}

path, err := c.Abs(fmt.Sprintf("~/.nix-defexpr/channels/%v/programs.sqlite", channel))
if err != nil {
return carapace.ActionMessage(err.Error())
}

if _, err := os.Stat(path); err != nil {
return carapace.ActionMessage(err.Error())
}

if c.CallbackValue == "" {
return carapace.ActionMessage("search needs at least 1 character")
}

query := fmt.Sprintf(`SELECT name FROM Programs WHERE name LIKE "%v%%"`, c.CallbackValue)
return carapace.ActionExecCommand("sqlite3", path, query)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
return carapace.ActionValues(lines[:len(lines)-1]...)
})
})
}

0 comments on commit 8bb74bc

Please sign in to comment.