Skip to content

Commit

Permalink
Implement basic plugin loading mechanism.
Browse files Browse the repository at this point in the history
  • Loading branch information
metacosm committed Jan 31, 2019
1 parent 0d6b970 commit d393829
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
47 changes: 47 additions & 0 deletions cmd/odo/odo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@ import (
"github.com/golang/glog"
"github.com/posener/complete"
"github.com/redhat-developer/odo/pkg/config"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/odo/cli"
"github.com/redhat-developer/odo/pkg/odo/cli/version"
"github.com/redhat-developer/odo/pkg/odo/events"
"github.com/redhat-developer/odo/pkg/odo/util"
"github.com/redhat-developer/odo/pkg/odo/util/completion"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"os"
"path/filepath"
"plugin"
"strings"
)

func main() {
loadPlugins()

// create the complete command
root := cli.NewCmdOdo(cli.OdoRecommendedName, cli.OdoRecommendedName)
rootCmp := createCompletion(root)
Expand Down Expand Up @@ -112,3 +119,43 @@ func createCompletion(root *cobra.Command) complete.Command {

return rootCmp
}

func loadPlugins() {
configDir, err := config.GetPluginsDir()
if err != nil {
panic(err)
}

bus := events.GetEventBus()

err = filepath.Walk(configDir, func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".so") {
// Open the plugin library
plug, err := plugin.Open(path)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Look up the exporter Listener variable
candidate, err := plug.Lookup("Listener")
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Assert that Listener is indeed a Listener :)
listener, ok := candidate.(events.Listener)
if !ok {
log.Error("exported Listener variable is not implementing the Listener interface")
os.Exit(1)
}

bus.RegisterToAll(listener)
}
return nil
})
if err != nil {
panic(err)
}
}
29 changes: 29 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,35 @@ type ConfigInfo struct {
Config
}

func GetConfigDir() (string, error) {
currentUser, err := user.Current()
if err != nil {
return "", err
}

configDir := filepath.Join(currentUser.HomeDir, ".odo")

// Check whether directory present or not
_, err = os.Stat(filepath.Dir(configDir))
if os.IsNotExist(err) {
err = os.MkdirAll(filepath.Dir(configDir), 0755)
if err != nil {
return "", err
}
}

return configDir, nil
}

func GetPluginsDir() (string, error) {
configDir, err := GetConfigDir()
if err != nil {
return "", err
}

return filepath.Join(configDir, "plugins"), nil
}

func getDefaultConfigFile() string {
currentUser, err := user.Current()
if err != nil {
Expand Down

0 comments on commit d393829

Please sign in to comment.