-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Toby Padilla
committed
Jul 30, 2021
1 parent
daeb6bf
commit 7c823a0
Showing
6 changed files
with
156 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
smoothie | ||
.ssh | ||
.repos |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/gliderlabs/ssh" | ||
) | ||
|
||
func GitMiddleware(repoDir string) Middleware { | ||
return func(sh ssh.Handler) ssh.Handler { | ||
return func(s ssh.Session) { | ||
cmd := s.Command() | ||
if len(cmd) == 2 { | ||
switch cmd[0] { | ||
case "git-upload-pack", "git-receive-pack", "git-upload-archive": | ||
r := cmd[1] | ||
rp := fmt.Sprintf("%s%s", repoDir, r) | ||
ctx := s.Context() | ||
err := ensureRepo(ctx, repoDir, r) | ||
if err != nil { | ||
fatalGit(s, err) | ||
break | ||
} | ||
c := exec.CommandContext(ctx, cmd[0], rp) | ||
c.Dir = "./" | ||
c.Stdout = s | ||
c.Stdin = s | ||
err = c.Run() | ||
if err != nil { | ||
fatalGit(s, err) | ||
break | ||
} | ||
} | ||
} | ||
sh(s) | ||
} | ||
} | ||
} | ||
|
||
func fileExists(path string) (bool, error) { | ||
_, err := os.Stat(path) | ||
if err == nil { | ||
return true, nil | ||
} | ||
if os.IsNotExist(err) { | ||
return false, nil | ||
} | ||
return true, err | ||
} | ||
|
||
func fatalGit(s ssh.Session, err error) { | ||
// hex length includes 4 byte length prefix and ending newline | ||
logError(s, err) | ||
msg := err.Error() | ||
pktLine := fmt.Sprintf("%04x%s\n", len(msg)+5, msg) | ||
_, err = s.Write([]byte(pktLine)) | ||
if err != nil { | ||
logError(s, err) | ||
} | ||
s.Exit(1) | ||
} | ||
|
||
func ensureRepo(ctx context.Context, dir string, repo string) error { | ||
exists, err := fileExists(dir) | ||
if err != nil { | ||
return err | ||
} | ||
if !exists { | ||
err = os.MkdirAll(dir, os.ModeDir|os.FileMode(0700)) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
rp := fmt.Sprintf("%s%s", dir, repo) | ||
exists, err = fileExists(rp) | ||
if err != nil { | ||
return err | ||
} | ||
if !exists { | ||
c := exec.CommandContext(ctx, "git", "init", "--bare", rp) | ||
err = c.Run() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.