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

Refactor-x, closes #373 #386

Merged
merged 14 commits into from Aug 27, 2018
5 changes: 5 additions & 0 deletions .codeclimate.yml
Expand Up @@ -14,3 +14,8 @@ plugins:
enabled: true
govet:
enabled: true

checks:
return-statements:
config:
threshold: 6
4 changes: 2 additions & 2 deletions api/core/listen_event.go
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/mesg-foundation/core/event"
"github.com/mesg-foundation/core/pubsub"
service "github.com/mesg-foundation/core/service"
"github.com/mesg-foundation/core/utils/array"
"github.com/mesg-foundation/core/x/xstrings"
)

// ListenEvent listens for an event from a specific service.
Expand Down Expand Up @@ -56,5 +56,5 @@ func validateEventKey(service *service.Service, eventKey string) error {
}

func isSubscribedEvent(request *ListenEventRequest, e *event.Event) bool {
return array.IncludedIn([]string{"", "*", e.Key}, request.EventFilter)
return xstrings.SliceContains([]string{"", "*", e.Key}, request.EventFilter)
}
8 changes: 4 additions & 4 deletions api/core/listen_result.go
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/mesg-foundation/core/execution"
"github.com/mesg-foundation/core/pubsub"
service "github.com/mesg-foundation/core/service"
"github.com/mesg-foundation/core/utils/array"
"github.com/mesg-foundation/core/x/xstrings"
)

// ListenResult listens for results from a services.
Expand Down Expand Up @@ -87,16 +87,16 @@ func isSubscribed(request *ListenResultRequest, e *execution.Execution) bool {
}

func isSubscribedToTask(request *ListenResultRequest, e *execution.Execution) bool {
return array.IncludedIn([]string{"", "*", e.Task}, request.TaskFilter)
return xstrings.SliceContains([]string{"", "*", e.Task}, request.TaskFilter)
}

func isSubscribedToOutput(request *ListenResultRequest, e *execution.Execution) bool {
return array.IncludedIn([]string{"", "*", e.Output}, request.OutputFilter)
return xstrings.SliceContains([]string{"", "*", e.Output}, request.OutputFilter)
}

func isSubscribedToTags(request *ListenResultRequest, e *execution.Execution) bool {
for _, tag := range request.TagFilters {
if !array.IncludedIn(e.Tags, tag) {
if !xstrings.SliceContains(e.Tags, tag) {
return false
}
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/service/dev.go
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/logrusorgru/aurora"
"github.com/mesg-foundation/core/api/core"
"github.com/mesg-foundation/core/cmd/utils"
"github.com/mesg-foundation/core/x/xsignal"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -39,7 +40,7 @@ func devHandler(cmd *cobra.Command, args []string) {
closeReaders := showLogs(serviceID, "*")
defer closeReaders()

<-utils.WaitForCancel()
<-xsignal.WaitForInterrupt()

utils.ShowSpinnerForFunc(utils.SpinnerOptions{Text: "Deleting test service..."}, func() {
cli().DeleteService(context.Background(), &core.DeleteServiceRequest{ // Delete service. This will automatically stop the service too
Expand Down
2 changes: 1 addition & 1 deletion cmd/service/execute.go
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/mesg-foundation/core/api/core"
"github.com/mesg-foundation/core/cmd/utils"
"github.com/mesg-foundation/core/service"
"github.com/mesg-foundation/core/utils/xpflag"
"github.com/mesg-foundation/core/x/xpflag"
uuid "github.com/satori/go.uuid"
"github.com/spf13/cobra"
survey "gopkg.in/AlecAivazis/survey.v1"
Expand Down
2 changes: 1 addition & 1 deletion cmd/service/execute_test.go
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"github.com/mesg-foundation/core/service"
"github.com/mesg-foundation/core/utils/xpflag"
"github.com/mesg-foundation/core/x/xpflag"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
)
Expand Down
12 changes: 10 additions & 2 deletions cmd/service/init.go
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/logrusorgru/aurora"
"github.com/mesg-foundation/core/cmd/utils"
"github.com/mesg-foundation/core/x/xgit"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"
)
Expand Down Expand Up @@ -140,12 +141,19 @@ func getTemplateResult(result string, templates []*templateStruct) (tmpl *templa
}

func downloadTemplate(tmpl *templateStruct) (path string, err error) {
path, err = createTempFolder()
path, err = ioutil.TempDir("", utils.TempDirPrefix)
if err != nil {
return "", err
}

return path, gitClone(tmpl.URL, path, "Downloading template "+tmpl.Name+"...")
message := fmt.Sprintf("Downloading template %s ...\n", tmpl.Name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not valid, the \n create some problems with the spinner. We should never use a \n when it's a message for the spinner

⣾ Downloading template Javascript ...
⣽ Downloading template Javascript ...
⣻ Downloading template Javascript ...
⢿ Downloading template Javascript ...
⡿ Downloading template Javascript ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

utils.ShowSpinnerForFunc(utils.SpinnerOptions{Text: message}, func() {
err = xgit.Clone(tmpl.URL, path)
})
if err != nil {
return "", err
}
return path, nil
}

func ask(label string, value string, validator survey.Validator) string {
Expand Down
3 changes: 2 additions & 1 deletion cmd/service/logs.go
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/docker/docker/pkg/stdcopy"
"github.com/mesg-foundation/core/api/core"
"github.com/mesg-foundation/core/cmd/utils"
"github.com/mesg-foundation/core/x/xsignal"
"github.com/spf13/cobra"
)

Expand All @@ -28,7 +29,7 @@ func init() {
func logsHandler(cmd *cobra.Command, args []string) {
closeReaders := showLogs(args[0], cmd.Flag("dependency").Value.String())
defer closeReaders()
<-utils.WaitForCancel()
<-xsignal.WaitForInterrupt()
}

func showLogs(serviceID string, dependency string) func() {
Expand Down
35 changes: 6 additions & 29 deletions cmd/service/utils.go
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io/ioutil"
"log"
"net/url"
"os"

"github.com/asaskevich/govalidator"
Expand All @@ -15,10 +14,9 @@ import (
"github.com/mesg-foundation/core/container"
"github.com/mesg-foundation/core/service"
"github.com/mesg-foundation/core/service/importer"
"github.com/mesg-foundation/core/x/xgit"
"github.com/spf13/viper"
"google.golang.org/grpc"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
)

// TODO(ilgooz): remove this after service package made Newable.
Expand Down Expand Up @@ -77,40 +75,19 @@ func downloadServiceIfNeeded(path string) (newPath string, didDownload bool, err
if !govalidator.IsURL(path) {
return path, false, nil
}
newPath, err = createTempFolder()
newPath, err = ioutil.TempDir("", utils.TempDirPrefix)
if err != nil {
return "", false, err
}
if err := gitClone(path, newPath, "Downloading service..."); err != nil {
utils.ShowSpinnerForFunc(utils.SpinnerOptions{Text: "Downloading service..."}, func() {
err = xgit.Clone(path, newPath)
})
if err != nil {
return "", false, err
}
return newPath, true, nil
}

func gitClone(repoURL string, path string, message string) error {
u, err := url.Parse(repoURL)
if err != nil {
return err
}
if u.Scheme == "" {
u.Scheme = "https"
}
options := &git.CloneOptions{}
if u.Fragment != "" {
options.ReferenceName = plumbing.ReferenceName("refs/heads/" + u.Fragment)
u.Fragment = ""
}
options.URL = u.String()
utils.ShowSpinnerForFunc(utils.SpinnerOptions{Text: message}, func() {
_, err = git.PlainClone(path, false, options)
})
return err
}

func createTempFolder() (path string, err error) {
return ioutil.TempDir("", "mesg-")
}

func buildDockerImage(path string) (imageHash string, err error) {
utils.ShowSpinnerForFunc(utils.SpinnerOptions{Text: "Building image..."}, func() {
imageHash, err = defaultContainer.Build(path)
Expand Down
41 changes: 0 additions & 41 deletions cmd/service/utils_test.go
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/mesg-foundation/core/service"
"github.com/stretchr/testify/require"
git "gopkg.in/src-d/go-git.v4"
)

func TestDefaultPath(t *testing.T) {
Expand All @@ -20,33 +19,6 @@ func TestBuildDockerImagePathDoNotExist(t *testing.T) {
require.NotNil(t, err)
}

func TestGitCloneRepositoryDoNotExist(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should keep these tests

Copy link
Contributor

@ilgooz ilgooz Aug 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed my mind, no need this here but got to put equivalent to xgit. and mock underlying git package if possible. we need these tests to see if we call git.PlainClone() with the right args.

path, _ := createTempFolder()
defer os.RemoveAll(path)
err := gitClone("/doNotExist", path, "testing...")
require.NotNil(t, err)
}

func TestGitCloneWithoutURLSchema(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should keep these tests

Copy link
Contributor

@ilgooz ilgooz Aug 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed my mind, no need this here but got to put equivalent to xgit. and mock underlying git package if possible. we need these tests to see if we call git.PlainClone() with the right args.

path, _ := createTempFolder()
defer os.RemoveAll(path)
err := gitClone("github.com/mesg-foundation/awesome.git", path, "testing...")
require.Nil(t, err)
}

func TestGitCloneCustomBranch(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should keep these tests

Copy link
Contributor

@ilgooz ilgooz Aug 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed my mind, no need this here but got to put equivalent to xgit. and mock underlying git package if possible. we need these tests to see if we call git.PlainClone() with the right args.

branchName := "5-generic-service"
path, _ := createTempFolder()
defer os.RemoveAll(path)
err := gitClone("github.com/mesg-foundation/service-ethereum-erc20#"+branchName, path, "testing...")
require.Nil(t, err)
repo, err := git.PlainOpen(path)
require.Nil(t, err)
branch, err := repo.Branch(branchName)
require.Nil(t, err)
require.NotNil(t, branch)
}

func TestDownloadServiceIfNeededAbsolutePath(t *testing.T) {
path := "/users/paul/service-js-function"
newPath, didDownload, err := downloadServiceIfNeeded(path)
Expand All @@ -72,19 +44,6 @@ func TestDownloadServiceIfNeededUrl(t *testing.T) {
require.Equal(t, true, didDownload)
}

func TestCreateTempFolder(t *testing.T) {
path, err := createTempFolder()
defer os.RemoveAll(path)
require.Nil(t, err)
require.NotEqual(t, "", path)
}

func TestRemoveTempFolder(t *testing.T) {
path, _ := createTempFolder()
err := os.RemoveAll(path)
require.Nil(t, err)
}

func TestInjectConfigurationInDependencies(t *testing.T) {
s := &service.Service{}
injectConfigurationInDependencies(s, "TestInjectConfigurationInDependencies")
Expand Down
14 changes: 0 additions & 14 deletions cmd/utils/cancel.go

This file was deleted.

4 changes: 4 additions & 0 deletions cmd/utils/const.go
@@ -0,0 +1,4 @@
package utils

// TempDirPrefix is a prefix for temp dirctories or files.
const TempDirPrefix = "mesg-"
9 changes: 2 additions & 7 deletions core/main.go
@@ -1,14 +1,11 @@
package main

import (
"os"
"os/signal"
"syscall"

"github.com/mesg-foundation/core/api"
"github.com/mesg-foundation/core/config"
"github.com/mesg-foundation/core/logger"
"github.com/mesg-foundation/core/version"
"github.com/mesg-foundation/core/x/xsignal"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
Expand All @@ -27,9 +24,7 @@ func main() {
Network: "unix",
Address: viper.GetString(config.APIServerSocket),
})
abort := make(chan os.Signal, 1)
signal.Notify(abort, syscall.SIGINT, syscall.SIGTERM)
<-abort
<-xsignal.WaitForInterrupt()
}

func startServer(server *api.Server) {
Expand Down
10 changes: 2 additions & 8 deletions daemon/start.go
@@ -1,12 +1,11 @@
package daemon

import (
"net"
"path/filepath"
"strconv"

"github.com/mesg-foundation/core/config"
"github.com/mesg-foundation/core/container"
"github.com/mesg-foundation/core/x/xnet"
"github.com/spf13/viper"
)

Expand All @@ -29,12 +28,7 @@ func serviceSpec() (spec container.ServiceOptions, err error) {
return container.ServiceOptions{}, err
}

_, portStr, err := net.SplitHostPort(viper.GetString(config.APIServerAddress))
if err != nil {
return container.ServiceOptions{}, err
}

port, err := strconv.ParseInt(portStr, 10, 64)
_, port, err := xnet.SplitHostPort(viper.GetString(config.APIServerAddress))
if err != nil {
return container.ServiceOptions{}, err
}
Expand Down
8 changes: 2 additions & 6 deletions service/hash.go
@@ -1,12 +1,8 @@
package service

import (
"github.com/cnf/structhash"
)
import "github.com/mesg-foundation/core/x/xstructhash"

// Hash calculates and returns the hash of the service.
func (service *Service) Hash() string {
// Ignore the err result because the lib always return nil
hash, _ := structhash.Hash(service, 1) // TODO: why not reuse the package utils/hash?
return hash
return xstructhash.Hash(service, 1)
}
16 changes: 0 additions & 16 deletions utils/array/include.go

This file was deleted.

18 changes: 0 additions & 18 deletions utils/array/include_test.go

This file was deleted.