Move `parseScope` to pkg (#92)
diff --git a/Makefile b/Makefile
index cd5b01a..04eecd5 100644
--- a/Makefile
+++ b/Makefile
@@ -174,10 +174,10 @@
.PHONY: test-commands
test-commands:
- @if ! docker run --name oap -p 12800:12800 -p 11800:11800 -d -e SW_HEALTH_CHECKER=default -e SW_TELEMETRY=prometheus apache/skywalking-oap-server:latest > /dev/null 2>&1;then \
+ @if ! docker run --name oap -p 12800:12800 -p 11800:11800 -d -e SW_HEALTH_CHECKER=default -e SW_TELEMETRY=prometheus apache/skywalking-oap-server:8.4.0-es7 > /dev/null 2>&1;then \
docker container stop oap; \
docker container prune -f; \
- docker run --name oap -p 12800:12800 -p 11800:11800 -d -e SW_HEALTH_CHECKER=default -e SW_TELEMETRY=prometheus apache/skywalking-oap-server:latest; \
+ docker run --name oap -p 12800:12800 -p 11800:11800 -d -e SW_HEALTH_CHECKER=default -e SW_TELEMETRY=prometheus apache/skywalking-oap-server:8.4.0-es7; \
fi
./scripts/test_commands.sh
@docker container stop oap
diff --git a/internal/commands/event/report.go b/internal/commands/event/report.go
index d5c1807..d0d2652 100644
--- a/internal/commands/event/report.go
+++ b/internal/commands/event/report.go
@@ -18,16 +18,15 @@
package event
import (
- "github.com/apache/skywalking-cli/internal/commands/interceptor"
+ event "skywalking/network/event/v3"
+
"github.com/apache/skywalking-cli/internal/logger"
"github.com/apache/skywalking-cli/internal/model"
+ pkgevent "github.com/apache/skywalking-cli/pkg/commands/event"
"github.com/apache/skywalking-cli/pkg/display"
"github.com/apache/skywalking-cli/pkg/display/displayable"
- "github.com/apache/skywalking-cli/pkg/grpc"
"github.com/urfave/cli"
-
- event "skywalking/network/event/v3"
)
var reportCommand = cli.Command{
@@ -79,32 +78,11 @@
},
},
Action: func(ctx *cli.Context) error {
- parameters, err := interceptor.ParseParameters(ctx.Args())
+ reply, err := pkgevent.Report(ctx)
if err != nil {
return err
}
- event := event.Event{
- Uuid: ctx.String("uuid"),
- Source: &event.Source{
- Service: ctx.String("service"),
- ServiceInstance: ctx.String("instance"),
- Endpoint: ctx.String("endpoint"),
- },
- Name: ctx.String("name"),
- Type: ctx.Generic("type").(*model.EventTypeEnumValue).Selected,
- Message: ctx.String("message"),
- Parameters: parameters,
- StartTime: ctx.Int64("startTime"),
- EndTime: ctx.Int64("endTime"),
- }
-
- reply, err := grpc.ReportEvent(ctx.GlobalString("grpcAddr"), &event)
- if err != nil {
- logger.Log.Fatalln(err)
- return err
- }
-
logger.Log.Println("Report the event successfully, whose uuid is ", ctx.String("uuid"))
return display.Display(ctx, &displayable.Displayable{Data: reply})
},
diff --git a/internal/commands/interceptor/entity.go b/internal/commands/interceptor/entity.go
index 552309a..efaf6dd 100644
--- a/internal/commands/interceptor/entity.go
+++ b/internal/commands/interceptor/entity.go
@@ -21,6 +21,7 @@
"github.com/urfave/cli"
"github.com/apache/skywalking-cli/api"
+ "github.com/apache/skywalking-cli/pkg/graphql/utils"
)
func ParseEntity(ctx *cli.Context) *api.Entity {
@@ -44,28 +45,7 @@
DestServiceInstanceName: &destInstance,
DestEndpointName: &destEndpoint,
}
- entity.Scope = parseScope(entity)
+ entity.Scope = utils.ParseScope(entity)
return entity
}
-
-// parseScope defines the scope based on the input parameters.
-func parseScope(entity *api.Entity) api.Scope {
- scope := api.ScopeAll
-
- if *entity.DestEndpointName != "" {
- scope = api.ScopeEndpointRelation
- } else if *entity.DestServiceInstanceName != "" {
- scope = api.ScopeServiceInstanceRelation
- } else if *entity.DestServiceName != "" {
- scope = api.ScopeServiceRelation
- } else if *entity.EndpointName != "" {
- scope = api.ScopeEndpoint
- } else if *entity.ServiceInstanceName != "" {
- scope = api.ScopeServiceInstance
- } else if *entity.ServiceName != "" {
- scope = api.ScopeService
- }
-
- return scope
-}
diff --git a/pkg/commands/event/report.go b/pkg/commands/event/report.go
new file mode 100644
index 0000000..417e6fc
--- /dev/null
+++ b/pkg/commands/event/report.go
@@ -0,0 +1,60 @@
+// Licensed to Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright
+// ownership. Apache Software Foundation (ASF) licenses this file to you under
+// the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package event
+
+import (
+ common "skywalking/network/common/v3"
+ event "skywalking/network/event/v3"
+
+ "github.com/apache/skywalking-cli/internal/commands/interceptor"
+ "github.com/apache/skywalking-cli/internal/logger"
+ "github.com/apache/skywalking-cli/internal/model"
+ "github.com/apache/skywalking-cli/pkg/grpc"
+
+ "github.com/urfave/cli"
+)
+
+func Report(ctx *cli.Context) (*common.Commands, error) {
+ parameters, err := interceptor.ParseParameters(ctx.Args())
+ if err != nil {
+ return nil, err
+ }
+
+ e := event.Event{
+ Uuid: ctx.String("uuid"),
+ Source: &event.Source{
+ Service: ctx.String("service"),
+ ServiceInstance: ctx.String("instance"),
+ Endpoint: ctx.String("endpoint"),
+ },
+ Name: ctx.String("name"),
+ Type: ctx.Generic("type").(*model.EventTypeEnumValue).Selected,
+ Message: ctx.String("message"),
+ Parameters: parameters,
+ StartTime: ctx.Int64("startTime"),
+ EndTime: ctx.Int64("endTime"),
+ }
+
+ reply, err := grpc.ReportEvent(ctx.GlobalString("grpcAddr"), &e)
+ if err != nil {
+ logger.Log.Fatalln(err)
+ return nil, err
+ }
+
+ return reply, nil
+}
diff --git a/pkg/graphql/utils/parser.go b/pkg/graphql/utils/parser.go
new file mode 100644
index 0000000..d54fe18
--- /dev/null
+++ b/pkg/graphql/utils/parser.go
@@ -0,0 +1,41 @@
+// Licensed to Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright
+// ownership. Apache Software Foundation (ASF) licenses this file to you under
+// the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package utils
+
+import "github.com/apache/skywalking-cli/api"
+
+// ParseScope defines the scope based on the input parameters.
+func ParseScope(entity *api.Entity) api.Scope {
+ scope := api.ScopeAll
+
+ if *entity.DestEndpointName != "" {
+ scope = api.ScopeEndpointRelation
+ } else if *entity.DestServiceInstanceName != "" {
+ scope = api.ScopeServiceInstanceRelation
+ } else if *entity.DestServiceName != "" {
+ scope = api.ScopeServiceRelation
+ } else if *entity.EndpointName != "" {
+ scope = api.ScopeEndpoint
+ } else if *entity.ServiceInstanceName != "" {
+ scope = api.ScopeServiceInstance
+ } else if *entity.ServiceName != "" {
+ scope = api.ScopeService
+ }
+
+ return scope
+}
diff --git a/internal/commands/interceptor/entity_test.go b/pkg/graphql/utils/parser_test.go
similarity index 94%
rename from internal/commands/interceptor/entity_test.go
rename to pkg/graphql/utils/parser_test.go
index 2f66f52..361eb04 100644
--- a/internal/commands/interceptor/entity_test.go
+++ b/pkg/graphql/utils/parser_test.go
@@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.
-package interceptor
+package utils
import (
"testing"
@@ -23,7 +23,7 @@
"github.com/apache/skywalking-cli/api"
)
-func Test_parseScope(t *testing.T) {
+func TestParseScope(t *testing.T) {
empty := ""
nonEmpty := "test"
tests := []struct {
@@ -116,10 +116,11 @@
want: api.ScopeServiceInstanceRelation,
},
}
+
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- if got := parseScope(tt.args); got != tt.want {
- t.Errorf("parseScope() = %v, want %v", got, tt.want)
+ if got := ParseScope(tt.args); got != tt.want {
+ t.Errorf("ParseScope() = %v, want %v", got, tt.want)
}
})
}