repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
c6594eb
parent
ec895f7
author
Eric Bower
date
2024-09-05 04:42:13 +0000 UTC
fix(pubsub): web
7 files changed,  +90, -66
M go.mod
M go.sum
M go.mod
+1, -1
1@@ -36,7 +36,7 @@ require (
2 	github.com/muesli/termenv v0.15.3-0.20240509142007-81b8f94111d5
3 	github.com/neurosnap/go-exif-remove v0.0.0-20221010134343-50d1e3c35577
4 	github.com/picosh/pobj v0.0.0-20240709135546-27097077b26a
5-	github.com/picosh/pubsub v0.0.0-20240905021216-076708fdc9c2
6+	github.com/picosh/pubsub v0.0.0-20240905034711-96ac1473168b
7 	github.com/picosh/send v0.0.0-20240820031602-5d3b1a4494cc
8 	github.com/picosh/tunkit v0.0.0-20240709033345-8315d4f3cd0e
9 	github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06
M go.sum
+4, -0
 1@@ -226,6 +226,10 @@ github.com/picosh/pobj v0.0.0-20240709135546-27097077b26a h1:Cr1xODiyd/SjjBRtYA9
 2 github.com/picosh/pobj v0.0.0-20240709135546-27097077b26a/go.mod h1:VIkR1MZBvxSK2OO47jikxikAO/sKb/vTmXX5ZuYTIvo=
 3 github.com/picosh/pubsub v0.0.0-20240905021216-076708fdc9c2 h1:evyOvl/lZeW06naB1KOuZr6DRxCvbK98IqI5AfYF5pI=
 4 github.com/picosh/pubsub v0.0.0-20240905021216-076708fdc9c2/go.mod h1:FKC8uot+40iXmuDzTfbxYDG5PIc3ghwkmP2iItBKH0I=
 5+github.com/picosh/pubsub v0.0.0-20240905023503-81b6f9d0c296 h1:kNcOi4dfl+EUy56bOWZZ/9FtKnjmTGdxM/s01TUnPJg=
 6+github.com/picosh/pubsub v0.0.0-20240905023503-81b6f9d0c296/go.mod h1:FKC8uot+40iXmuDzTfbxYDG5PIc3ghwkmP2iItBKH0I=
 7+github.com/picosh/pubsub v0.0.0-20240905034711-96ac1473168b h1:GdwZbhoda9K8RpQH8mFovszBvIBH3l8bvTKzelL7FUc=
 8+github.com/picosh/pubsub v0.0.0-20240905034711-96ac1473168b/go.mod h1:FKC8uot+40iXmuDzTfbxYDG5PIc3ghwkmP2iItBKH0I=
 9 github.com/picosh/send v0.0.0-20240820031602-5d3b1a4494cc h1:IIsJuAFG2ju3cygKVKTIsYYZf21q5S3Dr1H4fGbfgJg=
10 github.com/picosh/send v0.0.0-20240820031602-5d3b1a4494cc/go.mod h1:RAgLDK3LrDK6pNeXtU9tjo28obl5DxShcTUk2nm/KCM=
11 github.com/picosh/senpai v0.0.0-20240503200611-af89e73973b0 h1:pBRIbiCj7K6rGELijb//dYhyCo8A3fvxW5dijrJVtjs=
M imgs/cli.go
+4, -4
 1@@ -260,10 +260,10 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
 2 				return
 3 			} else if cmd == "sub" {
 4 				err = pubsub.PubSub.Sub(&psub.Subscriber{
 5-					ID:      uuid.NewString(),
 6-					Name:    fmt.Sprintf("%s@%s", user.Name, repoName),
 7-					Session: sesh,
 8-					Chan:    make(chan error),
 9+					ID:     uuid.NewString(),
10+					Name:   fmt.Sprintf("%s@%s", user.Name, repoName),
11+					Writer: sesh,
12+					Chan:   make(chan error),
13 				})
14 				if err != nil {
15 					wish.Errorln(sesh, err)
M pubsub/cli.go
+49, -54
  1@@ -2,8 +2,10 @@ package pubsub
  2 
  3 import (
  4 	"fmt"
  5+	"io"
  6 	"log/slog"
  7 	"strings"
  8+	"text/tabwriter"
  9 
 10 	"github.com/charmbracelet/ssh"
 11 	"github.com/charmbracelet/wish"
 12@@ -11,11 +13,14 @@ import (
 13 	"github.com/picosh/pico/db"
 14 	"github.com/picosh/pico/shared"
 15 	"github.com/picosh/pico/shared/storage"
 16-	"github.com/picosh/pico/tui/common"
 17 	psub "github.com/picosh/pubsub"
 18 	"github.com/picosh/send/send/utils"
 19 )
 20 
 21+func NewTabWriter(out io.Writer) *tabwriter.Writer {
 22+	return tabwriter.NewWriter(out, 0, 0, 1, ' ', tabwriter.TabIndent)
 23+}
 24+
 25 func getUser(s ssh.Session, dbpool db.DB) (*db.User, error) {
 26 	var err error
 27 	key, err := shared.KeyText(s)
 28@@ -35,42 +40,7 @@ func getUser(s ssh.Session, dbpool db.DB) (*db.User, error) {
 29 	return user, nil
 30 }
 31 
 32-type Cmd struct {
 33-	User    *db.User
 34-	Session shared.CmdSession
 35-	Log     *slog.Logger
 36-	Dbpool  db.DB
 37-	Styles  common.Styles
 38-}
 39-
 40-func (c *Cmd) output(out string) {
 41-	_, _ = c.Session.Write([]byte(out + "\r\n"))
 42-}
 43-
 44-func (c *Cmd) error(err error) {
 45-	_, _ = fmt.Fprint(c.Session.Stderr(), err, "\r\n")
 46-	_ = c.Session.Exit(1)
 47-	_ = c.Session.Close()
 48-}
 49-
 50-func (c *Cmd) bail(err error) {
 51-	if err == nil {
 52-		return
 53-	}
 54-	c.Log.Error(err.Error())
 55-	c.error(err)
 56-}
 57-
 58-func (c *Cmd) help() {
 59-	helpStr := "Commands: [pub, sub, ls]\n"
 60-	c.output(helpStr)
 61-}
 62-
 63-func (c *Cmd) ls() error {
 64-	helpStr := "TODO\n"
 65-	c.output(helpStr)
 66-	return nil
 67-}
 68+var helpStr = "Commands: [pub, sub, ls]\n"
 69 
 70 type CliHandler struct {
 71 	DBPool      db.DB
 72@@ -95,15 +65,8 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
 73 
 74 			args := sesh.Command()
 75 
 76-			opts := Cmd{
 77-				Session: sesh,
 78-				User:    user,
 79-				Log:     log,
 80-				Dbpool:  dbpool,
 81-			}
 82-
 83 			if len(args) == 0 {
 84-				opts.help()
 85+				wish.Println(sesh, helpStr)
 86 				next(sesh)
 87 				return
 88 			}
 89@@ -111,10 +74,24 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
 90 			cmd := strings.TrimSpace(args[0])
 91 			if len(args) == 1 {
 92 				if cmd == "help" {
 93-					opts.help()
 94+					wish.Println(sesh, helpStr)
 95 				} else if cmd == "ls" {
 96-					err := opts.ls()
 97-					opts.bail(err)
 98+					subs := pubsub.PubSub.GetSubs()
 99+
100+					if len(subs) == 0 {
101+						wish.Println(sesh, "no subs found")
102+					} else {
103+						writer := NewTabWriter(sesh)
104+						fmt.Fprintln(writer, "Channel\tID")
105+						for _, sub := range subs {
106+							fmt.Fprintf(
107+								writer,
108+								"%s\t%s\n",
109+								sub.Name, sub.ID,
110+							)
111+						}
112+						writer.Flush()
113+					}
114 				}
115 				next(sesh)
116 				return
117@@ -131,19 +108,37 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
118 			)
119 
120 			if cmd == "pub" {
121-				err = pubsub.PubSub.Pub(&psub.Msg{
122+				wish.Println(sesh, "sending msg ...")
123+				msg := &psub.Msg{
124 					Name:   fmt.Sprintf("%s@%s", user.Name, repoName),
125 					Reader: sesh,
126-				})
127+				}
128+
129+				// hacky: we want to notify when no subs are found so
130+				// we duplicate some logic for now
131+				subs := pubsub.PubSub.GetSubs()
132+				found := false
133+				for _, sub := range subs {
134+					if pubsub.PubSub.PubMatcher(msg, sub) {
135+						found = true
136+						break
137+					}
138+				}
139+				if !found {
140+					wish.Println(sesh, "no subs found ... waiting")
141+				}
142+
143+				err = pubsub.PubSub.Pub(msg)
144+				wish.Println(sesh, "msg sent!")
145 				if err != nil {
146 					wish.Errorln(sesh, err)
147 				}
148 			} else if cmd == "sub" {
149 				err = pubsub.PubSub.Sub(&psub.Subscriber{
150-					ID:      uuid.NewString(),
151-					Name:    fmt.Sprintf("%s@%s", user.Name, repoName),
152-					Session: sesh,
153-					Chan:    make(chan error),
154+					ID:     uuid.NewString(),
155+					Name:   fmt.Sprintf("%s@%s", user.Name, repoName),
156+					Writer: sesh,
157+					Chan:   make(chan error),
158 				})
159 				if err != nil {
160 					wish.Errorln(sesh, err)
M pubsub/config.go
+1, -0
1@@ -16,5 +16,6 @@ func NewConfigSite() *shared.ConfigSite {
2 		Protocol: protocol,
3 		DbURL:    dbURL,
4 		Logger:   shared.CreateLogger(),
5+		Space:    "pubsub",
6 	}
7 }
M pubsub/html/base.layout.tmpl
+1, -1
1@@ -11,7 +11,7 @@
2         <meta name="keywords" content="pastebin, paste, copy, snippets" />
3         {{template "meta" .}}
4 
5-        <link rel="stylesheet" href="/smol.css" />
6+        <link rel="stylesheet" href="https://pico.sh/smol.css" />
7     </head>
8     <body {{template "attrs" .}}>{{template "body" .}}</body>
9 </html>
M pubsub/html/marketing.page.tmpl
+30, -6
 1@@ -24,15 +24,39 @@
 2 <meta property="og:image" content="https://{{.Site.Domain}}/card.png" />
 3 {{end}}
 4 
 5-{{define "attrs"}}{{end}}
 6+{{define "attrs"}}class="container-sm"{{end}}
 7 
 8 {{define "body"}}
 9-<header class="text-center">
10-    <h1 class="text-2xl font-bold">{{.Site.Domain}}</h1>
11-    <p class="text-lg">pubsub using ssh</p>
12-    <pre>ssh {{.Site.Domain}} sub mykey</pre>
13-    <pre>echo "hello world!" | ssh {{.Site.Domain}} pub mykey</pre>
14+<header>
15+  <h1 class="text-2xl font-bold text-center">pubsub using ssh</h1>
16 </header>
17 
18+<hr />
19+
20+<article>
21+  <p>
22+    The simplest authenticated pubsub system.  Send messages through
23+    user-defined channels.  Channels are private to the authenticated
24+    ssh user.  The default pubsub model is multicast, meaning a publisher
25+    (<code>pub</code>) will send its message to all subscribers
26+    (<code>sub</code>).  Further, the <code>pub</code> will wait for a
27+    valid <code>sub</code> before exiting.
28+  </p>
29+  <div>
30+    <pre>ssh {{.Site.Domain}} sub mykey</pre>
31+    <pre>echo "hello world!" | ssh {{.Site.Domain}} pub mykey
32+# or
33+cat important.md | ssh {{.Site.Domain}} pub mykey</pre>
34+  </div>
35+  <div>
36+    <p>Compose automated scripts using unix pipes:</p>
37+    <pre>ssh {{.Site.Domain}} sub "alpine:latest" | docker pull alpine:latest && docker up -d dev</pre>
38+    <pre>docker build -t alpine:latest . | ssh {{.Site.Domain}} pub "alpine:latest"</pre>
39+  </div>
40+  <div class="text-center">
41+    <a href="https://pico.sh/getting-started" class="btn-link my-2 inline-block">GET STARTED</a>
42+  </div>
43+</article>
44+
45 {{template "marketing-footer" .}}
46 {{end}}