Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI issue with service execute command fix #546 #553

Merged
merged 12 commits into from
Nov 1, 2018
50 changes: 25 additions & 25 deletions commands/service_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package commands
import (
"encoding/json"
"fmt"
"io/ioutil"
"time"

"github.com/mesg-foundation/core/protobuf/coreapi"
"github.com/mesg-foundation/core/utils/pretty"
casting "github.com/mesg-foundation/core/utils/servicecasting"
"github.com/mesg-foundation/core/x/xjson"
"github.com/mesg-foundation/core/x/xpflag"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
Expand Down Expand Up @@ -51,27 +51,30 @@ func (c *serviceExecuteCmd) preRunE(cmd *cobra.Command, args []string) error {

func (c *serviceExecuteCmd) runE(cmd *cobra.Command, args []string) error {
var (
err error
s *coreapi.Service
result *coreapi.ResultData
listenResultsC chan *coreapi.ResultData
inputData string
resultsErrC chan error
err error
)
pretty.Progress(fmt.Sprintf("Executing task %q...", c.taskKey), func() {
var s *coreapi.Service
s, err = c.e.ServiceByID(args[0])
if err != nil {
return
}

if err = c.getTaskKey(s); err != nil {
return
}
pretty.Progress("Loading the service...", func() {
s, err = c.e.ServiceByID(args[0])
})
if err != nil {
return err
}

var inputData string
inputData, err = c.getData(c.taskKey, s, c.executeData)
if err != nil {
return
}
if err = c.getTaskKey(s); err != nil {
return err
}

inputData, err = c.getData(c.taskKey, s, c.executeData)
if err != nil {
return err
}
pretty.Progress(fmt.Sprintf("Executing task %q...", c.taskKey), func() {
// Create an unique tag that will be used to listen to the result of this exact execution
tags := []string{uuid.NewV4().String()}

Expand All @@ -92,13 +95,10 @@ func (c *serviceExecuteCmd) runE(cmd *cobra.Command, args []string) error {
}
fmt.Printf("%s Task %q executed\n", pretty.SuccessSign, c.taskKey)

var result *coreapi.ResultData
pretty.Progress("Waiting for result...", func() {
select {
case result = <-listenResultsC:
return
case err = <-resultsErrC:
return
}
})
if err != nil {
Expand Down Expand Up @@ -146,18 +146,18 @@ func (c *serviceExecuteCmd) getData(taskKey string, s *coreapi.Service, dataStru
return "", errors.New("no filepath given")
}
}
return readJSONFile(c.jsonFile)
}

func readJSONFile(path string) (string, error) {
if path == "" {
// still no answer then return empty json object
if c.jsonFile == "" {
return "{}", nil
}
data, err := ioutil.ReadFile(path)

content, err := xjson.ReadFile(c.jsonFile)
if err != nil {
return "", err
}
return string(data), nil

return string(content), nil
}

func taskKeysFromService(s *coreapi.Service) []string {
Expand Down
21 changes: 21 additions & 0 deletions x/xjson/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package xjson

import (
"encoding/json"
"io/ioutil"
)

// ReadFile reads the file named by filename and returns the contents.
// It returns error if file is not well formated json.
func ReadFile(filename string) ([]byte, error) {
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}

var raw json.RawMessage
if err := json.Unmarshal(content, &raw); err != nil {
return nil, err
}
return raw, nil
}
45 changes: 45 additions & 0 deletions x/xjson/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package xjson

import (
"encoding/json"
"io/ioutil"
"os"
"testing"
)

const testfilenmae = "data.test"

func TestReadFile(t *testing.T) {
if err := ioutil.WriteFile(testfilenmae, []byte("{}"), os.ModePerm); err != nil {
t.Fatalf("write error: %s", err)
}
defer os.Remove(testfilenmae)

content, err := ReadFile(testfilenmae)
if err != nil {
t.Fatalf("read error: %s", err)
}

if string(content) != "{}" {
t.Fatalf("invalid content - got: %s, want: {}", string(content))
}
}

func TestReadFileNoFile(t *testing.T) {
_, err := ReadFile("nodata.test")
if _, ok := err.(*os.PathError); !ok {
t.Fatalf("read expected os.PathError - got: %s", err)
}
}

func TestReadFileInvalidJSON(t *testing.T) {
if err := ioutil.WriteFile(testfilenmae, []byte("{"), os.ModePerm); err != nil {
t.Fatalf("write error: %s", err)
}
defer os.Remove(testfilenmae)

_, err := ReadFile(testfilenmae)
if _, ok := err.(*json.SyntaxError); !ok {
t.Fatalf("read expected json.SyntaxError - got: %s", err)
}
}