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

List all the registered plugins #4114

Merged
merged 2 commits into from
Sep 1, 2023
Merged
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
3 changes: 3 additions & 0 deletions changelog/unreleased/plugin-list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Enhancement: List all the registered plugins

https://github.com/cs3org/reva/pull/4114
49 changes: 49 additions & 0 deletions cmd/revad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ import (
"io/fs"
"os"
"path"
"reflect"
"regexp"
"strings"
"sync"
"syscall"

gorun "runtime"

"github.com/cs3org/reva"
"github.com/cs3org/reva/cmd/revad/pkg/config"
"github.com/cs3org/reva/cmd/revad/pkg/grace"
Expand All @@ -48,6 +52,7 @@ var (
configFlag = flag.String("c", "/etc/revad/revad.toml", "set configuration file")
pidFlag = flag.String("p", "", "pid file. If empty defaults to a random file in the OS temporary directory")
dirFlag = flag.String("dev-dir", "", "runs any toml file in the specified directory. Intended for development use only")
pluginsFlag = flag.Bool("plugins", false, "list all the plugins and exit")

// Compile time variables initialized with gcc flags.
gitCommit, buildDate, version, goVersion string
Expand All @@ -70,6 +75,7 @@ func Main() {

handleVersionFlag()
handleSignalFlag()
handlePluginsFlag()

confs, err := getConfigs()
if err != nil {
Expand Down Expand Up @@ -107,6 +113,49 @@ func handleVersionFlag() {
}
}

func handlePluginsFlag() {
if !*pluginsFlag {
return
}

// TODO (gdelmont): maybe in future if needed we can filter
// by namespace (for example for getting all the http plugins).
// For now we just list all the plugins.
plugins := reva.GetPlugins("")
grouped := groupByNamespace(plugins)

count := 0
for ns, plugins := range grouped {
fmt.Printf("[%s]\n", ns)
for _, p := range plugins {
fmt.Printf("%s -> %s\n", p.ID.Name(), pkgOfFunction(p.New))
}
count++
if len(grouped) != count {
fmt.Println()
}
}
os.Exit(0)
}

func nameOfFunction(f any) string {
return gorun.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}

func pkgOfFunction(f any) string {
name := nameOfFunction(f)
i := strings.LastIndex(name, ".")
return name[:i]
}

func groupByNamespace(plugins []reva.PluginInfo) map[string][]reva.PluginInfo {
m := make(map[string][]reva.PluginInfo)
for _, p := range plugins {
m[p.ID.Namespace()] = append(m[p.ID.Namespace()], p)
}
return m
}

func getVersionString() string {
msg := "version=%s "
msg += "commit=%s "
Expand Down