- commit
- aeccda9
- parent
- 7b8f68e
- author
- Eric Bower
- date
- 2024-09-08 15:23:52 +0000 UTC
feat(pubsub): flag for empty messages
2 files changed,
+49,
-10
+33,
-2
1@@ -1,6 +1,7 @@
2 package pubsub
3
4 import (
5+ "flag"
6 "fmt"
7 "io"
8 "log/slog"
9@@ -17,6 +18,22 @@ import (
10 "github.com/picosh/send/send/utils"
11 )
12
13+func flagSet(cmdName string, sesh ssh.Session) *flag.FlagSet {
14+ cmd := flag.NewFlagSet(cmdName, flag.ContinueOnError)
15+ cmd.SetOutput(sesh)
16+ return cmd
17+}
18+
19+func flagCheck(cmd *flag.FlagSet, posArg string, cmdArgs []string) bool {
20+ _ = cmd.Parse(cmdArgs)
21+
22+ if posArg == "-h" || posArg == "--help" || posArg == "-help" {
23+ cmd.Usage()
24+ return false
25+ }
26+ return true
27+}
28+
29 func NewTabWriter(out io.Writer) *tabwriter.Writer {
30 return tabwriter.NewWriter(out, 0, 0, 1, ' ', tabwriter.TabIndent)
31 }
32@@ -138,10 +155,24 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
33 )
34
35 if cmd == "pub" {
36+ pubCmd := flagSet("pub", sesh)
37+ empty := pubCmd.Bool("e", false, "Send an empty message to subs")
38+ if !flagCheck(pubCmd, repoName, cmdArgs) {
39+ return
40+ }
41+ channelName := repoName
42+
43+ var reader io.Reader
44+ if *empty {
45+ reader = strings.NewReader("")
46+ } else {
47+ reader = sesh
48+ }
49+
50 wish.Println(sesh, "sending msg ...")
51 msg := &psub.Msg{
52- Name: toChannel(user.Name, repoName),
53- Reader: sesh,
54+ Name: toChannel(user.Name, channelName),
55+ Reader: reader,
56 }
57
58 // hacky: we want to notify when no subs are found so
+16,
-8
1@@ -42,33 +42,41 @@
2 subscribers (<code>sub</code>). Further, both <code>pub</code> and
3 <code>sub</code> will wait for at least one event to be sent or received.
4 </p>
5+
6 <div>
7+ <h2 class="text-xl">A basic API</h2>
8 <pre>ssh {{.Site.Domain}} sub mykey</pre>
9- <pre>echo "hello world!" | ssh {{.Site.Domain}} pub mykey
10-# or
11-cat important.md | ssh {{.Site.Domain}} pub mykey</pre>
12+ <pre>echo "hello world!" | ssh {{.Site.Domain}} pub mykey</pre>
13 </div>
14
15 <div>
16 <h2 class="text-xl">Simple desktop notifications</h2>
17- <pre>ssh {{.Site.Domain}} sub notify | notify-send "Job done!"</pre>
18- <pre>./longjob.sh; ssh {{.Site.Domain}} pub notify</pre>
19+ <pre>ssh {{.Site.Domain}} sub notify | notify-send</pre>
20+ <pre>./longjob.sh; echo "Job done!" | ssh {{.Site.Domain}} pub notify</pre>
21 </div>
22
23 <div>
24 <h2 class="text-xl">Simple CI/CD</h2>
25 <pre>ssh {{.Site.Domain}} sub myimg | docker pull myimg && docker up -d dev</pre>
26- <pre>docker build -t myimg .; docker push myimg; ssh {{.Site.Domain}} pub myimg</pre>
27+ <pre>docker buildx build --push -t myimg .; ssh {{.Site.Domain}} pub myimg</pre>
28 </div>
29
30- <div class="text-center">
31- <a href="https://pico.sh/getting-started" class="btn-link my-2 inline-block">GET STARTED</a>
32+ <div>
33+ <h2 class="text-xl">Caveats</h2>
34+ <p>
35+ You must always pipe something into <code>pub</code> or else it will block
36+ indefinitely until the process is killed.
37+ </p>
38 </div>
39
40 <p>
41 This project was heavily inspired by
42 <a href="https://patchbay.pub">patchbay.pub</a>
43 </p>
44+
45+ <div class="text-center">
46+ <a href="https://pico.sh/getting-started" class="btn-link my-2 inline-block">GET STARTED</a>
47+ </div>
48 </article>
49
50 {{template "marketing-footer" .}}