/
service.go
executable file
·190 lines (152 loc) · 4.52 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
Copyright (c) 2019 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package service
import (
"context"
"path"
"strings"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
"github.com/kubenext/lissio/pkg/action"
"github.com/kubenext/lissio/pkg/navigation"
"github.com/kubenext/lissio/pkg/plugin"
)
func defaultServerFactory(service plugin.Service) {
plugin.Serve(service)
}
// PluginOption is an option for configuring Plugin.
type PluginOption func(p *Plugin)
// WithPrinter configures the plugin to have a printer.
func WithPrinter(fn HandlerPrinterFunc) PluginOption {
return func(p *Plugin) {
p.pluginHandler.HandlerFuncs.Print = fn
}
}
// WithTabPrinter configures the plugin to have a tab printer.
func WithTabPrinter(fn HandlerTabPrintFunc) PluginOption {
return func(p *Plugin) {
p.pluginHandler.HandlerFuncs.PrintTab = fn
}
}
// WithObjectStatus configures the plugin to supply object status.
func WithObjectStatus(fn HandlerObjectStatusFunc) PluginOption {
return func(p *Plugin) {
p.pluginHandler.HandlerFuncs.ObjectStatus = fn
}
}
// WithActionHandler configures the plugin to handle actions.
func WithActionHandler(fn HandlerActionFunc) PluginOption {
return func(p *Plugin) {
p.pluginHandler.HandlerFuncs.HandleAction = fn
}
}
// WithNavigation configures the plugin to handle navigation and routes.
func WithNavigation(fn HandlerNavigationFunc, routerInit HandlerInitRoutesFunc) PluginOption {
return func(p *Plugin) {
p.pluginHandler.HandlerFuncs.Navigation = fn
p.pluginHandler.HandlerFuncs.InitRoutes = routerInit
}
}
// Plugin is a plugin service helper.
type Plugin struct {
pluginHandler *Handler
serverFactory func(service plugin.Service)
}
// Register registers a plugin with Lissio.
func Register(name, description string, capabilities *plugin.Capabilities, options ...PluginOption) (*Plugin, error) {
router := NewRouter()
p := &Plugin{
pluginHandler: &Handler{
name: name,
description: description,
capabilities: capabilities,
dashboardFactory: NewDashboardClient,
router: router,
},
serverFactory: defaultServerFactory,
}
for _, option := range options {
option(p)
}
if p.pluginHandler.InitRoutes != nil {
p.pluginHandler.InitRoutes(router)
}
if err := p.Validate(); err != nil {
return nil, err
}
return p, nil
}
// Validate validates this helper.
func (p *Plugin) Validate() error {
var list []string
if p.pluginHandler.name == "" {
list = append(list, "requires name")
}
if p.pluginHandler.description == "" {
list = append(list, "requires description")
}
if p.pluginHandler.capabilities == nil {
list = append(list, "requires capabilities")
}
if err := p.pluginHandler.Validate(); err != nil {
list = append(list, err.Error())
}
if len(list) == 0 {
return nil
}
return errors.Errorf("validation errors: %s", strings.Join(list, ", "))
}
// Serve serves a plugin.
func (p *Plugin) Serve() {
p.serverFactory(p.pluginHandler)
}
type baseRequest struct {
ctx context.Context
pluginName string
}
func newBaseRequest(ctx context.Context, pluginName string) baseRequest {
return baseRequest{
ctx: ctx,
pluginName: pluginName,
}
}
func (r *baseRequest) Context() context.Context {
return r.ctx
}
func (r *baseRequest) GeneratePath(pathParts ...string) string {
return path.Join(append([]string{r.pluginName}, pathParts...)...)
}
// PrintRequest is a request for printing.
type PrintRequest struct {
baseRequest
DashboardClient Dashboard
Object runtime.Object
}
// ActionRequest is a request for actions.
type ActionRequest struct {
baseRequest
DashboardClient Dashboard
Payload action.Payload
}
// NavigationRequest is a request for navigation.
type NavigationRequest struct {
baseRequest
DashboardClient Dashboard
}
type HandlerPrinterFunc func(request *PrintRequest) (plugin.PrintResponse, error)
type HandlerTabPrintFunc func(request *PrintRequest) (plugin.TabResponse, error)
type HandlerObjectStatusFunc func(request *PrintRequest) (plugin.ObjectStatusResponse, error)
type HandlerActionFunc func(request *ActionRequest) error
type HandlerNavigationFunc func(request *NavigationRequest) (navigation.Navigation, error)
type HandlerInitRoutesFunc func(router *Router)
// HandlerFuncs are functions for configuring a plugin.
type HandlerFuncs struct {
Print HandlerPrinterFunc
PrintTab HandlerTabPrintFunc
ObjectStatus HandlerObjectStatusFunc
HandleAction HandlerActionFunc
Navigation HandlerNavigationFunc
InitRoutes HandlerInitRoutesFunc
}