~erock/pico.sh

feat: add ability to pipe to any service v1 PROPOSED

Eric Bower: 1
 feat: add ability to pipe to any service

 4 files changed, 70 insertions(+), 55 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~erock/pico.sh/patches/34572/mbox | git am -3
Learn more about email & git

[PATCH v1] feat: add ability to pipe to any service Export this patch

Implements https://todo.sr.ht/~erock/pico.sh/47
---
 cmd/lists/ssh/main.go  |  2 ++
 cmd/pastes/ssh/main.go | 57 ++-----------------------------------
 cmd/prose/ssh/main.go  |  2 ++
 wish/pipe/pipe.go      | 64 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 70 insertions(+), 55 deletions(-)
 create mode 100644 wish/pipe/pipe.go

diff --git a/cmd/lists/ssh/main.go b/cmd/lists/ssh/main.go
index 2137f65..965f690 100644
--- a/cmd/lists/ssh/main.go
+++ b/cmd/lists/ssh/main.go
@@ -13,6 +13,7 @@ import (
	"git.sr.ht/~erock/pico/lists"
	"git.sr.ht/~erock/pico/shared"
	"git.sr.ht/~erock/pico/wish/cms"
	"git.sr.ht/~erock/pico/wish/pipe"
	"git.sr.ht/~erock/pico/wish/proxy"
	"git.sr.ht/~erock/pico/wish/send/scp"
	"git.sr.ht/~erock/pico/wish/send/sftp"
@@ -36,6 +37,7 @@ func createRouter(handler *filehandlers.ScpUploadHandler) proxy.Router {

		if len(cmd) == 0 {
			mdw = append(mdw,
				pipe.Middleware(handler),
				bm.Middleware(cms.Middleware(&handler.Cfg.ConfigCms, handler.Cfg)),
				lm.Middleware(),
			)
diff --git a/cmd/pastes/ssh/main.go b/cmd/pastes/ssh/main.go
index c45b307..0d8762b 100644
--- a/cmd/pastes/ssh/main.go
+++ b/cmd/pastes/ssh/main.go
@@ -3,11 +3,8 @@ package main
import (
	"context"
	"fmt"
	"io/fs"
	"os"
	"os/signal"
	"strconv"
	"strings"
	"syscall"
	"time"

@@ -16,10 +13,10 @@ import (
	"git.sr.ht/~erock/pico/pastes"
	"git.sr.ht/~erock/pico/shared"
	"git.sr.ht/~erock/pico/wish/cms"
	"git.sr.ht/~erock/pico/wish/pipe"
	"git.sr.ht/~erock/pico/wish/proxy"
	"git.sr.ht/~erock/pico/wish/send/scp"
	"git.sr.ht/~erock/pico/wish/send/sftp"
	"git.sr.ht/~erock/pico/wish/send/utils"
	"github.com/charmbracelet/promwish"
	"github.com/charmbracelet/wish"
	bm "github.com/charmbracelet/wish/bubbletea"
@@ -42,7 +39,7 @@ func createRouter(handler *filehandlers.ScpUploadHandler) proxy.Router {
			mdw = append(mdw, scp.Middleware(handler))
		} else {
			mdw = append(mdw,
				pasteMiddleware(handler),
				pipe.Middleware(handler),
				bm.Middleware(cms.Middleware(&handler.Cfg.ConfigCms, handler.Cfg)),
				lm.Middleware(),
			)
@@ -52,56 +49,6 @@ func createRouter(handler *filehandlers.ScpUploadHandler) proxy.Router {
	}
}

func pasteMiddleware(writeHandler *filehandlers.ScpUploadHandler) wish.Middleware {
	return func(sshHandler ssh.Handler) ssh.Handler {
		return func(session ssh.Session) {
			_, _, activePty := session.Pty()
			if activePty {
				_ = session.Exit(0)
				_ = session.Close()
				return
			}

			err := writeHandler.Validate(session)
			if err != nil {
				utils.ErrorHandler(session, err)
				return
			}

			name := strings.TrimSpace(strings.Join(session.Command(), " "))
			postTime := time.Now()

			if name == "" {
				name = strconv.Itoa(int(postTime.UnixNano()))
			}

			result, err := writeHandler.Write(session, &utils.FileEntry{
				Name:     name,
				Filepath: name,
				Mode:     fs.FileMode(0777),
				Size:     0,
				Mtime:    postTime.Unix(),
				Atime:    postTime.Unix(),
				Reader:   session,
			})
			if err != nil {
				utils.ErrorHandler(session, err)
				return
			}

			if result != "" {
				_, err = session.Write([]byte(fmt.Sprintf("%s\n", result)))
				if err != nil {
					utils.ErrorHandler(session, err)
				}
				return
			}

			sshHandler(session)
		}
	}
}

func withProxy(handler *filehandlers.ScpUploadHandler, otherMiddleware ...wish.Middleware) ssh.Option {
	return func(server *ssh.Server) error {
		err := sftp.SSHOption(handler)(server)
diff --git a/cmd/prose/ssh/main.go b/cmd/prose/ssh/main.go
index fdad023..15dbaf8 100644
--- a/cmd/prose/ssh/main.go
+++ b/cmd/prose/ssh/main.go
@@ -13,6 +13,7 @@ import (
	"git.sr.ht/~erock/pico/prose"
	"git.sr.ht/~erock/pico/shared"
	"git.sr.ht/~erock/pico/wish/cms"
	"git.sr.ht/~erock/pico/wish/pipe"
	"git.sr.ht/~erock/pico/wish/proxy"
	"git.sr.ht/~erock/pico/wish/send/scp"
	"git.sr.ht/~erock/pico/wish/send/sftp"
@@ -36,6 +37,7 @@ func createRouter(handler *filehandlers.ScpUploadHandler) proxy.Router {

		if len(cmd) == 0 {
			mdw = append(mdw,
				pipe.Middleware(handler),
				bm.Middleware(cms.Middleware(&handler.Cfg.ConfigCms, handler.Cfg)),
				lm.Middleware(),
			)
diff --git a/wish/pipe/pipe.go b/wish/pipe/pipe.go
new file mode 100644
index 0000000..fcbfe25
--- /dev/null
+++ b/wish/pipe/pipe.go
@@ -0,0 +1,64 @@
package pipe

import (
	"fmt"
	"io/fs"
	"strconv"
	"strings"
	"time"

	"git.sr.ht/~erock/pico/filehandlers"
	"git.sr.ht/~erock/pico/wish/send/utils"
	"github.com/charmbracelet/wish"
	"github.com/gliderlabs/ssh"
)

func Middleware(writeHandler *filehandlers.ScpUploadHandler) wish.Middleware {
	return func(sshHandler ssh.Handler) ssh.Handler {
		return func(session ssh.Session) {
			_, _, activePty := session.Pty()
			if activePty {
				_ = session.Exit(0)
				_ = session.Close()
				return
			}

			err := writeHandler.Validate(session)
			if err != nil {
				utils.ErrorHandler(session, err)
				return
			}

			name := strings.TrimSpace(strings.Join(session.Command(), " "))
			postTime := time.Now()

			if name == "" {
				name = strconv.Itoa(int(postTime.UnixNano()))
			}

			result, err := writeHandler.Write(session, &utils.FileEntry{
				Name:     name,
				Filepath: name,
				Mode:     fs.FileMode(0777),
				Size:     0,
				Mtime:    postTime.Unix(),
				Atime:    postTime.Unix(),
				Reader:   session,
			})
			if err != nil {
				utils.ErrorHandler(session, err)
				return
			}

			if result != "" {
				_, err = session.Write([]byte(fmt.Sprintf("%s\n", result)))
				if err != nil {
					utils.ErrorHandler(session, err)
				}
				return
			}

			sshHandler(session)
		}
	}
}
-- 
2.37.1