From b22f153d4c5ce7c40771d096a0c48ad89a76ba22 Mon Sep 17 00:00:00 2001 From: Mario Lubenka Date: Sun, 26 Sep 2021 20:00:43 +0200 Subject: [PATCH] feat: add gorpc --- go.mod | 5 ++++- go.sum | 2 ++ install.go | 8 ++++---- stackhead.go | 14 ++++++++++---- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 08725b6..d764fb6 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/getstackhead/pluginlib go 1.17 -require github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef +require ( + github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef + github.com/valyala/gorpc v0.0.0-20160519171614-908281bef774 +) diff --git a/go.sum b/go.sum index a2349a2..3555a9f 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef h1:2JGTg6JapxP9/R33ZaagQtAM4EkkSYnIAlOG5EI8gkM= github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef/go.mod h1:JS7hed4L1fj0hXcyEejnW57/7LCetXggd+vwrRnYeII= +github.com/valyala/gorpc v0.0.0-20160519171614-908281bef774 h1:SUHFQHAaySqF0YHCmmm0EIFooFZpDPpi5KTom7YJ07c= +github.com/valyala/gorpc v0.0.0-20160519171614-908281bef774/go.mod h1:8uNqM1i7pr0jO7gdvbNCgsSa8Ki2vMh7JCQxO9BlF90= diff --git a/install.go b/install.go index 91e30ff..956e1b4 100644 --- a/install.go +++ b/install.go @@ -4,10 +4,10 @@ type PackageName struct { ApkPackageName string } -func InstallPackage(packageName PackageName) <-chan error { - return StackHeadMain.Execute(IntCmdInstallPkgApk, packageName.ApkPackageName) +func InstallPackage(packageName PackageName) error { + return ExecCmd(IntCmdInstallPkgApk, packageName.ApkPackageName) } -func UninstallPackage(packageName PackageName) <-chan error { - return StackHeadMain.Execute(IntCmdUninstallPkgApk, packageName.ApkPackageName) +func UninstallPackage(packageName PackageName) error { + return ExecCmd(IntCmdUninstallPkgApk, packageName.ApkPackageName) } diff --git a/stackhead.go b/stackhead.go index aa79f46..a9bf7f8 100644 --- a/stackhead.go +++ b/stackhead.go @@ -1,12 +1,18 @@ package pluginlib -type IStackHeadMain interface { - Execute(command string, args ...interface{}) <-chan error -} +import "github.com/valyala/gorpc" const ( IntCmdInstallPkgApk string = "stackhead:install:package:apk" IntCmdUninstallPkgApk string = "stackhead:uninstall:package:apk" ) -var StackHeadMain IStackHeadMain = nil +func ExecCmd(command string, args ...interface{}) error { + c := &gorpc.Client{Addr: "localhost:1412"} + c.Start() + defer c.Stop() + // All client methods issuing RPCs are thread-safe and goroutine-safe, + // i.e. it is safe to call them from multiple concurrently running goroutines. + _, err := c.Call(command, args...) + return err +}