Skip to content

Commit

Permalink
docs: several example improvements (#47)
Browse files Browse the repository at this point in the history
* docs: several example improvements

- logging middleware should probably be the first to run
- some linter issues fixed

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

* fix: order

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

* docs: clarify middlewares a bit

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed May 17, 2022
1 parent b0c88fc commit 8982236
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -27,6 +27,13 @@ and should be easy to integrate into any existing projects.

## Middleware

Wish middlewares are analogous to those in several HTTP frameworks.
They are essentially SSH handlers that you can use to do specific tasks,
and then call the next middleware.

Notice that middlewares are composed from first to last,
which means the last one is executed first.

### Bubble Tea

The [`bubbletea`](bubbletea) middleware makes it easy to serve any
Expand Down
8 changes: 5 additions & 3 deletions examples/bubbletea/main.go
Expand Up @@ -19,8 +19,10 @@ import (
"github.com/gliderlabs/ssh"
)

const host = "localhost"
const port = 23234
const (
host = "localhost"
port = 23234
)

func main() {
s, err := wish.NewServer(
Expand Down Expand Up @@ -56,7 +58,7 @@ func main() {
// You can wire any Bubble Tea model up to the middleware with a function that
// 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
// teaw.WithAltScreen) on a session by session basis
// tea.WithAltScreen) on a session by session basis.
func teaHandler(s ssh.Session) (tea.Model, []tea.ProgramOption) {
pty, _, active := s.Pty()
if !active {
Expand Down
14 changes: 7 additions & 7 deletions examples/bubbleteaprogram/main.go
Expand Up @@ -20,8 +20,10 @@ import (
"github.com/muesli/termenv"
)

const host = "localhost"
const port = 23234
const (
host = "localhost"
port = 23234
)

func main() {
s, err := wish.NewServer(
Expand Down Expand Up @@ -61,10 +63,8 @@ func myCustomBubbleteaMiddleware() wish.Middleware {
p := tea.NewProgram(m, opts...)
go func() {
for {
select {
case <-time.After(1 * time.Second):
p.Send(timeMsg(time.Now()))
}
<-time.After(1 * time.Second)
p.Send(timeMsg(time.Now()))
}
}()
return p
Expand All @@ -73,7 +73,7 @@ func myCustomBubbleteaMiddleware() wish.Middleware {
pty, _, active := s.Pty()
if !active {
fmt.Println("no active terminal, skipping")
s.Exit(1)
_ = s.Exit(1)
return nil
}
m := model{
Expand Down
8 changes: 5 additions & 3 deletions examples/git/main.go
Expand Up @@ -19,9 +19,11 @@ import (
"github.com/gliderlabs/ssh"
)

const port = 23233
const host = "localhost"
const repoDir = ".repos"
const (
port = 23233
host = "localhost"
repoDir = ".repos"
)

type app struct {
access gm.AccessLevel
Expand Down
1 change: 1 addition & 0 deletions options.go
Expand Up @@ -35,6 +35,7 @@ func WithVersion(version string) ssh.Option {
// WithMiddleware composes the provided Middleware and return a ssh.Option.
// This useful if you manually create an ssh.Server and want to set the
// Server.Handler.
//
// Notice that middlewares are composed from first to last, which means the last one is executed first.
func WithMiddleware(mw ...Middleware) ssh.Option {
return func(s *ssh.Server) error {
Expand Down

0 comments on commit 8982236

Please sign in to comment.