~erock/pico

893485f1dcbef4ed04c4e294675c11ff45b264ea — Eric Bower a month ago 3583e4a
feat: add ability to pipe to any service

Implements https://todo.sr.ht/~erock/pico.sh/47
4 files changed, 76 insertions(+), 61 deletions(-)

M cmd/lists/ssh/main.go
M cmd/pastes/ssh/main.go
M cmd/prose/ssh/main.go
A wish/pipe/pipe.go
M cmd/lists/ssh/main.go => cmd/lists/ssh/main.go +5 -3
@@ 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"


@@ 34,13 35,14 @@ func createRouter(handler *filehandlers.ScpUploadHandler) proxy.Router {
		cmd := s.Command()
		mdw := []wish.Middleware{}

		if len(cmd) == 0 {
		if len(cmd) > 0 && cmd[0] == "scp" {
			mdw = append(mdw, scp.Middleware(handler))
		} else {
			mdw = append(mdw,
				pipe.Middleware(handler, ".txt"),
				bm.Middleware(cms.Middleware(&handler.Cfg.ConfigCms, handler.Cfg)),
				lm.Middleware(),
			)
		} else if cmd[0] == "scp" {
			mdw = append(mdw, scp.Middleware(handler))
		}

		return mdw

M cmd/pastes/ssh/main.go => cmd/pastes/ssh/main.go +2 -55
@@ 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)

M cmd/prose/ssh/main.go => cmd/prose/ssh/main.go +5 -3
@@ 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"


@@ 34,13 35,14 @@ func createRouter(handler *filehandlers.ScpUploadHandler) proxy.Router {
		cmd := s.Command()
		mdw := []wish.Middleware{}

		if len(cmd) == 0 {
		if len(cmd) > 0 && cmd[0] == "scp" {
			mdw = append(mdw, scp.Middleware(handler))
		} else {
			mdw = append(mdw,
				pipe.Middleware(handler, ".md"),
				bm.Middleware(cms.Middleware(&handler.Cfg.ConfigCms, handler.Cfg)),
				lm.Middleware(),
			)
		} else if cmd[0] == "scp" {
			mdw = append(mdw, scp.Middleware(handler))
		}

		return mdw

A wish/pipe/pipe.go => wish/pipe/pipe.go +64 -0
@@ 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, ext string) 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 = fmt.Sprintf("%s%s", strconv.Itoa(int(postTime.UnixNano())), ext)
			}

			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)
		}
	}
}