Skip to content

Commit

Permalink
Merge pull request #233 from mesg-foundation/improve-test-command
Browse files Browse the repository at this point in the history
Add logs to test command
  • Loading branch information
NicolasMahe authored Jun 19, 2018
2 parents 496ad16 + 1824a5b commit 41c04f0
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- (#183) Add a `configuration` attribute in the `mesg.yml` file to accept docker configuration for your service
- (#187) Stop all services when the MESG Core stops
- (#190) Possibility to `test` or `deploy` a service from a git or GitHub url
- (#233) Add logs in the `service test` command with service logs by default and all dependencies logs with the `--full-logs` flag
- (#235) Add `ListServices` and `GetService` APIs

#### Removed
Expand Down
17 changes: 13 additions & 4 deletions cmd/service/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,24 @@ func init() {
}

func logsHandler(cmd *cobra.Command, args []string) {
closeReaders := showLogs(args[0], cmd.Flag("dependency").Value.String())
defer closeReaders()
<-utils.WaitForCancel()
}

func showLogs(serviceID string, dependency string) func() {
reply, err := cli.GetService(context.Background(), &core.GetServiceRequest{
ServiceID: args[0],
ServiceID: serviceID,
})
utils.HandleError(err)
readers, err := reply.Service.Logs(cmd.Flag("dependency").Value.String())
readers, err := reply.Service.Logs(dependency)
utils.HandleError(err)
for _, reader := range readers {
defer reader.Close()
go stdcopy.StdCopy(os.Stdout, os.Stderr, reader)
}
<-utils.WaitForCancel()
return func() {
for _, reader := range readers {
reader.Close()
}
}
}
12 changes: 9 additions & 3 deletions cmd/service/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var Test = &cobra.Command{
See more detail on the [Test page from the documentation](https://docs.mesg.tech/service/test.html)`,
Example: `mesg-core service test
mesg-core service test ./SERVICE_FOLDER
mesg-core service test --logs-all
mesg-core service test --event-filter EVENT_NAME
mesg-core service test --task TASK_NAME --data ./PATH_TO_DATA_FILE.json
mesg-core service test --task-filter TASK_NAME --output-filter OUTPUT_NAME
Expand All @@ -38,6 +39,7 @@ func init() {
Test.Flags().StringP("event-filter", "e", "*", "Only log the data of the given event")
Test.Flags().StringP("task-filter", "r", "", "Only log the result of the given task")
Test.Flags().StringP("output-filter", "o", "", "Only log the data of the given output of a task result. If set, you also need to set the task in --task-filter")
Test.Flags().BoolP("logs-all", "a", false, "Display logs from service and its dependencies")
}

func listenEvents(serviceID string, filter string) {
Expand Down Expand Up @@ -96,7 +98,6 @@ func executeTask(serviceID string, task string, dataPath string) (execution *cor
}

func testHandler(cmd *cobra.Command, args []string) {
var err error
serviceID := cmd.Flag("serviceID").Value.String()
if serviceID == "" {
service := prepareService(defaultPath(args))
Expand All @@ -119,18 +120,23 @@ func testHandler(cmd *cobra.Command, args []string) {

go listenEvents(serviceID, cmd.Flag("event-filter").Value.String())
go listenResults(serviceID, cmd.Flag("task-filter").Value.String(), cmd.Flag("output-filter").Value.String())
logs := "service"
if cmd.Flag("logs-all").Value.String() == "true" {
logs = "*"
}
closeReaders := showLogs(serviceID, logs)
defer closeReaders()

time.Sleep(time.Second)
executeTask(serviceID, cmd.Flag("task").Value.String(), cmd.Flag("data").Value.String())
<-utils.WaitForCancel()

if cmd.Flag("keep-alive").Value.String() != "true" {
utils.ShowSpinnerForFunc(utils.SpinnerOptions{Text: "Stopping service..."}, func() {
_, err = cli.StopService(context.Background(), &core.StopServiceRequest{
cli.StopService(context.Background(), &core.StopServiceRequest{
ServiceID: serviceID,
})
})
utils.HandleError(err)
fmt.Println(aurora.Green("Service stopped"))
}
}
9 changes: 6 additions & 3 deletions docs/cli/mesg-core.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## mesg-core

MESG CORE
MESG Core

### Synopsis

MESG CORE
MESG Core

### Options

Expand All @@ -14,6 +14,9 @@ MESG CORE

### SEE ALSO

* [mesg-core daemon](mesg-core_daemon.md) - Manage the MESG daemon
* [mesg-core logs](mesg-core_logs.md) - Show the MESG Core's logs
* [mesg-core service](mesg-core_service.md) - Manage your services
* [mesg-core start](mesg-core_start.md) - Start the MESG Core
* [mesg-core status](mesg-core_status.md) - Status of the MESG Core
* [mesg-core stop](mesg-core_stop.md) - Stop the MESG Core

22 changes: 22 additions & 0 deletions docs/cli/mesg-core_logs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## mesg-core logs

Show the MESG Core's logs

### Synopsis

Show the MESG Core's logs

```
mesg-core logs [flags]
```

### Options

```
-h, --help help for logs
```

### SEE ALSO

* [mesg-core](mesg-core.md) - MESG Core

5 changes: 3 additions & 2 deletions docs/cli/mesg-core_service.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ Manage your services

### SEE ALSO

* [mesg-core](mesg-core.md) - MESG CORE
* [mesg-core service delete](mesg-core_service_delete.md) - Delete a service
* [mesg-core](mesg-core.md) - MESG Core
* [mesg-core service delete](mesg-core_service_delete.md) - Delete one or many services
* [mesg-core service deploy](mesg-core_service_deploy.md) - Deploy a service
* [mesg-core service detail](mesg-core_service_detail.md) - Show details of a published service
* [mesg-core service init](mesg-core_service_init.md) - Initialize a service
* [mesg-core service list](mesg-core_service_list.md) - List all published services
* [mesg-core service logs](mesg-core_service_logs.md) - Show the logs of a service
* [mesg-core service start](mesg-core_service_start.md) - Start a service
* [mesg-core service status](mesg-core_service_status.md) - List started services
* [mesg-core service stop](mesg-core_service_stop.md) - Stop a service
Expand Down
8 changes: 5 additions & 3 deletions docs/cli/mesg-core_service_delete.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## mesg-core service delete

Delete a service
Delete one or many services

### Synopsis

Delete a service
Delete one or many services

```
mesg-core service delete [flags]
Expand All @@ -13,12 +13,14 @@ mesg-core service delete [flags]
### Examples

```
mesg-core service delete
mesg-core service delete SERVICE_ID
mesg-core service delete SERVICE_ID_1 SERVICE_ID_2
```

### Options

```
--all Delete all services
-h, --help help for delete
```

Expand Down
6 changes: 3 additions & 3 deletions docs/cli/mesg-core_service_deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Deploy a service

### Synopsis

Deploy a service on the Network.
Deploy a service.

To get more information, see the [deploy page from the documentation](https://docs.mesg.tech/service/publish-a-service)
To get more information, see the [deploy page from the documentation](https://docs.mesg.com/service/deploy-a-service)

```
mesg-core service deploy [flags]
Expand All @@ -15,7 +15,7 @@ mesg-core service deploy [flags]
### Examples

```
mesg-core sevice deploy
mesg-core service deploy PATH_TO_SERVICE
```

### Options
Expand Down
30 changes: 30 additions & 0 deletions docs/cli/mesg-core_service_logs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## mesg-core service logs

Show the logs of a service

### Synopsis

Show the logs of a service

```
mesg-core service logs [flags]
```

### Examples

```
mesg-core service logs SERVICE_ID
mesg-core service logs SERVICE_ID --dependency DEPENDENCY_NAME
```

### Options

```
-d, --dependency string Name of the dependency to only show the logs from (default "*")
-h, --help help for logs
```

### SEE ALSO

* [mesg-core service](mesg-core_service.md) - Manage your services

24 changes: 13 additions & 11 deletions docs/cli/mesg-core_service_test.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,24 @@ mesg-core service test [flags]
```
mesg-core service test
mesg-core service test ./SERVICE_FOLDER
mesg-core service test --event EVENT_NAME
mesg-core service test --task TASK_NAME --data ./PATH_TO_DATA_FILE.yml
mesg-core service test --keep-alive
mesg-core service test --event-filter EVENT_NAME
mesg-core service test --task TASK_NAME --data ./PATH_TO_DATA_FILE.json
mesg-core service test --task-filter TASK_NAME --output-filter OUTPUT_NAME
mesg-core service test --serviceID SERVICE_ID --keep-alive
```

### Options

```
-d, --data string Path to the file containing the data required to run the task
-e, --event string Only log a specific event (default "*")
-h, --help help for test
--keep-alive Do not stop the service
-o, --output string Filter output of a task
-r, --result string Filter the result of a specific task
-s, --service string Debug a deployed service
-t, --task string Run a specific task
-d, --data string Path to the file containing the data required to run the task
-e, --event-filter string Only log the data of the given event (default "*")
-f, --full-logs Display logs from service and its dependencies
-h, --help help for test
--keep-alive Do not stop the service at the end of this command
-o, --output-filter string Only log the data of the given output of a task result. If set, you also need to set the task in --task-filter
-s, --serviceID string ID of a previously deployed service
-t, --task string Run the given task
-r, --task-filter string Only log the result of the given task
```

### SEE ALSO
Expand Down
22 changes: 22 additions & 0 deletions docs/cli/mesg-core_start.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## mesg-core start

Start the MESG Core

### Synopsis

Start the MESG Core

```
mesg-core start [flags]
```

### Options

```
-h, --help help for start
```

### SEE ALSO

* [mesg-core](mesg-core.md) - MESG Core

22 changes: 22 additions & 0 deletions docs/cli/mesg-core_status.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## mesg-core status

Status of the MESG Core

### Synopsis

Status of the MESG Core

```
mesg-core status [flags]
```

### Options

```
-h, --help help for status
```

### SEE ALSO

* [mesg-core](mesg-core.md) - MESG Core

22 changes: 22 additions & 0 deletions docs/cli/mesg-core_stop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## mesg-core stop

Stop the MESG Core

### Synopsis

Stop the MESG Core

```
mesg-core stop [flags]
```

### Options

```
-h, --help help for stop
```

### SEE ALSO

* [mesg-core](mesg-core.md) - MESG Core

0 comments on commit 41c04f0

Please sign in to comment.