提交 045abf77 编写于 作者: Z zhang-wei 提交者: kezhenxu94

[Feature] Support instance search command (#15)

上级 b5f72ee5
...@@ -65,6 +65,7 @@ This section covers all the available commands in SkyWalking CLI and their usage ...@@ -65,6 +65,7 @@ This section covers all the available commands in SkyWalking CLI and their usage
- [`list`, `ls`](#service-list---startstart-time---endend-time) - [`list`, `ls`](#service-list---startstart-time---endend-time)
- [`instance`](#instance-second-level-command) (second level command) - [`instance`](#instance-second-level-command) (second level command)
- [`list`, `ls`](#instance-list---service-idservice-id---service-nameservice-name---startstart-time---endend-time) - [`list`, `ls`](#instance-list---service-idservice-id---service-nameservice-name---startstart-time---endend-time)
- [`search`](#instance-search---regexinstance-name-regex---service-idservice-id---service-nameservice-name---startstart-time---endend-time)
### `swctl` top-level command ### `swctl` top-level command
`swctl` is the top-level command, which has some options that will take effects globally. `swctl` is the top-level command, which has some options that will take effects globally.
...@@ -102,6 +103,17 @@ and it also has some options and third-level commands. ...@@ -102,6 +103,17 @@ and it also has some options and third-level commands.
| `--start` | See [Common options](#common-options) | See [Common options](#common-options) | | `--start` | See [Common options](#common-options) | See [Common options](#common-options) |
| `--end` | See [Common options](#common-options) | See [Common options](#common-options) | | `--end` | See [Common options](#common-options) | See [Common options](#common-options) |
#### `instance search [--regex=<instance name regex>] [--service-id=<service id>] [--service-name=<service name>] [--start=<start time>] [--end=<end time>]`
`instance search` filter the instance in the time range of \[`start`, `end`\] and given --regex --service-id or --service-name.
| option | description | default |
| :--- | :--- | :--- |
| `--regex` | Query regex of instance name| |
| `--service-id` | Query service id (priority over --service-name)| |
| `--service-name` | Query service name | |
| `--start` | See [Common options](#common-options) | See [Common options](#common-options) |
| `--end` | See [Common options](#common-options) | See [Common options](#common-options) |
# Developer guide # Developer guide
## Compiling and building ## Compiling and building
......
...@@ -19,7 +19,7 @@ package flags ...@@ -19,7 +19,7 @@ package flags
import "github.com/urfave/cli" import "github.com/urfave/cli"
var InstanceServiceIDFlags = append(DurationFlags, var InstanceServiceIDFlags = []cli.Flag{
cli.StringFlag{ cli.StringFlag{
Name: "service-id", Name: "service-id",
Usage: "query service `ID` (priority over \"--service-name\")", Usage: "query service `ID` (priority over \"--service-name\")",
...@@ -28,4 +28,4 @@ var InstanceServiceIDFlags = append(DurationFlags, ...@@ -28,4 +28,4 @@ var InstanceServiceIDFlags = append(DurationFlags,
Name: "service-name", Name: "service-name",
Usage: "query service `Name`", Usage: "query service `Name`",
}, },
) }
// 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 flags
import "github.com/urfave/cli"
var SearchRegexFlags = []cli.Flag{
cli.StringFlag{
Name: "regex",
Required: true,
Usage: "search `Regex`",
},
}
...@@ -19,6 +19,9 @@ package instance ...@@ -19,6 +19,9 @@ package instance
import ( import (
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/apache/skywalking-cli/graphql/client"
"github.com/apache/skywalking-cli/logger"
) )
var Command = cli.Command{ var Command = cli.Command{
...@@ -27,5 +30,24 @@ var Command = cli.Command{ ...@@ -27,5 +30,24 @@ var Command = cli.Command{
Usage: "Instance related sub-command", Usage: "Instance related sub-command",
Subcommands: cli.Commands{ Subcommands: cli.Commands{
ListCommand, ListCommand,
SearchCommand,
}, },
} }
func verifyAndSwitchServiceParameter(ctx *cli.Context) string {
serviceID := ctx.String("service-id")
serviceName := ctx.String("service-name")
if serviceID == "" && serviceName == "" {
logger.Log.Fatalf("flags \"service-id, service-name\" must set one")
}
if serviceID == "" && serviceName != "" {
service, err := client.SearchService(ctx, serviceName)
if err != nil {
logger.Log.Fatalln(err)
}
serviceID = service.ID
}
return serviceID
}
...@@ -26,32 +26,18 @@ import ( ...@@ -26,32 +26,18 @@ import (
"github.com/apache/skywalking-cli/display" "github.com/apache/skywalking-cli/display"
"github.com/apache/skywalking-cli/graphql/client" "github.com/apache/skywalking-cli/graphql/client"
"github.com/apache/skywalking-cli/graphql/schema" "github.com/apache/skywalking-cli/graphql/schema"
"github.com/apache/skywalking-cli/logger"
) )
var ListCommand = cli.Command{ var ListCommand = cli.Command{
Name: "list", Name: "list",
ShortName: "ls", ShortName: "ls",
Usage: "List all available instance by given --service-id or --service-name parameter", Usage: "List all available instance by given --service-id or --service-name parameter",
Flags: flags.InstanceServiceIDFlags, Flags: append(flags.DurationFlags, flags.InstanceServiceIDFlags...),
Before: interceptor.BeforeChain([]cli.BeforeFunc{ Before: interceptor.BeforeChain([]cli.BeforeFunc{
interceptor.DurationInterceptor, interceptor.DurationInterceptor,
}), }),
Action: func(ctx *cli.Context) error { Action: func(ctx *cli.Context) error {
serviceID := ctx.String("service-id") serviceID := verifyAndSwitchServiceParameter(ctx)
serviceName := ctx.String("service-name")
if serviceID == "" && serviceName == "" {
logger.Log.Fatalf("flags \"service-id, service-name\" must set one")
}
if serviceID == "" && serviceName != "" {
service, err := client.SearchService(ctx, serviceName)
if err != nil {
logger.Log.Fatalln(err)
}
serviceID = service.ID
}
end := ctx.String("end") end := ctx.String("end")
start := ctx.String("start") start := ctx.String("start")
......
// 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 instance
import (
"regexp"
"github.com/urfave/cli"
"github.com/apache/skywalking-cli/commands/flags"
"github.com/apache/skywalking-cli/commands/interceptor"
"github.com/apache/skywalking-cli/commands/model"
"github.com/apache/skywalking-cli/display"
"github.com/apache/skywalking-cli/graphql/client"
"github.com/apache/skywalking-cli/graphql/schema"
)
var SearchCommand = cli.Command{
Name: "search",
Usage: "Filter the instance from the existing service instance list",
Flags: append(flags.DurationFlags, append(flags.SearchRegexFlags, flags.InstanceServiceIDFlags...)...),
Before: interceptor.BeforeChain([]cli.BeforeFunc{
interceptor.DurationInterceptor,
}),
Action: func(ctx *cli.Context) error {
serviceID := verifyAndSwitchServiceParameter(ctx)
end := ctx.String("end")
start := ctx.String("start")
step := ctx.Generic("step")
regex := ctx.String("regex")
instances := client.Instances(ctx, serviceID, schema.Duration{
Start: start,
End: end,
Step: step.(*model.StepEnumValue).Selected,
})
var result []schema.ServiceInstance
if len(instances) > 0 {
for _, instance := range instances {
if ok, _ := regexp.Match(regex, []byte(instance.Name)); ok {
result = append(result, instance)
}
}
}
return display.Display(ctx, result)
},
}
// 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 json
import (
"testing"
"github.com/apache/skywalking-cli/graphql/schema"
)
func TestJsonDisplay(t *testing.T) {
var result []schema.Service
display(t, result)
result = make([]schema.Service, 0)
display(t, result)
result = append(result, schema.Service{
ID: "1",
Name: "json",
})
display(t, result)
}
func display(t *testing.T, result []schema.Service) {
if err := Display(result); err != nil {
t.Error(err)
}
}
...@@ -30,6 +30,10 @@ func Display(object interface{}) error { ...@@ -30,6 +30,10 @@ func Display(object interface{}) error {
bytes, _ := json.Marshal(object) bytes, _ := json.Marshal(object)
_ = json.Unmarshal(bytes, &objMaps) _ = json.Unmarshal(bytes, &objMaps)
if len(objMaps) < 1 {
return nil
}
var header []string var header []string
for k := range objMaps[0] { for k := range objMaps[0] {
......
// 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 table
import (
"testing"
"github.com/apache/skywalking-cli/graphql/schema"
)
func TestTableDisplay(t *testing.T) {
var result []schema.Service
display(t, result)
result = make([]schema.Service, 0)
display(t, result)
result = append(result, schema.Service{
ID: "1",
Name: "table",
})
display(t, result)
}
func display(t *testing.T, result []schema.Service) {
if err := Display(result); err != nil {
t.Error(err)
}
}
// 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 yaml
import (
"testing"
"github.com/apache/skywalking-cli/graphql/schema"
)
func TestYamlDisplay(t *testing.T) {
var result []schema.Service
display(t, result)
result = make([]schema.Service, 0)
display(t, result)
result = append(result, schema.Service{
ID: "1",
Name: "yaml",
})
display(t, result)
}
func display(t *testing.T, result []schema.Service) {
if err := Display(result); err != nil {
t.Error(err)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册