Progress on plugin loading

This commit is contained in:
Sasha Koshka 2024-12-10 14:03:02 -05:00
parent df7f3bdadc
commit c2ccaff8ab
2 changed files with 44 additions and 20 deletions

View File

@ -41,6 +41,9 @@ func main () {
flagDirectories := cli.NewFlag (
'd', "directories",
"Serve the contents of directories.")
flagUnsafePlugins := cli.NewFlag (
0, "unsafe-plugins",
"Load plugins not owned by the root user.")
cmd := cli.New (
"Run an HTTP server that automaticaly executes STEP files.",
flagPidFile,
@ -48,10 +51,24 @@ func main () {
flagHTTPErrorDocument,
flagHTTPDirectoryDocument,
flagDirectories,
flagUnsafePlugins,
cli.NewHelp())
cmd.Syntax = "[OPTIONS]... [DIRECTORY]"
cmd.ParseOrExit(os.Args)
// get env variables
pluginPath := filepath.SplitList(os.Getenv("STEP_PLUGIN_PATH"))
if len(pluginPath) == 0 {
pluginPath = []string {
"/usr/lib/step/plugins",
"/usr/local/lib/step/plugins",
}
}
for index, pat := range pluginPath {
pluginPath[index], _ = filepath.Abs(pat)
}
// log header for telling apart separate program runs
log.Println(`==========| STEP |===========`)
log.Println(`Scriptable Template Processor`)
log.Println(`... initializing`)
@ -115,21 +132,21 @@ func main () {
err := environment.Init(context.Background())
if err != nil { log.Fatal(err) }
// TODO: load plugins
pluginPath := os.Getenv("STEP_PLUGIN_PATH")
if pluginPath == "" {
pluginPath = "/usr/lib/step/plugins:/usr/local/lib/step/plugins"
}
for _, pat := range filepath.SplitList(pluginPath) {
// load plugins
for _, pat := range pluginPath {
entries, err := os.ReadDir(pat)
if err != nil { continue }
for _, entry := range entries {
pluginPath := filepath.Join(pat, entry.Name())
_, err := environment.LoadProviderPlugin(pat)
ext := filepath.Ext(pluginPath)
if ext != ".so" { continue }
if flagUnsafePlugins.Value == "true" {
_, err = environment.LoadProviderPluginUnsafe(pluginPath)
} else {
_, err = environment.LoadProviderPlugin(pluginPath)
}
if err != nil {
log.Printf (
"!!! could not load plugin %s: %v",
pluginPath, err)
log.Println("!!!", err)
}
}
}
@ -197,6 +214,18 @@ func (this *httpServerRoutine) Run (ctx context.Context) error {
func logProviders (providers []step.Provider) {
output := "providers: "
x := utf8.RuneCountInString(output)
first := true
line := func () {
if output == "" { return }
if first {
first = false
log.Println("(i)", output)
} else {
log.Println(" ", output)
}
output = ""
x = 0
}
for index, provider := range providers {
packag := provider.Package()
if index != len(providers) - 1 {
@ -204,16 +233,11 @@ func logProviders (providers []step.Provider) {
x += 2
}
packageLen := utf8.RuneCountInString(packag)
if x + packageLen >= 60 && output != "" {
log.Println("(i)", output)
output = ""
x = 2
continue
if x + packageLen >= 60 {
line()
}
output += packag
x += packageLen
}
if output != "" {
log.Println("(i)", output)
}
line()
}

View File

@ -129,7 +129,7 @@ func (this *Environment) LoadProviderPluginUnsafe (name string) (Provider, error
func (this *Environment) loadProviderPlugin (name string, checkRoot bool) (Provider, error) {
plugin, err := this.loadPlugin(name, checkRoot)
if err != nil { return nil, err }
providerSymbol, err := plugin.Lookup("Provider")
providerSymbol, err := plugin.Lookup("NewProvider")
if err != nil { return nil, err }
providerFactory, ok := providerSymbol.(func () Provider)
if !ok { return nil, ErrPluginBadSymbol }