Skip to content

Commit

Permalink
feat: add print/fatal/error convenience methods
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed May 24, 2022
1 parent 8982236 commit 92de95f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -11,3 +11,6 @@ examples/git/.repos
.repos
.ssh
coverage.txt

# MacOS specific
.DS_Store
24 changes: 13 additions & 11 deletions git/git.go
@@ -1,6 +1,7 @@
package git

import (
"errors"
"fmt"
"os"
"os/exec"
Expand All @@ -13,13 +14,13 @@ import (
)

// ErrNotAuthed represents unauthorized access.
var ErrNotAuthed = fmt.Errorf("you are not authorized to do this")
var ErrNotAuthed = errors.New("you are not authorized to do this")

// ErrSystemMalfunction represents a general system error returned to clients.
var ErrSystemMalfunction = fmt.Errorf("something went wrong")
var ErrSystemMalfunction = errors.New("something went wrong")

// ErrInvalidRepo represents an attempt to access a non-existent repo.
var ErrInvalidRepo = fmt.Errorf("invalid repo")
var ErrInvalidRepo = errors.New("invalid repo")

// AccessLevel is the level of access allowed to a repo.
type AccessLevel int
Expand Down Expand Up @@ -78,12 +79,12 @@ func Middleware(repoDir string, gh Hooks) wish.Middleware {
case ReadWriteAccess, AdminAccess:
err := gitReceivePack(s, gc, repoDir, repo)
if err != nil {
fatalGit(s, ErrSystemMalfunction)
Fatal(s, ErrSystemMalfunction)
} else {
gh.Push(repo, pk)
}
default:
fatalGit(s, ErrNotAuthed)
Fatal(s, ErrNotAuthed)
}
return
case "git-upload-archive", "git-upload-pack":
Expand All @@ -92,14 +93,14 @@ func Middleware(repoDir string, gh Hooks) wish.Middleware {
err := gitUploadPack(s, gc, repoDir, repo)
switch err {
case ErrInvalidRepo:
fatalGit(s, ErrInvalidRepo)
Fatal(s, ErrInvalidRepo)
case nil:
gh.Fetch(repo, pk)
default:
fatalGit(s, ErrSystemMalfunction)
Fatal(s, ErrSystemMalfunction)
}
default:
fatalGit(s, ErrNotAuthed)
Fatal(s, ErrNotAuthed)
}
return
}
Expand Down Expand Up @@ -157,11 +158,12 @@ func fileExists(path string) (bool, error) {
return true, err
}

func fatalGit(s ssh.Session, err error) {
// Fatal prints to the session's STDOUT as a git response and exit 1.
func Fatal(s ssh.Session, v ...interface{}) {
msg := fmt.Sprint(v...)
// hex length includes 4 byte length prefix and ending newline
msg := err.Error()
pktLine := fmt.Sprintf("%04x%s\n", len(msg)+5, msg)
_, _ = s.Write([]byte(pktLine))
_, _ = wish.WriteString(s, pktLine)
s.Exit(1) // nolint: errcheck
}

Expand Down
57 changes: 52 additions & 5 deletions wish.go
Expand Up @@ -2,6 +2,7 @@ package wish

import (
"fmt"
"io"

"github.com/charmbracelet/keygen"
"github.com/gliderlabs/ssh"
Expand Down Expand Up @@ -37,14 +38,60 @@ func NewServer(ops ...ssh.Option) (*ssh.Server, error) {
return s, nil
}

// Fatal takes prints the given error to the given session's STDERR and exits 1.
func Fatal(s ssh.Session, err error) {
Error(s, err)
// Fatal prints to the given session's STDERR and exits 1.
func Fatal(s ssh.Session, v ...interface{}) {
Error(s, v...)
_ = s.Exit(1)
_ = s.Close()
}

// Fatalf formats according to the given format, prints to the session's STDERR
// followed by an exit 1.
func Fatalf(s ssh.Session, f string, v ...interface{}) {
Errorf(s, f, v...)
_ = s.Exit(1)
_ = s.Close()
}

// Fatalln formats according to the default format, prints to the session's
// STDERR followed by an exit 1.
func Fatalln(s ssh.Session, v ...interface{}) {
Errorln(s, v...)
_ = s.Exit(1)
_ = s.Close()
}

// Error prints the given error the the session's STDERR.
func Error(s ssh.Session, err error) {
_, _ = fmt.Fprintf(s.Stderr(), "%s\n\r", err)
func Error(s ssh.Session, v ...interface{}) {
_, _ = fmt.Fprint(s.Stderr(), v...)
}

// Errorf formats according to the given format and prints to the session's STDERR.
func Errorf(s ssh.Session, f string, v ...interface{}) {
_, _ = fmt.Fprintf(s.Stderr(), f, v...)
}

// Errorf formats according to the default format and prints to the session's STDERR.
func Errorln(s ssh.Session, v ...interface{}) {
_, _ = fmt.Fprintln(s.Stderr(), v...)
}

// Print writes to the session's STDOUT followed.
func Print(s ssh.Session, v ...interface{}) {
_, _ = fmt.Fprint(s, v...)
}

// Printf formats according to the given format and writes to the session's STDOUT.
func Printf(s ssh.Session, f string, v ...interface{}) {
_, _ = fmt.Fprintf(s, f, v...)
}

// Println formats according to the default format and writes to the session's STDOUT.
func Println(s ssh.Session, v ...interface{}) {
_, _ = fmt.Fprintln(s, v...)
}

// WriteString writes the given string to the session's STDOUT.
func WriteString(s ssh.Session, v string) (int, error) {
return io.WriteString(s, v)
}

0 comments on commit 92de95f

Please sign in to comment.