This is an automated email from the ASF dual-hosted git repository.

kezhenxu94 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-cli.git


The following commit(s) were added to refs/heads/master by this push:
     new cf606dd  Move `parseScope` to pkg (#92)
cf606dd is described below

commit cf606ddf05c73e464fd3396303b045652b6f387c
Author: Hoshea Jiang <[email protected]>
AuthorDate: Fri Mar 26 17:38:15 2021 +0800

    Move `parseScope` to pkg (#92)
---
 Makefile                                           |  4 +-
 internal/commands/event/report.go                  | 30 ++---------
 internal/commands/interceptor/entity.go            | 24 +--------
 pkg/commands/event/report.go                       | 60 ++++++++++++++++++++++
 .../entity.go => pkg/graphql/utils/parser.go       | 38 ++------------
 .../graphql/utils/parser_test.go                   |  9 ++--
 6 files changed, 77 insertions(+), 88 deletions(-)

diff --git a/Makefile b/Makefile
index cd5b01a..04eecd5 100644
--- a/Makefile
+++ b/Makefile
@@ -174,10 +174,10 @@ check-codegen: codegen
 
 .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,29 +78,8 @@ var reportCommand = cli.Command{
                },
        },
        Action: func(ctx *cli.Context) error {
-               parameters, err := interceptor.ParseParameters(ctx.Args())
-               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)
+               reply, err := pkgevent.Report(ctx)
                if err != nil {
-                       logger.Log.Fatalln(err)
                        return err
                }
 
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 @@ import (
        "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 @@ func ParseEntity(ctx *cli.Context) *api.Entity {
                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/internal/commands/interceptor/entity.go 
b/pkg/graphql/utils/parser.go
similarity index 57%
copy from internal/commands/interceptor/entity.go
copy to pkg/graphql/utils/parser.go
index 552309a..d54fe18 100644
--- a/internal/commands/interceptor/entity.go
+++ b/pkg/graphql/utils/parser.go
@@ -15,42 +15,12 @@
 // specific language governing permissions and limitations
 // under the License.
 
-package interceptor
+package utils
 
-import (
-       "github.com/urfave/cli"
+import "github.com/apache/skywalking-cli/api"
 
-       "github.com/apache/skywalking-cli/api"
-)
-
-func ParseEntity(ctx *cli.Context) *api.Entity {
-       service := ctx.String("service")
-       normal := ctx.BoolT("isNormal")
-       instance := ctx.String("instance")
-       endpoint := ctx.String("endpoint")
-
-       destService := ctx.String("destService")
-       destNormal := ctx.BoolT("isDestNormal")
-       destInstance := ctx.String("destServiceInstance")
-       destEndpoint := ctx.String("destEndpoint")
-
-       entity := &api.Entity{
-               ServiceName:             &service,
-               Normal:                  &normal,
-               ServiceInstanceName:     &instance,
-               EndpointName:            &endpoint,
-               DestServiceName:         &destService,
-               DestNormal:              &destNormal,
-               DestServiceInstanceName: &destInstance,
-               DestEndpointName:        &destEndpoint,
-       }
-       entity.Scope = parseScope(entity)
-
-       return entity
-}
-
-// parseScope defines the scope based on the input parameters.
-func parseScope(entity *api.Entity) api.Scope {
+// ParseScope defines the scope based on the input parameters.
+func ParseScope(entity *api.Entity) api.Scope {
        scope := api.ScopeAll
 
        if *entity.DestEndpointName != "" {
diff --git a/internal/commands/interceptor/entity_test.go 
b/pkg/graphql/utils/parser_test.go
similarity index 95%
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 @@ import (
        "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 @@ func Test_parseScope(t *testing.T) {
                        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)
                        }
                })
        }

Reply via email to