Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spec: global folder #2239

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ aurs:
- carapace
conflicts:
- carapace
package: |-
install -Dm755 "./carapace" "${pkgdir}/usr/bin/carapace"
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
Loading