aboutsummaryrefslogtreecommitdiff
path: root/widget
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2025-09-22 22:43:44 +0200
committerAnhgelus Morhtuuzh <william@herges.fr>2025-09-22 22:43:44 +0200
commitb19108ed8d4ad56e7bbf1ec59d0470e118275a49 (patch)
tree630995aadb2a02bfc3e7a4af3c67c7ac5d786aa2 /widget
parent433bd54a8b319377166ae3247d705684572cc2a4 (diff)
feat(widget): api to handle easily hello
Diffstat (limited to 'widget')
-rw-r--r--widget/api/handle.go34
-rw-r--r--widget/api/hello.go25
-rw-r--r--widget/widget.go13
3 files changed, 64 insertions, 8 deletions
diff --git a/widget/api/handle.go b/widget/api/handle.go
new file mode 100644
index 0000000..e04d3d2
--- /dev/null
+++ b/widget/api/handle.go
@@ -0,0 +1,34 @@
+package api
+
+import (
+ "fmt"
+ "io"
+ "os"
+)
+
+type Widget struct {
+ Hello
+ Writer io.Writer
+}
+
+func NewWidget(name, id string) *Widget {
+ return &Widget{
+ Hello: Hello{
+ Name: name,
+ ID: id,
+ Version: 1,
+ },
+ Writer: os.Stdout,
+ }
+}
+
+func (w *Widget) Handle() error {
+ if len(os.Args) < 2 {
+ return fmt.Errorf("no method")
+ }
+ switch Method(os.Args[1]) {
+ case MethodHello:
+ return w.Hello.Respond(w.Writer)
+ }
+ return fmt.Errorf("unknown method: %s", os.Args[1])
+}
diff --git a/widget/api/hello.go b/widget/api/hello.go
new file mode 100644
index 0000000..7995a36
--- /dev/null
+++ b/widget/api/hello.go
@@ -0,0 +1,25 @@
+package api
+
+import (
+ "encoding/json"
+ "io"
+)
+
+type Method string
+
+const MethodHello Method = "hello"
+
+type Hello struct {
+ Name string `json:"name"`
+ ID string `json:"id"`
+ Version uint `json:"version"`
+}
+
+func (h *Hello) Respond(w io.Writer) error {
+ b, err := json.Marshal(h)
+ if err != nil {
+ return err
+ }
+ _, err = w.Write(b)
+ return err
+}
diff --git a/widget/widget.go b/widget/widget.go
index fa8a308..29a8f3a 100644
--- a/widget/widget.go
+++ b/widget/widget.go
@@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
+ "now/widget/api"
"os"
"os/exec"
"strings"
@@ -20,14 +21,10 @@ var (
)
type Widget struct {
- Name string `json:"name"`
- ID string `json:"id"`
- Version uint `json:"version"`
- Path string `json:"-"`
+ api.Hello
+ Path string `json:"-"`
}
-type Method string
-
func FromJson(data []byte) (*Widget, error) {
var widget Widget
return &widget, json.Unmarshal(data, &widget)
@@ -71,7 +68,7 @@ func readPluginDir(dir []os.DirEntry, path string) ([]*Widget, error) {
}
c := make(chan any)
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
- go w.Request(ctx, "", c)
+ go w.Request(ctx, api.MethodHello, c)
select {
case <-ctx.Done():
cancel()
@@ -95,7 +92,7 @@ func readPluginDir(dir []os.DirEntry, path string) ([]*Widget, error) {
return widgets, nil
}
-func (w *Widget) Request(_ context.Context, m Method, c chan<- any, args ...string) {
+func (w *Widget) Request(_ context.Context, m api.Method, c chan<- any, args ...string) {
args = append([]string{string(m)}, args...)
cmd := exec.Command(fmt.Sprintf("./plugins/%s", w.Path), args...)
if cmd.Err != nil {