all repos

mugit @ c543efd

🐮 git server that your cow will love

mugit/internal/cli/ssh_shell.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
run errcheck, 28 days ago
1
package cli
2
3
import (
4
	"context"
5
	"errors"
6
	"fmt"
7
	"log/slog"
8
	"os"
9
10
	"github.com/urfave/cli/v3"
11
)
12
13
var errSSHDisabled = errors.New("ssh is disabled")
14
15
func (c *Cli) sshShellAction(ctx context.Context, cmd *cli.Command) error {
16
	if !c.cfg.SSH.Enable {
17
		return errSSHDisabled
18
	}
19
	if err := c.setupLogger(); err != nil {
20
		return err
21
	}
22
23
	sshCommand := os.Getenv("SSH_ORIGINAL_COMMAND")
24
	if err := c.ssh.HandleCommand(ctx, sshCommand, os.Stdin, os.Stdout, os.Stderr); err != nil {
25
		slog.Error("ssh command failed", "command", sshCommand, "err", err)
26
		os.Exit(1)
27
		return nil
28
	}
29
	return nil
30
}
31
32
func (c *Cli) sshAuthorizedKeysAction(ctx context.Context, cmd *cli.Command) error {
33
	if !c.cfg.SSH.Enable {
34
		return errSSHDisabled
35
	}
36
37
	fingerprint := cmd.Args().First()
38
	if fingerprint == "" {
39
		return fmt.Errorf("fingerprint is required")
40
	}
41
42
	executablePath, err := os.Executable()
43
	if err != nil {
44
		return err
45
	}
46
47
	out := c.ssh.AuthorizedKeys(executablePath)
48
	_, _ = fmt.Fprint(os.Stdout, out)
49
50
	return nil
51
}