Skip to content

Commit

Permalink
move gitCmd to a wish middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
pomdtr committed Feb 25, 2025
1 parent 67dea11 commit 1f4c444
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
39 changes: 23 additions & 16 deletions cmd/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,25 @@ import (
"github.com/spf13/cobra"
)

func NewCmdGitReceivePack() *cobra.Command {
func NewCmdGit(appDir string) *cobra.Command {
cmd := &cobra.Command{
Use: "git-receive-pack <git-dir>",
Short: "Git receive-pack",
Hidden: true,
Args: cobra.ExactArgs(1),
Use: "git",
}

cmd.AddCommand(NewCmdGitReceivePack(appDir))
cmd.AddCommand(NewCmdGitUploadPack(appDir))

return cmd
}

func NewCmdGitReceivePack(appDir string) *cobra.Command {
cmd := &cobra.Command{
Use: "git-receive-pack <git-dir>",
Short: "Git receive-pack",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
appDir := filepath.Join(k.String("dir"), args[0])
if baseDir := filepath.Dir(appDir); baseDir != k.String("dir") {
return fmt.Errorf("not in an app directory")
if args[0] != "/" {
return fmt.Errorf("invalid path")
}

reposDir := filepath.Join(k.String("dir"), ".smallweb", "repos")
Expand Down Expand Up @@ -71,16 +80,14 @@ func NewCmdGitReceivePack() *cobra.Command {
return cmd
}

func NewCmdGitUploadPack() *cobra.Command {
func NewCmdGitUploadPack(appDir string) *cobra.Command {
cmd := &cobra.Command{
Use: "git-upload-pack <git-dir>",
Short: "Git upload-pack",
Hidden: true,
Args: cobra.ExactArgs(1),
Use: "git-upload-pack <git-dir>",
Short: "Git upload-pack",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
appDir := filepath.Join(k.String("dir"), args[0])
if baseDir := filepath.Dir(appDir); baseDir != k.String("dir") {
return fmt.Errorf("not in an app directory")
if args[0] != "/" {
return fmt.Errorf("invalid path")
}

reposDir := filepath.Join(k.String("dir"), ".smallweb", "repos")
Expand Down
2 changes: 0 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ func NewCmdRoot() *cobra.Command {
rootCmd.AddCommand(NewCmdLink())
rootCmd.AddCommand(NewCmdConfig())
rootCmd.AddCommand(NewCmdSecrets())
rootCmd.AddCommand(NewCmdGitReceivePack())
rootCmd.AddCommand(NewCmdGitUploadPack())

if env, ok := os.LookupEnv("SMALLWEB_DISABLED_COMMANDS"); ok {
disabledCommands := strings.Split(env, ",")
Expand Down
39 changes: 38 additions & 1 deletion cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,44 @@ func NewCmdUp() *cobra.Command {
return
}
}
}),
},
func(next ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
args := s.Command()
if len(args) == 0 {
next(s)
return
}

if args[0] != "git-receive-pack" && args[0] != "git-upload-pack" {
next(s)
return
}

if s.User() == "_" {
fmt.Fprintf(s.Stderr(), "git command not allowed for user _\n")
s.Exit(1)
return
}

appDir := filepath.Join(k.String("dir"), s.User())
gitCmd := NewCmdGit(appDir)
gitCmd.SetArgs(args)

gitCmd.SetIn(s)
gitCmd.SetOut(s)
gitCmd.SetErr(s.Stderr())

if err := gitCmd.Execute(); err != nil {
fmt.Fprintf(s.Stderr(), "failed to execute git command: %v\n", err)
s.Exit(1)
return
}

s.Exit(0)
}
},
),
)

if err != nil {
Expand Down

0 comments on commit 1f4c444

Please sign in to comment.