Skip to content

Commit 92483c4

Browse files
committed
Updated
1 parent 095cff7 commit 92483c4

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

cmd/server/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
type CLI struct {
2020
ServiceCommands
21+
Service2Commands
2122

2223
PG struct {
2324
pgmanager.DatabaseCommands

cmd/server/service2.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
// Packages
7+
server "github.com/mutablelogic/go-server"
8+
"github.com/mutablelogic/go-server/pkg/parser"
9+
provider "github.com/mutablelogic/go-server/pkg/provider"
10+
)
11+
12+
///////////////////////////////////////////////////////////////////////////////
13+
// TYPES
14+
15+
type Service2Commands struct {
16+
// Run ServiceRunCommand `cmd:"" group:"SERVICE" help:"Run the service"`
17+
Run2 Service2RunCommand `cmd:"" group:"SERVICE" help:"Run the service with plugins"`
18+
}
19+
20+
type Service2RunCommand struct {
21+
Plugins []string `help:"Plugin paths" env:"PLUGIN_PATH"`
22+
Args []string `arg:"" help:"Configuration files"`
23+
}
24+
25+
///////////////////////////////////////////////////////////////////////////////
26+
// PUBLIC METHODS
27+
28+
func (cmd *Service2RunCommand) Run(app server.Cmd) error {
29+
// Create a provider by loading the plugins
30+
provider, err := provider.NewWithPlugins(cmd.Plugins...)
31+
if err != nil {
32+
return err
33+
}
34+
35+
// Create a parser of the config files
36+
parser, err := parser.New(provider.Plugins()...)
37+
if err != nil {
38+
return err
39+
}
40+
41+
// Parse the configuration files
42+
for _, path := range cmd.Args {
43+
if err := parser.Parse(path); err != nil {
44+
return err
45+
}
46+
}
47+
48+
fmt.Println(provider, parser)
49+
50+
return nil
51+
}

etc/testdata/httpserver.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"httpserver": {
3+
"main": {
4+
"port": 8080,
5+
"host": "localhost"
6+
}
7+
}
8+
}

pkg/provider/provider.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ func (provider *provider) WriteConfig(w io.Writer) error {
141141
////////////////////////////////////////////////////////////////////////////////
142142
// PUBLIC METHODS
143143

144+
// Return all the plugins
145+
func (provider *provider) Plugins() []server.Plugin {
146+
var result []server.Plugin
147+
for _, meta := range provider.protos {
148+
result = append(result, meta.New())
149+
}
150+
return result
151+
}
152+
144153
// Return a task from a label
145154
func (provider *provider) Task(ctx context.Context, label string) server.Task {
146155
provider.Debugf(ctx, "Called Task for %q", label)

plugin.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ type Provider interface {
4343
// Write out the plugin configuration to a writer
4444
WriteConfig(io.Writer) error
4545

46+
// Return the plugins registered with the provider
47+
Plugins() []Plugin
48+
4649
// Load a plugin by name and label, and provide a resolver function
4750
Load(string, string, PluginResolverFunc) error
4851

0 commit comments

Comments
 (0)