Chore: Fix issues reported by staticcheck; enable stylecheck linter (#28866)
* Chore: Fix issues reported by staticcheck Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> * Apply suggestions from code review Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>
This commit is contained in:
co-authored by
Emil Tullstedt
parent
135b83e17f
commit
676d393ec9
@@ -6,7 +6,6 @@ import (
|
||||
datasourceV1 "github.com/grafana/grafana-plugin-model/go/datasource"
|
||||
rendererV1 "github.com/grafana/grafana-plugin-model/go/renderer"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend/grpcplugin"
|
||||
sdkgrpcplugin "github.com/grafana/grafana-plugin-sdk-go/backend/grpcplugin"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin/pluginextensionv2"
|
||||
@@ -28,8 +27,8 @@ var handshake = goplugin.HandshakeConfig{
|
||||
ProtocolVersion: DefaultProtocolVersion,
|
||||
|
||||
// The magic cookie values should NEVER be changed.
|
||||
MagicCookieKey: sdkgrpcplugin.MagicCookieKey,
|
||||
MagicCookieValue: sdkgrpcplugin.MagicCookieValue,
|
||||
MagicCookieKey: grpcplugin.MagicCookieKey,
|
||||
MagicCookieValue: grpcplugin.MagicCookieValue,
|
||||
}
|
||||
|
||||
func newClientConfig(executablePath string, env []string, logger log.Logger, versionedPlugins map[int]goplugin.PluginSet) *goplugin.ClientConfig {
|
||||
@@ -69,10 +68,10 @@ type PluginDescriptor struct {
|
||||
// getV2PluginSet returns list of plugins supported on v2.
|
||||
func getV2PluginSet() goplugin.PluginSet {
|
||||
return goplugin.PluginSet{
|
||||
"diagnostics": &sdkgrpcplugin.DiagnosticsGRPCPlugin{},
|
||||
"resource": &sdkgrpcplugin.ResourceGRPCPlugin{},
|
||||
"data": &sdkgrpcplugin.DataGRPCPlugin{},
|
||||
"transform": &sdkgrpcplugin.TransformGRPCPlugin{},
|
||||
"diagnostics": &grpcplugin.DiagnosticsGRPCPlugin{},
|
||||
"resource": &grpcplugin.ResourceGRPCPlugin{},
|
||||
"data": &grpcplugin.DataGRPCPlugin{},
|
||||
"transform": &grpcplugin.TransformGRPCPlugin{},
|
||||
"renderer": &pluginextensionv2.RendererGRPCPlugin{},
|
||||
}
|
||||
}
|
||||
@@ -87,7 +86,7 @@ func NewBackendPlugin(pluginID, executablePath string, startFns PluginStartFuncs
|
||||
DefaultProtocolVersion: {
|
||||
pluginID: &datasourceV1.DatasourcePluginImpl{},
|
||||
},
|
||||
sdkgrpcplugin.ProtocolVersion: getV2PluginSet(),
|
||||
grpcplugin.ProtocolVersion: getV2PluginSet(),
|
||||
},
|
||||
startFns: startFns,
|
||||
})
|
||||
@@ -103,7 +102,7 @@ func NewRendererPlugin(pluginID, executablePath string, startFns PluginStartFunc
|
||||
DefaultProtocolVersion: {
|
||||
pluginID: &rendererV1.RendererPluginImpl{},
|
||||
},
|
||||
sdkgrpcplugin.ProtocolVersion: getV2PluginSet(),
|
||||
grpcplugin.ProtocolVersion: getV2PluginSet(),
|
||||
},
|
||||
startFns: startFns,
|
||||
})
|
||||
|
||||
@@ -23,11 +23,11 @@ import (
|
||||
|
||||
var (
|
||||
// ErrPluginNotRegistered error returned when plugin not registered.
|
||||
ErrPluginNotRegistered = errors.New("Plugin not registered")
|
||||
ErrPluginNotRegistered = errors.New("plugin not registered")
|
||||
// ErrHealthCheckFailed error returned when health check failed.
|
||||
ErrHealthCheckFailed = errors.New("Health check failed")
|
||||
ErrHealthCheckFailed = errors.New("health check failed")
|
||||
// ErrPluginUnavailable error returned when plugin is unavailable.
|
||||
ErrPluginUnavailable = errors.New("Plugin unavailable")
|
||||
ErrPluginUnavailable = errors.New("plugin unavailable")
|
||||
// ErrMethodNotImplemented error returned when plugin method not implemented.
|
||||
ErrMethodNotImplemented = errors.New("method not implemented")
|
||||
)
|
||||
@@ -81,7 +81,7 @@ func (m *manager) Register(pluginID string, factory PluginFactoryFunc) error {
|
||||
defer m.pluginsMu.Unlock()
|
||||
|
||||
if _, exists := m.plugins[pluginID]; exists {
|
||||
return errors.New("Backend plugin already registered")
|
||||
return fmt.Errorf("backend plugin %s already registered", pluginID)
|
||||
}
|
||||
|
||||
pluginSettings := pluginSettings{}
|
||||
@@ -141,7 +141,7 @@ func (m *manager) StartPlugin(ctx context.Context, pluginID string) error {
|
||||
}
|
||||
|
||||
if p.IsManaged() {
|
||||
return errors.New("Backend plugin is managed and cannot be manually started")
|
||||
return errors.New("backend plugin is managed and cannot be manually started")
|
||||
}
|
||||
|
||||
return startPluginAndRestartKilledProcesses(ctx, p)
|
||||
@@ -241,7 +241,7 @@ func (m *manager) callResourceInternal(w http.ResponseWriter, req *http.Request,
|
||||
|
||||
body, err := ioutil.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
return errors.New("Failed to read request body")
|
||||
return fmt.Errorf("failed to read request body: %w", err)
|
||||
}
|
||||
|
||||
crReq := &backend.CallResourceRequest{
|
||||
@@ -315,13 +315,13 @@ func flushStream(plugin Plugin, stream CallResourceClientResponseStream, w http.
|
||||
resp, err := stream.Recv()
|
||||
if err == io.EOF {
|
||||
if processedStreams == 0 {
|
||||
return errors.New("Received empty resource response")
|
||||
return errors.New("received empty resource response")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
if processedStreams == 0 {
|
||||
return errutil.Wrap("Failed to receive response from resource call", err)
|
||||
return errutil.Wrap("failed to receive response from resource call", err)
|
||||
}
|
||||
|
||||
plugin.Logger().Error("Failed to receive response from resource call", "error", err)
|
||||
|
||||
@@ -23,7 +23,7 @@ type callResourceResponseStream struct {
|
||||
|
||||
func (s *callResourceResponseStream) Send(res *backend.CallResourceResponse) error {
|
||||
if s.closed {
|
||||
return errors.New("Cannot send to a closed stream")
|
||||
return errors.New("cannot send to a closed stream")
|
||||
}
|
||||
|
||||
select {
|
||||
@@ -48,7 +48,7 @@ func (s *callResourceResponseStream) Recv() (*backend.CallResourceResponse, erro
|
||||
|
||||
func (s *callResourceResponseStream) Close() error {
|
||||
if s.closed {
|
||||
return errors.New("Cannot close a closed stream")
|
||||
return errors.New("cannot close a closed stream")
|
||||
}
|
||||
|
||||
close(s.stream)
|
||||
|
||||
@@ -113,8 +113,8 @@ type DashTemplateEvaluator struct {
|
||||
result *simplejson.Json
|
||||
}
|
||||
|
||||
func (this *DashTemplateEvaluator) findInput(varName string, varType string) *ImportDashboardInput {
|
||||
for _, input := range this.inputs {
|
||||
func (e *DashTemplateEvaluator) findInput(varName string, varType string) *ImportDashboardInput {
|
||||
for _, input := range e.inputs {
|
||||
if varType == input.Type && (input.Name == varName || input.Name == "*") {
|
||||
return &input
|
||||
}
|
||||
@@ -123,34 +123,34 @@ func (this *DashTemplateEvaluator) findInput(varName string, varType string) *Im
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *DashTemplateEvaluator) Eval() (*simplejson.Json, error) {
|
||||
this.result = simplejson.New()
|
||||
this.variables = make(map[string]string)
|
||||
func (e *DashTemplateEvaluator) Eval() (*simplejson.Json, error) {
|
||||
e.result = simplejson.New()
|
||||
e.variables = make(map[string]string)
|
||||
|
||||
// check that we have all inputs we need
|
||||
for _, inputDef := range this.template.Get("__inputs").MustArray() {
|
||||
for _, inputDef := range e.template.Get("__inputs").MustArray() {
|
||||
inputDefJson := simplejson.NewFromAny(inputDef)
|
||||
inputName := inputDefJson.Get("name").MustString()
|
||||
inputType := inputDefJson.Get("type").MustString()
|
||||
input := this.findInput(inputName, inputType)
|
||||
input := e.findInput(inputName, inputType)
|
||||
|
||||
if input == nil {
|
||||
return nil, &DashboardInputMissingError{VariableName: inputName}
|
||||
}
|
||||
|
||||
this.variables["${"+inputName+"}"] = input.Value
|
||||
e.variables["${"+inputName+"}"] = input.Value
|
||||
}
|
||||
|
||||
return simplejson.NewFromAny(this.evalObject(this.template)), nil
|
||||
return simplejson.NewFromAny(e.evalObject(e.template)), nil
|
||||
}
|
||||
|
||||
func (this *DashTemplateEvaluator) evalValue(source *simplejson.Json) interface{} {
|
||||
func (e *DashTemplateEvaluator) evalValue(source *simplejson.Json) interface{} {
|
||||
sourceValue := source.Interface()
|
||||
|
||||
switch v := sourceValue.(type) {
|
||||
case string:
|
||||
interpolated := varRegex.ReplaceAllStringFunc(v, func(match string) string {
|
||||
replacement, exists := this.variables[match]
|
||||
replacement, exists := e.variables[match]
|
||||
if exists {
|
||||
return replacement
|
||||
}
|
||||
@@ -162,11 +162,11 @@ func (this *DashTemplateEvaluator) evalValue(source *simplejson.Json) interface{
|
||||
case json.Number:
|
||||
return v
|
||||
case map[string]interface{}:
|
||||
return this.evalObject(source)
|
||||
return e.evalObject(source)
|
||||
case []interface{}:
|
||||
array := make([]interface{}, 0)
|
||||
for _, item := range v {
|
||||
array = append(array, this.evalValue(simplejson.NewFromAny(item)))
|
||||
array = append(array, e.evalValue(simplejson.NewFromAny(item)))
|
||||
}
|
||||
return array
|
||||
}
|
||||
@@ -174,14 +174,14 @@ func (this *DashTemplateEvaluator) evalValue(source *simplejson.Json) interface{
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *DashTemplateEvaluator) evalObject(source *simplejson.Json) interface{} {
|
||||
func (e *DashTemplateEvaluator) evalObject(source *simplejson.Json) interface{} {
|
||||
result := make(map[string]interface{})
|
||||
|
||||
for key, value := range source.MustMap() {
|
||||
if key == "__inputs" {
|
||||
continue
|
||||
}
|
||||
result[key] = this.evalValue(simplejson.NewFromAny(value))
|
||||
result[key] = e.evalValue(simplejson.NewFromAny(value))
|
||||
}
|
||||
|
||||
return result
|
||||
|
||||
@@ -165,6 +165,6 @@ func (tw *DatasourcePluginWrapper) mapRowValue(rv *datasource.RowValue) (interfa
|
||||
case datasource.RowValue_TYPE_BYTES:
|
||||
return rv.BytesValue, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("Unsupported row value %v from plugin", rv.Kind)
|
||||
return nil, fmt.Errorf("unsupported row value %v from plugin", rv.Kind)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,7 +302,7 @@ func (pm *PluginManager) GetDatasource(pluginID string) (*DataSourcePlugin, bool
|
||||
return ds, exist
|
||||
}
|
||||
|
||||
func (scanner *PluginScanner) walker(currentPath string, f os.FileInfo, err error) error {
|
||||
func (s *PluginScanner) walker(currentPath string, f os.FileInfo, err error) error {
|
||||
// We scan all the subfolders for plugin.json (with some exceptions) so that we also load embedded plugins, for
|
||||
// example https://github.com/raintank/worldping-app/tree/master/dist/grafana-worldmap-panel worldmap panel plugin
|
||||
// is embedded in worldping app.
|
||||
@@ -322,9 +322,9 @@ func (scanner *PluginScanner) walker(currentPath string, f os.FileInfo, err erro
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := scanner.loadPlugin(currentPath); err != nil {
|
||||
scanner.log.Error("Failed to load plugin", "error", err, "pluginPath", filepath.Dir(currentPath))
|
||||
scanner.errors = append(scanner.errors, err)
|
||||
if err := s.loadPlugin(currentPath); err != nil {
|
||||
s.log.Error("Failed to load plugin", "error", err, "pluginPath", filepath.Dir(currentPath))
|
||||
s.errors = append(s.errors, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -367,7 +367,7 @@ func (s *PluginScanner) loadPlugin(pluginJSONFilePath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (scanner *PluginScanner) IsBackendOnlyPlugin(pluginType string) bool {
|
||||
func (*PluginScanner) IsBackendOnlyPlugin(pluginType string) bool {
|
||||
return pluginType == "renderer" || pluginType == "transform"
|
||||
}
|
||||
|
||||
|
||||
@@ -149,7 +149,7 @@ func (s *transformCallback) QueryData(ctx context.Context, req *pluginv2.QueryDa
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(getDsInfo); err != nil {
|
||||
return nil, fmt.Errorf("Could not find datasource %v", err)
|
||||
return nil, fmt.Errorf("could not find datasource: %w", err)
|
||||
}
|
||||
|
||||
// Convert plugin-model (datasource) queries to tsdb queries
|
||||
|
||||
Reference in New Issue
Block a user