diff --git a/.gitignore b/.gitignore index c2d6b91..cbac575 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,6 @@ examples/git/.repos .repos .ssh coverage.txt + +# MacOS specific +.DS_Store \ No newline at end of file diff --git a/git/git.go b/git/git.go index 4faf417..7ae587d 100644 --- a/git/git.go +++ b/git/git.go @@ -1,6 +1,7 @@ package git import ( + "errors" "fmt" "os" "os/exec" @@ -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 @@ -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": @@ -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 } @@ -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 } diff --git a/wish.go b/wish.go index 7526b69..1a53a54 100644 --- a/wish.go +++ b/wish.go @@ -2,6 +2,7 @@ package wish import ( "fmt" + "io" "github.com/charmbracelet/keygen" "github.com/gliderlabs/ssh" @@ -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) }