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
2 changes: 1 addition & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ plugins:
checks:
return-statements:
config:
threshold: 6
threshold: 6
4 changes: 2 additions & 2 deletions api/core/listen_event.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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 @@ -43,7 +44,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 ...", tmpl.Name)
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
Original file line number Diff line number Diff line change
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
28 changes: 0 additions & 28 deletions cmd/service/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package service

import (
"fmt"
"io/ioutil"
"net/url"
"os"

"github.com/logrusorgru/aurora"
Expand All @@ -13,8 +11,6 @@ import (
"github.com/mesg-foundation/core/service/importer"
"github.com/spf13/viper"
"google.golang.org/grpc"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
)

func cli() core.CoreClient {
Expand All @@ -38,27 +34,3 @@ func handleValidationError(err error) {
os.Exit(0)
}
}

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-")
}
14 changes: 0 additions & 14 deletions cmd/utils/cancel.go

This file was deleted.

4 changes: 4 additions & 0 deletions cmd/utils/const.go
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
25 changes: 2 additions & 23 deletions mesg/deploy_deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"path/filepath"

Expand All @@ -13,9 +12,8 @@ import (
"github.com/mesg-foundation/core/database/services"
"github.com/mesg-foundation/core/service"
"github.com/mesg-foundation/core/service/importer"
"github.com/mesg-foundation/core/x/xgit"
uuid "github.com/satori/go.uuid"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
)

type serviceDeployer struct {
Expand Down Expand Up @@ -54,7 +52,7 @@ func (d *serviceDeployer) FromGitURL(url string) (*service.Service, *importer.Va
if err != nil {
return nil, nil, err
}
if err := d.gitClone(url, path); err != nil {
if err := xgit.Clone(url, path); err != nil {
return nil, nil, err
}
d.sendStatus(fmt.Sprintf("%s Service downloaded with success.", aurora.Green("✔")), DONE)
Expand All @@ -77,25 +75,6 @@ func (d *serviceDeployer) FromGzippedTar(r io.Reader) (*service.Service, *import
return d.deploy(path)
}

// gitClone clones a repo hosted at repoURL to path.
func (d *serviceDeployer) gitClone(repoURL string, path 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()
_, err = git.PlainClone(path, false, options)
return err
}

// deploy deploys a service in path.
func (d *serviceDeployer) deploy(path string) (*service.Service, *importer.ValidationError, error) {
defer os.RemoveAll(path)
Expand Down
37 changes: 0 additions & 37 deletions mesg/deploy_deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,8 @@ import (

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

func TestGitCloneRepositoryDoNotExist(t *testing.T) {
m, _ := newMESGAndDockerTest(t)
deployer := newServiceDeployer(m)

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

func TestGitCloneWithoutURLSchema(t *testing.T) {
m, _ := newMESGAndDockerTest(t)
deployer := newServiceDeployer(m)

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

func TestGitCloneCustomBranch(t *testing.T) {
m, _ := newMESGAndDockerTest(t)
deployer := newServiceDeployer(m)

branchName := "5-generic-service"
path, _ := deployer.createTempDir()
defer os.RemoveAll(path)
err := deployer.gitClone("github.com/mesg-foundation/service-ethereum-erc20#"+branchName, path)
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 TestCreateTempFolder(t *testing.T) {
m, _ := newMESGAndDockerTest(t)
deployer := newServiceDeployer(m)
Expand Down
10 changes: 3 additions & 7 deletions mesg/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"testing"

"github.com/cnf/structhash"
"github.com/docker/docker/pkg/archive"
"github.com/logrusorgru/aurora"
"github.com/mesg-foundation/core/service/importer"
"github.com/mesg-foundation/core/x/xdocker/xarchive"
"github.com/stretchr/testify/require"
)

Expand All @@ -27,9 +27,7 @@ func TestDeployService(t *testing.T) {
go func() {
defer wg.Done()

archive, err := archive.TarWithOptions(path, &archive.TarOptions{
Compression: archive.Gzip,
})
archive, err := xarchive.GzippedTar(path)
require.Nil(t, err)

service, validationError, err := mesg.DeployService(archive, DeployServiceStatusOption(statuses))
Expand Down Expand Up @@ -78,9 +76,7 @@ func TestDeployInvalidService(t *testing.T) {
go func() {
defer wg.Done()

archive, err := archive.TarWithOptions(path, &archive.TarOptions{
Compression: archive.Gzip,
})
archive, err := xarchive.GzippedTar(path)
require.Nil(t, err)

service, validationError, err := mesg.DeployService(archive, DeployServiceStatusOption(statuses))
Expand Down
Loading