Skip to content

Commit

Permalink
refactor: type aliasing ssh types (#94)
Browse files Browse the repository at this point in the history
* refactor: type aliasing ssh types

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: gossh imports

* test: fix git tests

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Nov 17, 2022
1 parent 45af061 commit f7f0c95
Show file tree
Hide file tree
Showing 36 changed files with 300 additions and 241 deletions.
5 changes: 2 additions & 3 deletions accesscontrol/accesscontrol.go
Expand Up @@ -5,14 +5,13 @@ import (
"fmt"

"github.com/charmbracelet/wish"
"github.com/gliderlabs/ssh"
)

// Middleware will exit 1 connections trying to execute commands that are not allowed.
// If no allowed commands are provided, no commands will be allowed.
func Middleware(cmds ...string) wish.Middleware {
return func(sh ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
return func(sh wish.Handler) wish.Handler {
return func(s wish.Session) {
if len(s.Command()) == 0 {
sh(s)
return
Expand Down
10 changes: 5 additions & 5 deletions accesscontrol/accesscontrol_test.go
Expand Up @@ -4,10 +4,10 @@ import (
"fmt"
"testing"

"github.com/charmbracelet/wish"
"github.com/charmbracelet/wish/accesscontrol"
"github.com/charmbracelet/wish/testsession"
"github.com/gliderlabs/ssh"
gossh "golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh"
)

const out = "hello world"
Expand Down Expand Up @@ -77,10 +77,10 @@ func TestMiddleware(t *testing.T) {
})
}

func setup(tb testing.TB, allowedCmds ...string) *gossh.Session {
func setup(tb testing.TB, allowedCmds ...string) *ssh.Session {
tb.Helper()
return testsession.New(tb, &ssh.Server{
Handler: accesscontrol.Middleware(allowedCmds...)(func(s ssh.Session) {
return testsession.New(tb, &wish.Server{
Handler: accesscontrol.Middleware(allowedCmds...)(func(s wish.Session) {
s.Write([]byte(out))
}),
}, nil)
Expand Down
5 changes: 2 additions & 3 deletions activeterm/activeterm.go
Expand Up @@ -5,13 +5,12 @@ import (
"fmt"

"github.com/charmbracelet/wish"
"github.com/gliderlabs/ssh"
)

// Middleware will exit 1 connections trying with no active terminals.
func Middleware() wish.Middleware {
return func(sh ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
return func(sh wish.Handler) wish.Handler {
return func(s wish.Session) {
_, _, active := s.Pty()
if !active {
fmt.Fprintln(s, "Requires an active PTY")
Expand Down
10 changes: 5 additions & 5 deletions activeterm/activeterm_test.go
Expand Up @@ -3,10 +3,10 @@ package activeterm_test
import (
"testing"

"github.com/charmbracelet/wish"
"github.com/charmbracelet/wish/activeterm"
"github.com/charmbracelet/wish/testsession"
"github.com/gliderlabs/ssh"
gossh "golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh"
)

func TestMiddleware(t *testing.T) {
Expand All @@ -21,10 +21,10 @@ func TestMiddleware(t *testing.T) {
})
}

func setup(tb testing.TB) *gossh.Session {
func setup(tb testing.TB) *ssh.Session {
tb.Helper()
return testsession.New(tb, &ssh.Server{
Handler: activeterm.Middleware()(func(s ssh.Session) {
return testsession.New(tb, &wish.Server{
Handler: activeterm.Middleware()(func(s wish.Session) {
s.Write([]byte("hello"))
}),
}, nil)
Expand Down
11 changes: 5 additions & 6 deletions bubbletea/tea.go
Expand Up @@ -7,7 +7,6 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/wish"
"github.com/gliderlabs/ssh"
"github.com/muesli/termenv"
)

Expand All @@ -21,7 +20,7 @@ type BubbleTeaHandler = Handler // nolint: revive
// Handler is the function Bubble Tea apps implement to hook into the
// SSH Middleware. This will create a new tea.Program for every connection and
// start it with the tea.ProgramOptions returned.
type Handler func(ssh.Session) (tea.Model, []tea.ProgramOption)
type Handler func(wish.Session) (tea.Model, []tea.ProgramOption)

// ProgramHandler is the function Bubble Tea apps implement to hook into the SSH
// Middleware. This should return a new tea.Program. This handler is different
Expand All @@ -30,7 +29,7 @@ type Handler func(ssh.Session) (tea.Model, []tea.ProgramOption)
//
// Make sure to set the tea.WithInput and tea.WithOutput to the ssh.Session
// otherwise the program will not function properly.
type ProgramHandler func(ssh.Session) *tea.Program
type ProgramHandler func(wish.Session) *tea.Program

// Middleware takes a Handler and hooks the input and output for the
// ssh.Session into the tea.Program. It also captures window resize events and
Expand All @@ -45,7 +44,7 @@ func Middleware(bth Handler) wish.Middleware {
// by an SSH client's terminal cannot be detected by the server but this will
// allow for manually setting the color profile on all SSH connections.
func MiddlewareWithColorProfile(bth Handler, cp termenv.Profile) wish.Middleware {
h := func(s ssh.Session) *tea.Program {
h := func(s wish.Session) *tea.Program {
m, opts := bth(s)
if m == nil {
return nil
Expand All @@ -64,9 +63,9 @@ func MiddlewareWithColorProfile(bth Handler, cp termenv.Profile) wish.Middleware
// Make sure to set the tea.WithInput and tea.WithOutput to the ssh.Session
// otherwise the program will not function properly.
func MiddlewareWithProgramHandler(bth ProgramHandler, cp termenv.Profile) wish.Middleware {
return func(sh ssh.Handler) ssh.Handler {
return func(sh wish.Handler) wish.Handler {
lipgloss.SetColorProfile(cp)
return func(s ssh.Session) {
return func(s wish.Session) {
p := bth(s)
if p != nil {
_, windowChanges, _ := s.Pty()
Expand Down
9 changes: 3 additions & 6 deletions comment/comment.go
@@ -1,14 +1,11 @@
package comment

import (
"github.com/charmbracelet/wish"
"github.com/gliderlabs/ssh"
)
import "github.com/charmbracelet/wish"

// Middleware prints a comment at the end of the session.
func Middleware(comment string) wish.Middleware {
return func(sh ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
return func(sh wish.Handler) wish.Handler {
return func(s wish.Session) {
sh(s)
wish.Println(s, comment)
}
Expand Down
10 changes: 5 additions & 5 deletions comment/comment_test.go
Expand Up @@ -3,9 +3,9 @@ package comment
import (
"testing"

"github.com/charmbracelet/wish"
"github.com/charmbracelet/wish/testsession"
"github.com/gliderlabs/ssh"
gossh "golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh"
)

func TestMiddleware(t *testing.T) {
Expand All @@ -18,10 +18,10 @@ func TestMiddleware(t *testing.T) {
})
}

func setup(tb testing.TB) *gossh.Session {
func setup(tb testing.TB) *ssh.Session {
tb.Helper()
return testsession.New(tb, &ssh.Server{
Handler: Middleware("test")(func(s ssh.Session) {}),
return testsession.New(tb, &wish.Server{
Handler: Middleware("test")(func(s wish.Session) {}),
}, nil)
}

Expand Down
5 changes: 2 additions & 3 deletions elapsed/elapsed.go
Expand Up @@ -4,16 +4,15 @@ import (
"time"

"github.com/charmbracelet/wish"
"github.com/gliderlabs/ssh"
)

// MiddlewareWithFormat returns a middleware that logs the elapsed time of the
// session. It accepts a format string to print the elapsed time.
//
// This must be called as the last middleware in the chain.
func MiddlewareWithFormat(format string) wish.Middleware {
return func(sh ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
return func(sh wish.Handler) wish.Handler {
return func(s wish.Session) {
now := time.Now()
sh(s)
wish.Printf(s, format, time.Since(now))
Expand Down
14 changes: 6 additions & 8 deletions elapsed/elapsed_test.go
Expand Up @@ -4,14 +4,12 @@ import (
"testing"
"time"

"github.com/charmbracelet/wish"
"github.com/charmbracelet/wish/testsession"
"github.com/gliderlabs/ssh"
gossh "golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh"
)

var (
waitDuration = time.Second
)
var waitDuration = time.Second

func TestMiddleware(t *testing.T) {
t.Run("recover session", func(t *testing.T) {
Expand All @@ -25,10 +23,10 @@ func TestMiddleware(t *testing.T) {
})
}

func setup(tb testing.TB) *gossh.Session {
func setup(tb testing.TB) *ssh.Session {
tb.Helper()
return testsession.New(tb, &ssh.Server{
Handler: MiddlewareWithFormat("%v")(func(s ssh.Session) {
return testsession.New(tb, &wish.Server{
Handler: MiddlewareWithFormat("%v")(func(s wish.Session) {
time.Sleep(waitDuration)
}),
}, nil)
Expand Down
3 changes: 1 addition & 2 deletions examples/bubbletea/main.go
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/charmbracelet/wish"
bm "github.com/charmbracelet/wish/bubbletea"
lm "github.com/charmbracelet/wish/logging"
"github.com/gliderlabs/ssh"
)

const (
Expand Down Expand Up @@ -59,7 +58,7 @@ func main() {
// handles the incoming ssh.Session. Here we just grab the terminal info and
// pass it to the new model. You can also return tea.ProgramOptions (such as
// tea.WithAltScreen) on a session by session basis.
func teaHandler(s ssh.Session) (tea.Model, []tea.ProgramOption) {
func teaHandler(s wish.Session) (tea.Model, []tea.ProgramOption) {
pty, _, active := s.Pty()
if !active {
wish.Fatalln(s, "no active terminal, skipping")
Expand Down
3 changes: 1 addition & 2 deletions examples/bubbleteaprogram/main.go
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/charmbracelet/wish"
bm "github.com/charmbracelet/wish/bubbletea"
lm "github.com/charmbracelet/wish/logging"
"github.com/gliderlabs/ssh"
"github.com/muesli/termenv"
)

Expand Down Expand Up @@ -69,7 +68,7 @@ func myCustomBubbleteaMiddleware() wish.Middleware {
}()
return p
}
teaHandler := func(s ssh.Session) *tea.Program {
teaHandler := func(s wish.Session) *tea.Program {
pty, _, active := s.Pty()
if !active {
wish.Fatalln(s, "no active terminal, skipping")
Expand Down
5 changes: 2 additions & 3 deletions examples/cobra/main.go
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/charmbracelet/wish"
"github.com/charmbracelet/wish/logging"
"github.com/gliderlabs/ssh"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -48,8 +47,8 @@ func main() {
wish.WithAddress(fmt.Sprintf("%s:%d", host, port)),
wish.WithHostKeyPath(".ssh/term_info_ed25519"),
wish.WithMiddleware(
func(h ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
func(h wish.Handler) wish.Handler {
return func(s wish.Session) {
rootCmd := cmd()
rootCmd.SetArgs(s.Command())
rootCmd.SetIn(s)
Expand Down
19 changes: 9 additions & 10 deletions examples/git/main.go
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/charmbracelet/wish"
gm "github.com/charmbracelet/wish/git"
lm "github.com/charmbracelet/wish/logging"
"github.com/gliderlabs/ssh"
)

const (
Expand All @@ -29,23 +28,23 @@ type app struct {
access gm.AccessLevel
}

func (a app) AuthRepo(repo string, pk ssh.PublicKey) gm.AccessLevel {
func (a app) AuthRepo(repo string, pk wish.PublicKey) gm.AccessLevel {
return a.access
}

func (a app) Push(repo string, pk ssh.PublicKey) {
func (a app) Push(repo string, pk wish.PublicKey) {
log.Printf("pushed %s", repo)
}

func (a app) Fetch(repo string, pk ssh.PublicKey) {
func (a app) Fetch(repo string, pk wish.PublicKey) {
log.Printf("fetch %s", repo)
}

func passHandler(ctx ssh.Context, password string) bool {
func passHandler(ctx wish.Context, password string) bool {
return false
}

func pkHandler(ctx ssh.Context, key ssh.PublicKey) bool {
func pkHandler(ctx wish.Context, key wish.PublicKey) bool {
return true
}

Expand All @@ -54,8 +53,8 @@ func main() {
a := app{gm.ReadWriteAccess}

s, err := wish.NewServer(
ssh.PublicKeyAuth(pkHandler),
ssh.PasswordAuth(passHandler),
wish.WithPublicKeyAuth(pkHandler),
wish.WithPasswordAuth(passHandler),
wish.WithAddress(fmt.Sprintf("%s:%d", host, port)),
wish.WithHostKeyPath(".ssh/git_server_ed25519"),
wish.WithMiddleware(
Expand Down Expand Up @@ -89,8 +88,8 @@ func main() {
// Normally we would use a Bubble Tea program for the TUI but for simplicity,
// we'll just write a list of the pushed repos to the terminal and exit the ssh
// session.
func gitListMiddleware(h ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
func gitListMiddleware(h wish.Handler) wish.Handler {
return func(s wish.Session) {
// Git will have a command included so only run this if there are no
// commands passed to ssh.
if len(s.Command()) == 0 {
Expand Down
11 changes: 6 additions & 5 deletions examples/go.mod
Expand Up @@ -3,23 +3,25 @@ module examples
go 1.18

require (
github.com/charmbracelet/bubbletea v0.22.1
github.com/charmbracelet/bubbletea v0.23.1
github.com/charmbracelet/wish v0.5.0
github.com/gliderlabs/ssh v0.3.5
github.com/muesli/termenv v0.12.0
github.com/muesli/termenv v0.13.0
github.com/spf13/cobra v1.5.0
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d
)

require (
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect
github.com/aymanbagabas/go-osc52 v1.0.3 // indirect
github.com/caarlos0/sshmarshal v0.1.0 // indirect
github.com/charmbracelet/keygen v0.3.0 // indirect
github.com/charmbracelet/lipgloss v0.6.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/gliderlabs/ssh v0.3.5 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-git/go-git/v5 v5.4.2 // indirect
Expand All @@ -30,7 +32,7 @@ require (
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
Expand All @@ -39,7 +41,6 @@ require (
github.com/sergi/go-diff v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d // indirect
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b // indirect
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 // indirect
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
Expand Down

0 comments on commit f7f0c95

Please sign in to comment.