Skip to content

Commit

Permalink
spec: global folder
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Feb 13, 2024
1 parent 0e5ddb7 commit 9b0c822
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 16 deletions.
11 changes: 11 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ aurs:
- carapace
conflicts:
- carapace
package: |-
# bin
install -Dm755 "./carapace" "${pkgdir}/usr/bin/carapace"
# license
install -Dm644 "./LICENSE.md" "${pkgdir}/usr/share/licenses/mybin/LICENSE"

# folders
mkdir -p "${pkgdir}/etc/xdg/carapace/specs
mkdir -p "${pkgdir}/etc/xdg/carapace/variables


nfpms:
-
Expand Down
80 changes: 64 additions & 16 deletions cmd/carapace/cmd/completers/completers.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package completers

import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/env"
"github.com/rsteube/carapace/pkg/xdg"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -42,7 +44,7 @@ func Names() []string {
}
}
//if specNames, err := Specs(); err == nil {
if specNames, _ := Specs(); true { // TODO use and handle err
if specNames := Specs(); true { // TODO use and handle err
for _, name := range specNames {
unique[name] = true
}
Expand All @@ -57,11 +59,37 @@ func Names() []string {
}

func SpecPath(name string) (string, error) {
configDir, err := xdg.UserConfigDir()
if err != nil {
return "", err
userConfigDir, err := xdg.UserConfigDir()
var path string
if err == nil {
path, err = specPath(userConfigDir, name)
if err == nil {
return path, nil
}
}

if err != nil { // TODO have another look at the error handling here
if !os.IsNotExist(err) {
carapace.LOG.Println(err.Error())
}

configDirs, err := xdg.ConfigDirs()
if err != nil {
return "", err
}
for _, dir := range configDirs {
path, err = specPath(dir, name)
if err != nil {
carapace.LOG.Println(err.Error())
continue
}
return path, err
}
}
return "", errors.New("no spec found")
}

func specPath(configDir string, name string) (string, error) {
path := fmt.Sprintf("%v/carapace/specs/%v.yaml", configDir, name)
if _, err := os.Stat(path); err != nil {
return "", err
Expand Down Expand Up @@ -93,22 +121,42 @@ func OverlayPath(name string) (string, error) {
return abs, nil
}

func Specs() (specs []string, dir string) {
func Specs() []string {
unique := make(map[string]bool)

if userConfigDir, err := xdg.UserConfigDir(); err == nil {
for _, spec := range specs(userConfigDir) {
unique[spec] = true
}
}

if configDirs, err := xdg.ConfigDirs(); err == nil {
for _, dir := range configDirs {
for _, spec := range specs(dir) {
unique[spec] = true
}
}
}

result := make([]string, 0)
for spec := range unique {
result = append(result, spec)
}
return result
}

func specs(configDir string) []string {
r := regexp.MustCompile(`^[0-9a-zA-Z_\-.]+\.yaml$`) // sanity check
specs = make([]string, 0)
if configDir, err := xdg.UserConfigDir(); err == nil {
dir = configDir + "/carapace/specs"
if entries, err := os.ReadDir(dir); err == nil {
for _, entry := range entries {
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".yaml") && r.MatchString(entry.Name()) {
specs = append(specs, strings.TrimSuffix(entry.Name(), ".yaml"))
}
specs := make([]string, 0)
dir := configDir + "/carapace/specs"
if entries, err := os.ReadDir(dir); err == nil {
for _, entry := range entries {
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".yaml") && r.MatchString(entry.Name()) {
specs = append(specs, strings.TrimSuffix(entry.Name(), ".yaml"))
}
} else if os.IsNotExist(err) {
os.MkdirAll(dir, os.ModePerm)
}
}
return
return specs
}

func Description(name string) string {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ require (
)

replace github.com/spf13/pflag => github.com/rsteube/carapace-pflag v0.2.0

replace github.com/rsteube/carapace => ../carapace/

0 comments on commit 9b0c822

Please sign in to comment.