Skip to content

Commit

Permalink
Merge pull request #2058 from rsteube/add-pacman-conf
Browse files Browse the repository at this point in the history
added pacman-conf
  • Loading branch information
rsteube authored Dec 6, 2023
2 parents 8007f3b + 5c71122 commit d3a25cb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 24 deletions.
33 changes: 33 additions & 0 deletions completers/pacman-conf_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/pacman"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "pacman-conf",
Short: "query pacman's configuration file",
Long: "https://man.archlinux.org/man/pacman-conf.8.en",
Run: func(cmd *cobra.Command, args []string) {},
}

func Execute() error {
return rootCmd.Execute()
}
func init() {
carapace.Gen(rootCmd).Standalone()

rootCmd.Flags().StringP("config", "c", "", "set an alternate configuration file")
rootCmd.Flags().BoolP("help", "h", false, "display this help information")
rootCmd.Flags().StringP("repo", "r", "", "query options for a specific repo")
rootCmd.Flags().BoolP("repo-list", "l", false, "list configured repositories")
rootCmd.Flags().BoolP("verbose", "v", false, "always show directive names")
rootCmd.Flags().BoolP("version", "V", false, "display version information")

carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
"config": carapace.ActionFiles(),
"repo": pacman.ActionRepositories(),
})
}
7 changes: 7 additions & 0 deletions completers/pacman-conf_completer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/rsteube/carapace-bin/completers/pacman-conf_completer/cmd"

func main() {
cmd.Execute()
}
24 changes: 0 additions & 24 deletions pkg/actions/tools/pacman/pacman.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,8 @@ import (

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/style"
"gopkg.in/ini.v1"
)

// ActionRepositories completes package repositories
//
// extra
// multilib
func ActionRepositories() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
loadOptions := ini.LoadOptions{}
loadOptions.AllowBooleanKeys = true
loadOptions.UnparseableSections = []string{"options"}
if cfg, err := ini.LoadSources(loadOptions, "/etc/pacman.conf"); err != nil {
return carapace.ActionMessage(err.Error())
} else {
repos := make([]string, 0)
for _, section := range cfg.SectionStrings() {
if section != "DEFAULT" && section != "options" {
repos = append(repos, section)
}
}
return carapace.ActionValues(repos...)
}
})
}

// ActionPackages completes installed packages
//
// gnome-common
Expand Down
18 changes: 18 additions & 0 deletions pkg/actions/tools/pacman/repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package pacman

import (
"strings"

"github.com/rsteube/carapace"
)

// ActionRepositories completes package repositories
//
// extra
// multilib
func ActionRepositories() carapace.Action {
return carapace.ActionExecCommand("pacman-conf", "--repo-list")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
return carapace.ActionValues(lines[:len(lines)-1]...)
})
}

0 comments on commit d3a25cb

Please sign in to comment.